Facebook Messenger Bot,发送图片附件

时间:2016-04-17 05:31:50

标签: facebook bots messenger

我正在创建一个上传到图像的Facebook机器人,它会以图像作为响应。我可以将图像发送到附件中并将其从服务器上删除,还是必须将URL发送到图像并将图像保留在我的服务器上?

4 个答案:

答案 0 :(得分:4)

You can use their Upload API to upload your attachments to their servers.

curl -X POST -H "Content-Type: application/json" -d '{
  "message":{
    "attachment":{
      "type":"image", 
      "payload":{
        "url":"http://www.messenger-rocks.com/image.jpg", 
        "is_reusable":true,
      }
    }
  }
}' "https://graph.facebook.com/v2.6/me/message_attachments?access_token=PAGE_ACCESS_TOKEN"

The upload call will respond back an attachment_id which can be used to send the attachment to the user without uploading it again.

curl -X POST -H "Content-Type: application/json" -d '{
  "recipient": {
    "id": "USER_ID"
  },
  "message": {
    "attachment": {
      "type": "image",
      "payload": {
        "attachment_id": "1745504518999123"
      }
    }
  }
}' "https://graph.facebook.com/me/messages?access_token=PAGE_ACCESS_TOKEN"   

答案 1 :(得分:2)

    curl  \
  -F recipient='{"id":"USER_ID"}' \
  -F message='{"attachment":{"type":"image", "payload":{}}}' \
  -F filedata=@/tmp/testpng.png \
  "https://graph.facebook.com/v2.6/me/messages?access_token=PAGE_ACCESS_TOKEN"

Here you can find an official example on how to atach a jpg or a png. https://developers.facebook.com/docs/messenger-platform/send-api-reference#examples

答案 2 :(得分:1)


我有同样的问题,但仍然没有找到答案。
目前,使用Facebook bot发送图像的唯一方法是使用您的图片网址“image_url”。

答案 3 :(得分:0)

您可以通过Facebook Messenger API发送附件,方法是使用分段请求直接过帐它们(不使用payload.url选项)。这就是PHP和Guzzle的用途(尽管不管脚本语言如何,任何良好的HTTP Request包都应该这样做):

use GuzzleHttp\Client;

$client = new Client();
$graphRequest = $client->request('POST', 'https://graph.facebook.com/v5.0/me/messages', [
    'query' => [
        'access_token' => $facebookInfo['pageAccessToken']
    ],
    'multipart' => [
        [
            'name' => 'messaging_type',
            'contents' => 'RESPONSE',
        ],
        [
            'name' => 'recipient',
            'contents' => json_encode(['id' => $yourRecipientPSID]),
        ],
        [
            'name' => 'message',
            'contents' => json_encode(['attachment' => ['type' => 'file', 'payload' => []]]),
        ],
        [
            'name' => 'filedata',
            'contents' => fopen($yourFilePath, 'r'),
            'filename' => $yourFileName,
        ],
    ],
]);

请注意,设置了attachments.payload参数,但设置了null。如果未设置payload,则Graph API返回400响应。