栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python通过企业微信发送常用消息的方式集合,可以构建自用库方便调用

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python通过企业微信发送常用消息的方式集合,可以构建自用库方便调用

 希望得到你的支持,感谢点赞、关注、收藏

我常用的是:图片推送 、文字推送 、 卡片推送 、 图文推送

测试:

代码如下:

# -*- coding: utf-8 -*-
# by hearts

import requests
import json
import urllib.request

ID = ""  #企业ID
Secret = ""  #应用密码
UserID = "@all"  #成员ID列表(消息接收者,多个接收者用'|'分隔,最多支持1000个)。特殊情况:指定为@all,则向关注该企业应用的全部成员发送
PartyID = 1  #部门ID列表,多个接收者用‘|’分隔,最多支持100个。当touser为@all时忽略本参数
AppID = 1000001  #应用ID


def get_token():
    gurl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid={}&corpsecret={}".format(ID, Secret)
    r = requests.get(gurl)
    dict_result = (r.json())
    print("获取到企业微信token:"+dict_result['access_token'])
    return dict_result['access_token']
def get_media_ID(path):
    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)
    print("获取临时素材ID:" + re['media_id'])
    return re['media_id']
def send_text(text):
    post_data = {}
    msg_content = {}
    msg_content['content'] = text  
    post_data['touser'] = UserID
    post_data['toparty'] = PartyID
    post_data['msgtype'] = 'text'
    post_data['agentid'] = AppID
    post_data['text'] = msg_content
    post_data['enable_id_trans'] = '0' 
    post_data['enable_duplicate_check'] = '0'
    post_data['duplicate_check_interval'] = '1800'
    Gtoken = get_token()
    purl1 = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={}".format(Gtoken)
    json_post_data = json.dumps(post_data, skipkeys=False, ensure_ascii=False)
    request_post = urllib.request.urlopen(purl1, json_post_data.encode(encoding='UTF8'))
    print("文字发送成功")
    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'
    post_data1['enable_duplicate_check'] = '0'
    post_data1['duplicate_check_interval'] = '1800'
    Gtoken = get_token()
    purl2 = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={}".format(Gtoken)
    json_post_data1 = json.dumps(post_data1, skipkeys=False, ensure_ascii=False)
    request_post = urllib.request.urlopen(purl2, json_post_data1.encode(encoding='UTF8'))
    print("图片发送成功")
    return request_post
def send_cardtext(cardtxt):
    Msg = {
            "title": "卡片消息",
            "description": cardtxt,
            "url": "www.baidu.com",
            "btntxt": "更多"
        }
    post_data = {}
    post_data['touser'] = UserID
    post_data['toparty'] = PartyID
    post_data['msgtype'] = 'textcard'
    post_data['agentid'] = AppID
    post_data['textcard'] = Msg
    post_data['enable_id_trans'] = '0'
    post_data['enable_duplicate_check'] = '0'
    post_data['duplicate_check_interval'] = '1800'
    Gtoken = get_token()
    purl3 = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={}".format(Gtoken)
    json_post_data = json.dumps(post_data, skipkeys=False, ensure_ascii=False)
    request_post = urllib.request.urlopen(purl3, json_post_data.encode(encoding='UTF8'))
    print("卡片发送成功")
    return request_post
def send_news(title, description, url, picurl):
    msg = {"articles": [{"title": title,"description": description,"url": url,"picurl": picurl}]}
    post_data = {}
    post_data['touser'] = UserID
    post_data['toparty'] = PartyID
    post_data['msgtype'] = 'news'
    post_data['agentid'] = AppID
    post_data['news'] = msg
    post_data['enable_id_trans'] = '0'
    post_data['enable_duplicate_check'] = '0'
    post_data['duplicate_check_interval'] = '1800'
    Gtoken = get_token()
    purl3 = "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={}".format(Gtoken)
    json_post_data = json.dumps(post_data, skipkeys=False, ensure_ascii=False)
    request_post = urllib.request.urlopen(purl3, json_post_data.encode(encoding='UTF8'))
    print("图文发送成功")
    return request_post

因此整理的就是这些啦~

将上面代码复制到文件夹下创建一个 qiyeweixin.py 的文件即可,调用就是引用里面的某个函数即可:

#示例,调用图文发送模块
from qiyeweixin import send_news

各种方式发送代码如下:

send_tu("LOGO.png")  #图片的名称

send_text("测试nhello world")  #文字内容

send_cardtext("测试hello world")  #卡片内容,灰色(gray)、高亮(highlight)、默认黑色(normal)

send_news(title="@图文推送系统", description="测试:nhello world", url="www.baidu.com", picurl="https://img-home.csdnimg.cn/images/20201124032511.png")

 快去试试吧

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/589308.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号