首页 文章

有关如何从与会者和机器人之间的对话中获取此ID的任何想法?

提问于
浏览
3

Context:

BotFramework(C#SDK)Messenger通道,bot处理两种类型的用户:与会者(Messenger用户)和组织者(Facebook页面的管理员) .

Use case:

当与会者请求人工支持时(使用我的机器人菜单中的选项),组织者将收到一条消息 .

在该消息中,我想添加一个按钮,一旦被组织者点击,它将执行以下操作:

  • 停止机器人对该用户的自动回复

  • 将组织者重定向到Facebook的页面收件箱,并选择对话(在与会者和机器人之间)

What I have done:

  • 我成功完成了停止自动回复的部分

  • 我被困在如何将组织者重定向到FB Page收件箱中的正确对话

Technically:

当我在Facebook页面中查看时,我应该为我的操作生成的链接如下所示: https://www.facebook.com/mypage-mypageId/inbox/?selected_item_id=someId

我的问题是我从机器人的谈话中找不到 selected_item_id 的这个值 .

1 回答

  • 2

    借助Facebook Graph API,您将能够获得Facebook页面收件箱的链接(使用正确的主题) .

    必须调用 /me/conversations 来获取页面的对话(因此您必须为API调用提供页面的access_token) .

    然后在这些结果中,您必须与与会者的对话进行匹配 . 要做到这一点,你可以在机器人中使用 property id of the Activity (Activity.Id,而不是Activity.Conversation.Id!),因为这个值在你的机器人和facebook图形结果之间是常见的(只需要添加"m_"来匹配):你可以找到它在你的Graph API结果中的一个 message.id (小心:不是conversation.id)

    然后,您可以获得您在此测试中找到的此对话的Graph API结果的 link 值: "link": "\/myBotName\/manager\/messages\/?threadid=10154736814928655&folder=inbox" 在我的测试中

    以下是一个Dialog示例,它将搜索特定消息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; }
    }
    

相关问题