Python編程實現(xiàn)微信企業(yè)號文本消息推送功能示例
本文實例講述了Python微信企業(yè)號文本消息推送功能。分享給大家供大家參考,具體如下:
企業(yè)號的創(chuàng)建、企業(yè)號應(yīng)用的創(chuàng)建、組、tag、part就不贅述了,一搜一大堆,但是網(wǎng)上拿的那些個腳本好多都不好使,所以自己修了一個
坦率的講,這個腳本是用來作為zabbix的通知媒介腳本的,本人是個菜鳥,如果哪里不對,大神們不要笑話,python也處于學(xué)習(xí)階段,如果有哪些地方不合理,很希望可以不吝賜教,廢話不多說,腳本奉上:
#!/usr/bin/python
# _*_coding:utf-8 _*_
import urllib2
import json
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
def gettoken(corpid, corpsecret):
gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret
try:
token_file = urllib2.urlopen(gettoken_url)
except urllib2.HTTPError as e:
print e.code
print e.read().decode("utf8")
sys.exit()
token_data = token_file.read().decode('utf-8')
token_json = json.loads(token_data)
token_json.keys()
token = token_json['access_token']
return token
def senddata(access_token, user, party, agent, subject, content):
send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token
send_values = "{\"touser\":\"" + user + "\",\"toparty\":\"" + party + "\",\"totag\":\"\",\"msgtype\":\"text\",\"agentid\":\"" + agent + "\",\"text\":{\"content\":\"" + subject + "\n" + content + "\"},\"safe\":\"0\"}"
send_request = urllib2.Request(send_url, send_values)
response = json.loads(urllib2.urlopen(send_request).read())
print str(response)
if __name__ == '__main__':
user = str(sys.argv[1]) # 參數(shù)1:發(fā)送給用戶的賬號,必須關(guān)注企業(yè)號,并對企業(yè)號有發(fā)消息權(quán)限
party = str(sys.argv[2]) # 參數(shù)2:發(fā)送給組的id號,必須對企業(yè)號有權(quán)限
agent = str(sys.argv[3]) # 參數(shù)3:企業(yè)號中的應(yīng)用id
subject = str(sys.argv[4]) # 參數(shù)4:標(biāo)題【消息內(nèi)容的一部分】
content = str(sys.argv[5]) # 參數(shù)5:文本具體內(nèi)容
corpid = 'CorpID' # CorpID是企業(yè)號的標(biāo)識
corpsecret = 'corpsecretSecret' # corpsecretSecret是管理組憑證密鑰
try:
accesstoken = gettoken(corpid, corpsecret)
senddata(accesstoken, user, party, agent, subject, content)
except Exception, e:
print str(e) + "Error Please Check \"corpid\" or \"corpsecret\" Config"
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python字符串操作技巧匯總》、《Python編碼操作技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》及《Python入門與進階經(jīng)典教程》。
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
Python猜解網(wǎng)站數(shù)據(jù)庫管理員密碼的腳本
這篇文章主要和大家分享一個Python腳本,可以實現(xiàn)猜解網(wǎng)站數(shù)據(jù)庫管理員的密碼。文中的示例代碼講解詳細,需要的小伙伴可以參考一下2022-02-02
使用Python實現(xiàn)操作控制鼠標(biāo)和鍵盤
Python 有很多的庫可以實現(xiàn)各種各樣的功能,比如使用 pynput 操作,下面小編就來和大家詳細介紹一下如何使用pynput進行操作控制鼠標(biāo)和鍵盤吧2024-02-02
使Python代碼流暢無縫連接的鏈?zhǔn)秸{(diào)用技巧
鏈?zhǔn)秸{(diào)用是一種編程風(fēng)格,它允許將多個方法調(diào)用連接在一起,形成一個連貫的操作鏈,在Python中,鏈?zhǔn)秸{(diào)用常常用于使代碼更簡潔、易讀,尤其在處理數(shù)據(jù)處理和函數(shù)式編程中應(yīng)用廣泛2024-01-01

