微信小程序 python 发送客服消息

#!/usr/bin/python
# coding=utf-8
import requests, json, sys, time, logging

if sys.getdefaultencoding() != 'utf-8':
    reload(sys)
    sys.setdefaultencoding('utf-8')
logging.basicConfig(format='%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s',
                    level=logging.DEBUG, filename='/var/tmp/wxapp_push.log',
                    filemode='a')

appid = 'xxx';
appSecret = 'xxx';

url_access_token = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=' + appid + '&secret=' + appSecret

touser = sys.argv[1]
subject = "服务器异常"

message = "受限于字数限制,请点击进入查看详情"

tokenRes = requests.get(url_access_token)

token = json.loads(tokenRes.text).get('access_token')

logging.info(token)

url_custom_msg = 'https://api.weixin.qq.com/cgi-bin/message/custom/send';
body = {
    "touser": touser,
    "msgtype": "text",
    "text":
        {
            "content": sys.argv[1] + '\n' + sys.argv[2]
        }
}
res = requests.post(url=url_custom_msg, params={
    'access_token': token  # 这里是我们上面获取到的token
}, data=json.dumps(body, ensure_ascii=False).encode('utf-8'))
print(res.text)

Leave a Comment

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据