資源簡介

代碼片段和文件信息
#?-*-?coding:?utf-8?-*-
import?urllib2
from?urllib?import?urlencode
import?simplejson
import?sys
reload(sys)
sys.setdefaultencoding(‘utf-8‘)#wx?only?adapt?utf-8?(no?unicodeno?encoding?and?stay?what?it?looks?like)
Debug?=?True
IsMyQYH?=?True
def?getTokenIntime(CorpIdSecret):
????res?=?urllib2.urlopen(‘https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s‘%(CorpIdSecret))
????res_dict?=?simplejson.loads(res.read())
????token?=?res_dict.get(‘access_token‘False)
????if?not?token:
????????with?open(r‘./wx_log.txt‘‘ab‘)?as?f:
????????????f.write(u‘異常:?無法取得token‘)
????return?token
def?getToken():
????with?open(r‘./wx_token.txt‘)?as?f:
????????t?=?f.read()
????return?t
????
def?sendTxtMsg(tokencontentto_user=“@all“to_party=““to_tag=““application_id=0safe=0):
????try:
????????data?=?{
???????????“touser“:?to_user
???????????“toparty“:?to_party
???????????“totag“:?to_tag
???????????“msgtype“:?“text“
???????????“agentid“:?application_id
???????????“text“:?{
???????????????“content“:?content
???????????}
???????????“safe“:safe
????????}
????????data?=?simplejson.dumps(dataensure_ascii=False)
????????if?Debug:
????????????with?open(r‘./wx_data.txt‘‘ab‘)?as?f:
????????????????f.write(data+‘\r\n‘)
????????req?=?urllib2.Request(‘https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=%s‘%(token))????????
????????resp?=?urllib2.urlopen(reqdata)
????????msg?=?u‘返回值:‘?+?resp.read()
????except?Exceptionex:
????????msg?=?u‘異常:‘?+?str(ex)
????finally:
????????with?open(r‘./wx_log.txt‘‘ab‘)?as?f:
????????????f.write(msg+‘\r\n‘)
def?sendNews(tokentitledescriptionurlpic_urlto_user=“@all“to_party=““to_tag=““application_id=0safe=0):
????try:
????????data?=?{
???????????“touser“:?to_user
???????????“toparty“:?to_party
???????????“totag“:?to_tag
???????????“msgtype“:?“news“
???????????“agentid“:?application_id
???????????“news“:?{
???????????????“articles“:[
???????????????????{
???????????????????????“title“:?title
???????????????????????“description“:?description
???????????????????????“url“:?url
???????????????????????“picurl“:?pic_url
???????????????????}????
???????????????]
???????????}
????????}
?????
????????data?=?simplejson.dumps(dataensure_ascii=False)
????????if?Debug:
????????????with?open(r‘./wx_data.txt‘‘ab‘)?as?f:
????????????????f.write(data+‘\r\n‘)
????????req?=?urllib2.Request(‘https://qyapi.weixin.qq.com/cgi-bin/message/send?debug=1&access_token=%s‘%(token))
????????resp?=?urllib2.urlopen(reqdata)
????????msg?=?u‘返回值:‘?+?resp.read()
????except?Exceptionex:
????????msg?=?u‘異常:‘?+?str(ex)
????finally:
????????with?open(r‘./wx_log.txt‘‘ab‘)?as?f:
????????????f.write(msg?+?‘\r\n‘)
def?send_btn(tokenapplication_id=0):
????try:
????????req?=?urllib2.Request(‘https://qyapi.weixin.qq.com/cgi-bin/menu/create?access_token=%s&agentid=%s‘%(tokenapplication_id))
????????data?=?{
???????????“button
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件??????11981??2014-10-09?20:54??wx_data.txt
?????文件???????4761??2014-10-09?20:54??IWeixin.py
-----------?---------??----------?-----??----
????????????????16742????????????????????2
- 上一篇:SEO常用數據分析表格模板
- 下一篇:用Proteus仿真的sht11電路
評論
共有 條評論