可以';t通过REST Api Botframework向Facebook机器人发送消息

本文关键字:Facebook 机器人 消息 Botframework Api REST 通过 可以 | 更新日期: 2023-09-27 18:08:49


我想从一个c#应用程序向我的Facebook机器人发送一条简单的消息,该机器人是用Microsoft bot Framework创建的
使用Skype,这可以很好地工作,但当我尝试Messenger机器人程序时,我会收到以下请求错误:

{
   "message": "The 'form' field is unrecognized"
}

我正在使用以下活动发送消息:

{
"type": "message",
"id": "...",
"timestamp": "2016-09-24T02:47:03.8956722Z",
"serviceUrl": "https://facebook.botframework.com",
"channelId": "facebook",
"from": {
  "id": "...",
  "name": "..."
},
"conversation": {
  "id": "..."
},
"recipient": {
  "id": "...",
  "name": "..."
},
"text": "Hy, from remote!",
"channelData": {
  "sender": {
    "id": "..."
},
"recipient": {
  "id": "..."
},
"timestamp": 1474685223681,
"message": {
  "mid": "...",
  "seq": 35,
  "text": "Testtest"
}

}}


所以"from"字段实际上就在这里
当我删除"from"字段时,请求消息说它是必需的,所以它以某种方式识别该字段。也许只是格式不对
那么我该如何让它发挥作用呢?

可以';t通过REST Api Botframework向Facebook机器人发送消息

尝试用更少的参数构建消息
对于Facebook,出于某种原因,您需要from字段,但您不必提供所有其他参数。

尝试以下请求:

{
  "type": "message",
  "textFormat": "plain",
  "text": "Hello messenger!",
  "from":
  {
    "id": "your-id-from-recipient-id-in-the-message-received",
    "name": "your-name-from-recipient-name-in-the-message-received"
  }
}

准确使用用户发送消息时收到的id和名称非常重要。

数据可以从发送给机器人的类似消息中提取:

{
  "type": "message",
  "id": "<snip>",
  "timestamp": "2017-01-06T14:09:36.8882093Z",
  "serviceUrl": "https://facebook.botframework.com",
  "channelId": "facebook",
  "from": {
    "id": "<snip>",
    "name": "<snip>"
  },
  "conversation": {
    "isGroup": false,
    "id": "<snip>"
  },
  "recipient": {
    "id": "your-id-from-recipient-id-in-the-message-received",
    "name": "your-name-from-recipient-name-in-the-message-received"
  },
  <snip>
}