Any idea on how to get this ID from a conversation between a participant and a bot?

Context:

BotFramework (C # SDK) + Messenger channel, bot handles two types of users: contributors (Messenger users) and organizers (who are Facebook page admins).

Use case:

When a participant requests human support (using an option in my bot menu), the organizer will receive a message.

In this post, I would like to add a button that will do the following after the organizer is clicked:

  • stop automatic replies from bot to user
  • redirect the host to the Facebook inbox, with the selected conversation (between visitor and bot)

What I've done:

  • I followed this part successfully to stop automatic replies

  • I am stuck on how to redirect the host to the correct conversation in the FB Inbox

Technically:

When I look at the Facebook page, the link that seems to be the one I have to create for my action looks like this: https://www.facebook.com/mypage-mypageId/inbox/?selected_item_id=someId

My problem is that I cannot find this value for selected_item_id

from my bot dialog.

+3


source to share


1 answer


You can get the link to your facebook inbox (with the flow you want) thanks to the Facebook Graph API.

/me/conversations

must be called to get pagechains (so you need to provide the page's access_token for the API call).

Then in these results you have to make a match with the participant's conversation. To do this, you can use the Activity property in your bot (Activity.Id, not Activity.Conversation.Id!), Since this value is shared between your bot and facebook results. (just need to add "m_" to match): you can find it in one in your graphical API results (careful: not chat.id) id

message.id



Then you can get the link

Graph API result value for that conversation that you found: "link": "\/myBotName\/manager\/messages\/?threadid=10154736814928655&folder=inbox"

in my test

Here's an example of a dialog that will look for a link for a specific post 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; }
}

      

+2


source







All Articles