溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python3開發(fā)微信企業(yè)號發(fā)送圖文的示例源碼

發(fā)布時間:2021-01-25 10:03:58 來源:億速云 閱讀:141 作者:小新 欄目:移動開發(fā)

小編給大家分享一下python3開發(fā)微信企業(yè)號發(fā)送圖文的示例源碼,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

 
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import json
import urllib.request
 
ID="xxxxxxxxxxxx"  
Secret="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
 
UserID = "hequan2011"##成員ID列表(消息接收者,多個接收者用'|'分隔,最多支持1000個)。特殊情況:指定為@all,則向關(guān)注該企業(yè)應(yīng)用的全部成員發(fā)送
PartyID=17  ##部門ID列表,多個接收者用‘|’分隔,最多支持100個。當touser為@all時忽略本參數(shù)
AppID = 0  ##應(yīng)用ID,默認是 企業(yè)小助手  企業(yè)應(yīng)用的id,整型。可在應(yīng)用的設(shè)置頁面查看
 
 
 
def get_token():  ##獲取TOKEN
    gurl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={}&corpsecret={}".format(ID, Secret)
    r=requests.get(gurl)
    dict_result= (r.json())
    return dict_result['access_token']
def get_media_ID(path):  ##上傳到臨時素材  圖片ID
    Gtoken = get_token()
    img_url = "https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={}&type=image".format(Gtoken)
    files = {'image': open(path, 'rb')}
    r = requests.post(img_url, files=files)
    re = json.loads(r.text)
    return re['media_id']
##
 
 
  
def  send_text(text):  ##發(fā)送文字
    post_data = {}
    msg_content = {}
    msg_content['content'] = text  ## 消息內(nèi)容,最長不超過2048個字節(jié)
    post_data['touser'] = UserID
    post_data['toparty'] = PartyID
    post_data['msgtype'] = 'text'
    post_data['agentid'] = AppID
    post_data['text'] = msg_content
    post_data['safe'] = '0'  #表示是否是保密消息,0表示否,1表示是,默認0
    Gtoken = get_token()
    purl1="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={}".format(Gtoken)
    json_post_data = json.dumps(post_data,False,False)
    request_post = urllib.request.urlopen(purl,json_post_data.encode(encoding='UTF8'))
    return request_post
     
     
     
def  send_tu(path):  ##發(fā)送圖片
    img_id = get_media_ID(path)
    post_data1 = {}
    msg_content1 = {}
    msg_content1['media_id'] = img_id
    post_data1['touser'] = UserID
    post_data1['toparty'] = PartyID
    post_data1['msgtype'] = 'image'
    post_data1['agentid'] = AppID
    post_data1['image'] = msg_content1
    post_data1['safe'] = '0'
    Gtoken = get_token()
    purl2="https://cache.yisu.com/upload/information/20201208/260/11154")  ##圖片目錄
send_text("123-何全")  ##文字內(nèi)容

以上是“python3開發(fā)微信企業(yè)號發(fā)送圖文的示例源碼”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI