Advertisement

python企业微信机器人_python发企业微信机器人

阅读量:

import requests

import base64

import hashlib

import os

#参数bot是指机器人的key,具体可参考企业微信机器人官网

#发送普通消息,text是文本内容

def send_text(text,bot):

url = f"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={bot}"

headers = {"Content-Type": "text/plain"}

data = {

"msgtype": "text",

"text": {

"content": text,

}

}

r = requests.post(url, headers=headers, json=data)

print(r.text)

#发送markdown消息,text是文本内容,可接受markdown语法

def send_markdown(text,bot):

url = f"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={bot}"

data = {

"msgtype": "markdown",

"markdown": {

"content": text

}}

r = requests.post(url,json=data)

print(r.text)

#发送文件,file_path是指文件路径(我目前就试了excel文件)

def send_file(file_path,bot):

指定为 file_url = f-string https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key={bot}&type=file

file= {'file':open(file_path,'rb')}

result = requests.post(file_url, files=file)

file_id = eval(result.text)['media_id']

url = f"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={bot}"

data = {

"msgtype": "file",

"file": {"media_id": file_id,}

}

r = requests.post(url, json=data)

print(r.text)

#发送图片,file_path是指图片路径

def send_img(file_path,bot):

with open(file_path,"rb") as f:

base64_data = base64.b64encode(f.read())

file = open(file_path, "rb")

md = hashlib.md5()

md.update(file.read())

res1 = md.hexdigest()

url = f"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key={bot}"

headers = {"Content-Type": "text/plain"}

data = {

"msgtype": "image",

"image": {

"base64": base64_data.decode('utf-8'),

"md5": res1

}

}

r = requests.post(url, headers=headers, json=data)

print(r.text)

全部评论 (0)

还没有任何评论哟~