温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python3开发微信企业号发送图文的示例源码

发布时间:2021-01-25 10:03:58 来源:亿速云 阅读:142 作者:小新 栏目:移动开发

小编给大家分享一下python3开发微信企业号发送图文的示例源码,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

 
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import requests
import json
import urllib.request
 
ID="xxxxxxxxxxxx"  
Secret="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
 
UserID = "hequan2011"##成员ID列表(消息接收者,多个接收者用'|'分隔,最多支持1000个)。特殊情况:指定为@all,则向关注该企业应用的全部成员发送
PartyID=17  ##部门ID列表,多个接收者用‘|’分隔,最多支持100个。当touser为@all时忽略本参数
AppID = 0  ##应用ID,默认是 企业小助手  企业应用的id,整型。可在应用的设置页面查看
 
 
 
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):  ##发送文字
    post_data = {}
    msg_content = {}
    msg_content['content'] = text  ## 消息内容,最长不超过2048个字节
    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):  ##发送图片
    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-何全")  ##文字内容

以上是“python3开发微信企业号发送图文的示例源码”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI