python - 我如何向不和谐请求添加附件?
问题描述
我正在尝试制作发送不和谐消息+附件的python脚本,但我不知道该怎么做。此外,我需要从我的不和谐帐户发送消息,而不是从机器人/网络钩子发送消息。到目前为止,这是我的代码:
payload = {
'content': "Hello"
}
header = {
'authorization': "token"
}
r = requests.post("https://discord.com/api/v9/channels/874777490781003787/messages?limit=50", data=payload, headers=header)
解决方案
使用discord.py而不是 requests 库的一站式解决方案:
with open('my_image.png', 'rb') as f: channel.send(file=discord.File(f))
如果您确实必须使用 requests 库,则必须使用Content-Type: multipart/form-data
文件属性Content-Disposition: form-data; name="file"; filename="your_attachment.png" Content-Type: image/png
和消息属性来上传文件Content-Disposition: form-data; name="payload_json" {"content":"your_content","tts":false}
。
所以把它们放在一个嵌套在这样的字典中的元组中:
files = {
'file': ('./picture.jpg', open('./picture.jpg', 'rb')),
}
r = requests.post("https://discord.com/api/v9/channels/874777490781003787/messages?limit=50", data=payload, headers=header, files=files)
在此处阅读文档。
推荐阅读
- azure-ad-b2c - 使用身份验证方法创建 b2c 用户作为 SSPR 的电子邮件
- reactjs - 在线将 reactjs 应用程序部署到 herokuapp 的问题
- azure - 如何在看门狗中获取 VM Scale 中所有 VM 的状态
- objective-c - WKWebView 实际上并不遵循重定向
- apache-spark - Kafka 删除(墓碑)不更新 Spark 结构化流中的最大聚合
- javascript - 用 Promise 同步 javascript 函数,socket.io 发射
- c - 关于如何添加保存在两个结构中的两个数字并将它们保存在第三个结构中的任何想法
- objective-c - 在 Objective-C 中,在执行密集操作的循环中,使用 @synchonized 是否有利于实现的稳定性?
- php - 如何使用 PHP 中的 prepare 方法将数据从一个表移动到另一个表
- javascript - 输入不更新