Python与企业微信-2

上一篇文章讲了企业微信可以帮助我们发送消息,可以用Python写代码随时随地跑。

这篇再聊一下如何发送图片、视频、文本卡片等内容
在这里插入图片描述

1、发送图片:

跟发送文本主要的地方在于:图片要先上传,才能发送。
其他的都是小改动,注意下就行。

那么怎么上传呢?
之前调用百度api的时候,也遇到了传图片的问题。百度给了两种方式:
a:把图片转成二进制的base64字节码
b:把文件上传到任意图床,然后把图床地址当参数给传过去。

但是,企业微信不同。ta用的是这个“上传临时素材”接口(https://developer.work.weixin.qq.com/document/path/90253)。方式上感觉跟b类似。只不过是企业微信又做了一道封装,把文件上传到tx的服务器后,tx返回一个id,用这个id来标识这个文件。

对于这种方式,我感觉挺坑的。
而且,说明文档说的不是很明白,模模糊糊。
比如这里:使用multipart/form-data POST上传文件, 文件标识名为"media"。
我才疏学浅,不太会这个什么multipart;文件标识名为media又是啥?

我只想问下企业微信,你不能写一段演示代码嘛!!!文档就不能写的别那么绕么!!!

在这里插入图片描述
吐槽完毕。

上传代码,如下:

新建upload_tmp_file.py

import requests
import os
import config
import faker
import json

fk = faker.Faker()

# 也可以用官方的工具上传。传完了会有一个media_id,然后再用这个media_id,填写到send_image里的media_id。
# https://open.work.weixin.qq.com/wwopen/devtool/interface?doc_id=10112

# 图片文件,用下面的url
# url = f"https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={config.access_token}&type=image"
# voice语音文件,用下面的url
# url = f"https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={config.access_token}&type=voice"

# video,视频文件,用下面的url
# url = f"https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={config.access_token}&type=video"

# file,文件,用下面的url
url = f"https://qyapi.weixin.qq.com/cgi-bin/media/upload?access_token={config.access_token}&type=file"


dir_path = os.path.abspath(os.path.dirname(__file__))
file_path = os.path.join(dir_path,'1.jpg') # 改要上传的文件

payload={'filename': 'xxx file','filelength': '','name': 'media'} 
files= {'files':('1.jpg',open(file_path,'rb'),'text/plain'), 
}
headers = {'User-Agent': fk.user_agent()}

response = requests.request("POST", url, headers=headers, data=payload, files=files)
dic = dict(json.loads(response.text))
print(dic.get("media_id"))

运行此代码,会得到这个东西(media_id)。抄下来记到小本本上。
在这里插入图片描述

发送图片代码,如下:

import requests
import json
import faker
from work_wechat import config
fk = faker.Faker()

# 代码参数里很多id,都是微信企业号里的。不能随便填。id的概念看这个文章:
# https://developer.work.weixin.qq.com/document/path/90665

url = f"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={config.access_token}&random=69152"


payload = json.dumps({
   "touser": config.touser,
   "toparty": config.toparty,
   "totag": config.totag,
   "msgtype": "image",
   "agentid": config.agentid_ceshi1,
   "image": {
      "media_id": "3p6o7ZZd2cKTiJ-aoupmD5jrXTIOwDsvWDw2SZkwgC4OXDcWA6b62SqhLKIjAXTPq"# 把上一步上传之后得到的那个media_id写到这里
   },
   "safe": 0,
   "enable_duplicate_check": 0,
   "duplicate_check_interval": 1800
})
headers = {
   'User-Agent': fk.user_agent(),
   'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)

1、把上一步上传之后得到的那个media_id写到这里
2、执行此代码,执行完成后,打开手机企业微信。查看图片是否收到。
这里就不截图了,第一篇文章里已经展示过了图片的发送结果。

发送视频信息

1、按照上面上传文件的步骤,上传一个视频(注意视频有大小限制)。获取到id之后填入下面代码。
在这里插入图片描述

代码如下:

import requests
import json
import faker
from work_wechat import config
fk = faker.Faker()

# 代码参数里很多id,都是微信企业号里的。不能随便填。id的概念看这个文章:
# https://developer.work.weixin.qq.com/document/path/90665

url = f"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={config.access_token}&random=69152"


payload = json.dumps({
   "touser": config.touser,
   "toparty": config.toparty,
   "totag": config.totag,
   "msgtype": "video",
   "agentid": config.agentid_ceshi1,
   "video" : {
        "media_id" : "3YcVVN30PRfvYZ4dFK4ojPVCmAv5Tc45Kqi6LxZig-4vwskG8uj2Qp65EPdec8m7T",
        "title" : "这里是标题头",
       "description" : "这里是视频描述"
   },
   "safe": 0,
   "enable_duplicate_check": 0,
   "duplicate_check_interval": 1800
})
headers = {
   'User-Agent': fk.user_agent(),
   'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)

运行后,查看手机上的企业微信。

在这里插入图片描述
在这里插入图片描述

发送语音(仅支持amr格式)

方法跟上面类似,不再多说。参考下官方文档:https://developer.work.weixin.qq.com/document/path/90236

发送普通文件

方法跟上面类似,不再多说。参考下官方文档:https://developer.work.weixin.qq.com/document/path/90236

发送文本卡片

代码如下:

import requests
import json
import faker
from work_wechat import config
fk = faker.Faker()

# 代码参数里很多id,都是微信企业号里的。不能随便填。id的概念看这个文章:
# https://developer.work.weixin.qq.com/document/path/90665

url = f"https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={config.access_token}&random=69152"


payload = json.dumps({
   "touser": config.touser,
   "toparty": config.toparty,
   "totag": config.totag,
   "msgtype": "textcard",
   "agentid": config.agentid_ceshi1,
   "textcard" : {
            "title" : "文本卡片模式推送测试",
            "description" : "<div class=\"gray\">2022年4月12日</div> <div class=\"normal\">文本卡片推送测试</div><div class=\"highlight\">textcard推送测试</div>",
            "url" : "https://www.baidu.com",
                        "btntxt":"更多"
   },

   "safe": 0,
   "enable_duplicate_check": 0,
   "duplicate_check_interval": 1800
})
headers = {
   'User-Agent': fk.user_agent(),
   'Content-Type': 'application/json'
}

response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)

效果如下:
在这里插入图片描述

其他的就不多说了,只要上边的学会了。其他的基本上没啥难度。
第二篇结束。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值