100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > python微信接口发送消息_Python 微信公众号发送消息

python微信接口发送消息_Python 微信公众号发送消息

时间:2020-12-11 16:06:07

相关推荐

python微信接口发送消息_Python 微信公众号发送消息

#pip3 install requests

importrequestsimportjsondefget_access_token():"""获取微信全局接口的凭证(默认有效期俩个小时)

如果不每天请求次数过多, 通过设置缓存即可"""result=requests.get(

url="https://api./cgi-bin/token",

params={"grant_type": "client_credential","appid": "wx2499da7621f818e8","secret": "6239e3dfc5af686777ea40b9f3df5f48",

}

).json()if result.get("access_token"):

access_token= result.get('access_token')else:

access_token=Nonereturnaccess_tokendefsendmsg(openid,msg):

access_token=get_access_token()

body={"touser": openid,"msgtype": "text","text": {"content": msg

}

}

response=requests.post(

url="https://api./cgi-bin/message/custom/send",

params={'access_token': access_token

},

data=bytes(json.dumps(body, ensure_ascii=False), encoding='utf-8')

)#这里可根据回执code进行判定是否发送成功(也可以根据code根据错误信息)

result =response.json()print(result)if __name__ == '__main__':

sendmsg('关注者的ID','发送消息内容')

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。