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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值