天天看點

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"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)