【问题标题】:Sending audio to user in Facebook messenger bot在 Facebook Messenger bot 中向用户发送音频
【发布时间】:2017-10-05 07:23:27
【问题描述】:

我正在尝试使用文件上传将音频文件发送给 Facebook Messenger 机器人中的用户。 官方文档说你可以通过终端中的 curl 命令来完成。 此命令有效:

curl  \
  -F 'recipient={"id":user_id}' \
  -F 'message={"attachment":{"type":"audio", "payload":{}}}' \
  -F 'filedata=@mymp3.mp3;type=audio/mp3' \
  "https://graph.facebook.com/v2.6/me/messages?access_token=ACCESS_TOKEN"

其中 ACCESS_TOKEN 是我页面的令牌,“mymp3.mp3”是我要发送的文件。

问题 - 我如何在 python 中使用 requests 库做同样的事情?

我试过了:

with open("mymp3.mp3", "rb") as o:
    payload = {
        'recipient': "{id: 1336677726453307}",
        'message': {'attachment': {'type': 'audio', 'payload':{}}},
        'filedata':o, 'type':'audio/mp3'
    }
    files = {'recipient': {'id': '1336677726453307'},'filedata':o}
    headers = {'Content-Type': 'audio/mp3'}
    r = requests.post(fb_url, data=payload)
print r.text

我收到此错误:

{"error":{"message":"(#100) Message cannot be empty, must provide valid attachment or text","type":"OAuthException","code":100,"error_subcode":2018034,"fbtrace_id":"E5d95+ILnf5"}}

另外,试过这个:

import requests
from requests_toolbelt import MultipartEncoder

m = MultipartEncoder(
    fields={
        'recipient': {'id': '1336677726453307'},
        'message': {'attachment': {'type': 'audio', 'payload':{}}},
        'filedata':(open("mymp3.mp3", "rb"), 'audio/mp3')
    }
)
headers = {'Content-Type': m.content_type}
r = requests.post(fb_url, data=m, headers=headers)
print r.text

我收到了这个错误: AttributeError: 'dict' 对象没有属性 'encode'

【问题讨论】:

标签: python facebook facebook-graph-api curl python-requests


【解决方案1】:

好的,我明白了(非常感谢我的同事!)

fb_url = 'https://graph.facebook.com/v2.6/me/messages'
data = {
    'recipient': '{"id":1336677726453307}',
    'message': '{"attachment":{"type":"audio", "payload":{}}}'
}
files = {
    'filedata': ('mymp3.mp3', open("mymp3.mp3", "rb"), 'audio/mp3')}
params = {'access_token': ACCESS_TOKEN}
resp = requests.post(fb_url, params=params, data=data, files=files)

【讨论】:

    【解决方案2】:

    我认为您的第一次尝试接近目标,但您似乎在上传文件时遇到了困难。 您首先需要了解curl 的作用; curl documentation is here。然后对请求做同样的事情;文档是here。

    据我了解,你可以试试:

    with open("mymp3.mp3", "rb") as finput:
        data = {
            'recipient': "{id: 1336677726453307}",
            'message': {'attachment': {'type': 'audio', 'payload':{}}},
        }
        #
        files = {'filedata': ('mymp3.mp3', finput, 'audio/mp3')}
        # BTW you can remove the ACCESS_TOKEN from fb_url and pass it as params instead
        params = {'access_token': 'ACCESS_TOKEN'}
        resp = requests.post(fb_url, params=params, data=data, files=files)
    
    print r.text
    

    【讨论】:

    • 我收到此错误:{"error":{"message":"(#100) 消息不能为空,必须提供有效的附件或文本","type":"OAuthException" ,"code":100,"error_subcode":2018034,"fbtrace_id":"HTjHVPM0grB"}}
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-04
    • 2017-12-05
    • 1970-01-01
    • 2016-08-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多