import requests
import json
class WXWork_SMS :
# 文本类型消息
def send_msg_txt(self) :
headers = {"Content-Type" : "text/plain"}
send_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=69e3bed5-a20e-45be-9999-e30c0398c16c"
send_data = {
"msgtype": "text", # 消息类型,此时固定为text
"text": {
"content": "上海今日天气:32度,大部分多云,降雨概率:10%", # 文本内容,最长不超过2048个字节,必须是utf8编码
"mentioned_list":["@all"], # userid的列表,提醒群中的指定成员(@某个成员),@all表示提醒所有人,如果开发者获取不到userid,可以使用mentioned_mobile_list
"mentioned_mobile_list":["@all"] # 手机号列表,提醒手机号对应的群成员(@某个成员),@all表示提醒所有人
}
}
res = requests.post(url = send_url, headers = headers, json = send_data)
print(res.text)
# markdown类型消息
def send_msg_markdown(self) :
headers = {"Content-Type" : "text/plain"}
send_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=69e3bed5-a20e-45be-9999-e30c0398c16c"
send_data = {
"msgtype": "markdown", # 消息类型,此时固定为markdown
"markdown": {
"content": "# **提醒!实时新增用户反馈**<font color=\"warning\">**123例**</font>\n" + # 标题 (支持1至6级标题,注意#与文字中间要有空格)
"#### **请相关同事注意,及时跟进!**\n" + # 加粗:**需要加粗的字**
"> 类型:<font color=\"info\">用户反馈</font> \n" + # 引用:> 需要引用的文字
"> 普通用户反馈:<font color=\"warning\">117例</font> \n" + # 字体颜色(只支持3种内置颜色)
"> VIP用户反馈:<font color=\"warning\">6例</font>" # 绿色:info、灰色:comment、橙红:warning
}
}
res = requests.post(url = send_url, headers = headers, json = send_data)
print(res.text)
# 图文类型消息
def send_msg_txt_img(self) :
headers = {"Content-Type" : "text/plain"}
send_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=69e3bed5-a20e-45be-9999-e30c0398c16c"
send_data = {
"msgtype": "news", # 消息类型,此时固定为news
"news": {
"articles" : [ # 图文消息,一个图文消息支持1到8条图文
{
"title" : "中秋节礼品领取", # 标题,不超过128个字节,超过会自动截断
"description" : "今年中秋节公司有豪礼相送", # 描述,不超过512个字节,超过会自动截断
"url" : "www.baidu.com", # 点击后跳转的链接。
"picurl" : "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png" # 图文消息的图片链接,支持JPG、PNG格式,较好的效果为大图 1068*455,小图150*150。
},
{
"title" : "我的CSDN - 魏风物语", # 标题,不超过128个字节,超过会自动截断
"description" : "坚持每天写一点点", # 描述,不超过512个字节,超过会自动截断
"url" : "https://blog.csdn.net/itanping", # 点击后跳转的链接。
"picurl" : "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png" # 图文消息的图片链接,支持JPG、PNG格式,较好的效果为大图 1068*455,小图150*150。
},
{
"title" : "百度一下 - 魏风物语", # 标题,不超过128个字节,超过会自动截断
"description" : "百度一下魏风物语,百度一下魏风物语", # 描述,不超过512个字节,超过会自动截断
"url" : "https://blog.csdn.net/itanping", # 点击后跳转的链接。
"picurl" : "http://res.mail.qq.com/node/ww/wwopenmng/images/independent/doc/test_pic_msg1.png" # 图文消息的图片链接,支持JPG、PNG格式,较好的效果为大图 1068*455,小图150*150。
}
]
}
}
res = requests.post(url = send_url, headers = headers, json = send_data)
print(res.text)
if __name__ == '__main__' :
sms = WXWork_SMS()
# 文本类型消息
sms.send_msg_txt()
# markdown类型消息
sms.send_msg_markdown()
# 图文类型消息
sms.send_msg_txt_img()