5

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.

Nicolas R
  • 13,812
  • 2
  • 28
  • 57
Bob Swager
  • 884
  • 9
  • 25
  • Where in your code are you sending the message to the agent? What exactly is the "selected_item_id"? You ask: "how to get select_item_id" from initial conversation" but I have no context to understand what selected_item_id actually is. Please explain. – Eric Dahlvang Jun 13 '17 at 02:33
  • I edited my question. Hope it is more clear now :) – Bob Swager Jun 13 '17 at 10:15

1 Answers1

4

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; }
}
Nicolas R
  • 13,812
  • 2
  • 28
  • 57
  • Should i use some conversion between ids ? Because they aren't the same. The id i receive from graph looks like this : `t_mid.someNumber:9b0f112e52` The id from acitivity (activity.Id) : `mid.$cAAP8wkY2a91i10BEQ1cpfr5erOvg` – Bob Swager Jun 14 '17 at 09:58
  • You are not looking in the right field in Graph API. The value you provide is an `id` of a Graph `conversation`(top level), here the link we are trying to do is with the `id` of a `message` (lower level) – Nicolas R Jun 14 '17 at 10:00
  • BTW : This works also with context.Activity.Id. v3.8 – Bob Swager Jun 14 '17 at 10:54
  • Where do you find the activity (or activity.id) of the bot? I am not using the BotFramework – echan00 Oct 19 '17 at 19:40
  • 1
    /me/conversations is now deprecated and is uncallable by applications using v2.5+ facebook API. – Phillip Whelan May 08 '18 at 16:58
  • You can list the conversations of a Facebook page using https://developers.facebook.com/docs/graph-api/reference/page/conversations/ , then with the `id` fields of the conversation you can query the messages – Nicolas R Mar 05 '19 at 13:32