Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any idea on how to get this id from the conversation between attendee and bot?

Tags:

Context:

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

Use case:

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

In that message, I would like to add a button that will do the following once clicked by the organizer:

  1. stop the automatic replies from the bot to that user
  2. redirect the organizer to Facebook's Page inbox, with the conversation (between the attendee and the bot) selected

What I have done:

  • I successfully did the part to stop the automatic replies

  • I got stuck on how to redirect the organizer to the right conversation in FB Page's inbox

Technically:

When I'm looking in Facebook Page, the link that seems to be the one that I should generate for my action is like the following: https://www.facebook.com/mypage-mypageId/inbox/?selected_item_id=someId

My problem is that I can't find this value for selected_item_id from my bot's conversation.

like image 302
Bob Swager Avatar asked Jun 12 '17 14:06

Bob Swager


1 Answers

You will be able to get a link to the facebook page inbox (with the right thread) thanks to Facebook Graph API.

/me/conversations must be called to get the conversations of the Page (so you have to give an access_token of the page to the API call).

Then in those results, you have to make a match with the conversation of the attendee. To do this, you can use the property id of the Activity in your bot (Activity.Id, not Activity.Conversation.Id!) as this value is common between your bot and facebook graph results (just need to add "m_" to match): you can find it in one message.id on your Graph API results (careful: not conversation.id)

Then you can get the link value of the Graph API result for this conversation that you found: "link": "\/myBotName\/manager\/messages\/?threadid=10154736814928655&folder=inbox" in my test

Here is a sample of a Dialog that will search for the link for a specific message 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; }
}
like image 116
Nicolas R Avatar answered Sep 24 '22 23:09

Nicolas R