您好,登錄后才能下訂單哦!
python2.x 注意:data變量里, agent_id為剛剛創(chuàng)建的應(yīng)用id(可在web頁面看到) toparty即為目標(biāo)部門,或者可以用touser,totag指定目標(biāo)賬戶 比較簡(jiǎn)單的調(diào)用,已實(shí)測(cè),可以使用。 #coding:utf-8 import sys import requests import json from pymongo import MongoClient reload(sys) sys.setdefaultencoding('utf-8') class Weixin(object): def __init__(self, corp_id, corp_secret): self.token_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s' %(corp_id, corp_secret) self.send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' def get_token(self): try: r = requests.get(self.token_url, timeout=10) except Exception as e: print e sys.exit(1) if r.status_code == requests.codes.ok: data = r.json() if data.get('errcode'): print data['errmsg'] sys.exit(1) return data['access_token'] else: print r.status_code sys.exit(1) def send(self,message): url = self.send_url + self.get_token() data = { "touser": "hequan2011", "msgtype": "text", "agentid": "0", "text": { "content": message }, "safe":"0" } send_data = json.dumps(data,ensure_ascii=False) try: r = requests.post(url, send_data) except Exception, e: print e sys.exit(1) if r.status_code == requests.codes.ok: print r.json() else: print r.code sys.exit(1) corpid = 'xxxxxxxxxxx' corpsecret = 'xxxxxxxxxxxxxxxxx' client = MongoClient('mongodb://user:password@127.0.0.1:27017/') db = client.ku collection = db.biao a = [] for data in collection.find(): a.append(data) l = a[0] g = l z = str(g["name"]) z1 = int(g["jg"]) print z msg = "1:{0}\n 2:{1}\n".format(z,z1) w = Weixin(corpid,corpsecret) w.send(msg)
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。