Bob*_*ger 5 facebook facebook-graph-api botframework facebook-messenger-bot
语境:
BotFramework (C# SDK) + Messenger 频道,bot 处理两种类型的用户:与会者(Messenger 用户)和组织者(Facebook 主页的管理员)。
用例:
当与会者请求人工支持(使用我的机器人菜单中的选项)时,组织者将收到一条消息。
在该消息中,我想添加一个按钮,一旦组织者单击,该按钮将执行以下操作:
我做了什么:
我成功完成了停止自动回复的部分
我被困在如何将组织者重定向到 FB Page 收件箱中的正确对话
技术上:
当我在 Facebook 主页中查看时,似乎应该为我的操作生成的链接如下所示: https://www.facebook.com/mypage-mypageId/inbox/?selected_item_id=someId
我的问题是我无法selected_item_id从我的机器人对话中找到这个值。
借助 Facebook Graph API,您将能够获得 Facebook 页面收件箱的链接(使用正确的线程)。
/me/conversations必须调用以获取页面的对话(因此您必须向 API 调用提供页面的 access_token)。
然后,在这些结果中,您必须与与会者的对话进行匹配。为此,您可以使用机器人中的Activity属性id(Activity.Id,而不是 Activity.Conversation.Id!),因为该值在您的机器人和 Facebook 图表结果之间很常见(只需添加“m_”以匹配):您可以在message.idGraph API 结果中找到它(注意:不是对话.id)
然后您可以获取link您找到的此对话的 Graph API 结果的值:"link": "\/myBotName\/manager\/messages\/?threadid=10154736814928655&folder=inbox"在我的测试中
以下是一个对话框示例,它将搜索特定消息 ID 的链接:
[Serializable]
public class FacebookGetLinkTestDialog : IDialog<string>
{
public async Task StartAsync(IDialogContext context)
{
context.Wait(MessageReceivedAsync);
}
public async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> argument)
{
var jsonString = "";
var link = "";
using (var client = new HttpClient())
{
using (var response = await client.GetAsync($"https://graph.facebook.com/v2.9/me/conversations?access_token=yourAccessTokenHere").ConfigureAwait(false))
{
jsonString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var conversationList = Newtonsoft.Json.JsonConvert.DeserializeObject<ConversationsRootObject>(jsonString);
link = conversationList.data.Single(c => c.messages.data.Any(d => d.id.Equals("m_" + "yourActivityIdHere"))).link;
}
}
await context.PostAsync($"{link}");
}
}
public class ConversationsRootObject
{
public List<Conversation> data { get; set; }
public Paging paging { get; set; }
}
public class Conversation
{
public string id { get; set; }
public string snippet { get; set; }
public string updated_time { get; set; }
public int message_count { get; set; }
public int unread_count { get; set; }
public Participants participants { get; set; }
public FormerParticipants former_participants { get; set; }
public Senders senders { get; set; }
public Messages messages { get; set; }
public bool can_reply { get; set; }
public bool is_subscribed { get; set; }
public string link { get; set; }
}
public class Participant
{
public string name { get; set; }
public string email { get; set; }
public string id { get; set; }
}
public class Participants
{
public List<Participant> data { get; set; }
}
public class FormerParticipants
{
public List<object> data { get; set; }
}
public class Senders
{
public List<Participant> data { get; set; }
}
public class Messages
{
public List<FbMessage> data { get; set; }
public Paging paging { get; set; }
}
public class FbMessage
{
public string id { get; set; }
public string created_time { get; set; }
}
public class Cursors
{
public string before { get; set; }
public string after { get; set; }
}
public class Paging
{
public Cursors cursors { get; set; }
public string next { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2097 次 |
| 最近记录: |