adamasan
adamasan

Reputation: 1182

Is it possible to detect when a user opens the chat window on Facebook?

I'm trying to create a chatbot where in order to avoid the user opening the chat window and not knowing the available options, I want to give some basic instructions when the user opens the chat window.

Is there any trigger available when the user opens a chat window? Maybe then I can check, and if there's not an ongoing conversation I could provide basic instructions.

I did some googling and found nothing about this. Is it possible to do something like this, and if not, is there a way to mitigate this problem, and provide the user with information regarding the chatbot capabilities and supported instructions?

Upvotes: 2

Views: 2125

Answers (3)

Raph Schryn
Raph Schryn

Reputation: 139

Facebook does not allow bots to initiate a conversation, unlike Skype or other platforms. There are still some tricks you can do :

  • Go on the Settings of your Facebook page, then Messaging and check "Show a Messenger Greeting" as below, and write your greeting sentence. enter image description here

The result will look like this :

enter image description here

Upvotes: 6

Sergey L
Sergey L

Reputation: 1492

You can monitor for two event types: ConversationUpdate and ContactRelationUpdate.

The first one (ConversationUpdate) is called when a user is added or removed from the conversation. So, there's a place where you can introduce available options. It will be each type the new conversation has started. So, it may become annoying, you may add a check - do not show it if the user has been using the bot for some time.

The second (ContactRelationUpdate) is called when a user adds or removes the bot to/from the contacts. In general, it is only called once per user action.

Here's the extract from the Bot-Frameworks examples:

For Node.Js

bot.on('conversationUpdate', function (message) {
   // Check for group conversations
    if (message.address.conversation.isGroup) {
        // Send a hello message when bot is added
        if (message.membersAdded) {
            message.membersAdded.forEach(function (identity) {
                if (identity.id === message.address.bot.id) {
                    var reply = new builder.Message()
                            .address(message.address)
                            .text("Hello everyone!");
                    bot.send(reply);
                }
            });
        }

        // Send a goodbye message when bot is removed
        if (message.membersRemoved) {
            message.membersRemoved.forEach(function (identity) {
                if (identity.id === message.address.bot.id) {
                    var reply = new builder.Message()
                        .address(message.address)
                        .text("Goodbye");
                    bot.send(reply);
                }
            });
        }
    }
});

bot.on('contactRelationUpdate', function (message) {
    if (message.action === 'add') {
        var name = message.user ? message.user.name : null;
        var reply = new builder.Message()
                .address(message.address)
                .text("Hello %s... Thanks for adding me. Say 'hello' to see some great demos.", name || 'there');
        bot.send(reply);
    } else {
        // delete their data
    }
});

For C#

private void HandleMessage(Activity message)
{
    if (message.Type == ActivityTypes.ConversationUpdate)
    {
                if (activity.MembersAdded.Any(m => m.Id == activity.Recipient.Id))
                {
                    var connector = new ConnectorClient(new Uri(activity.ServiceUrl));

                    var response = activity.CreateReply();
                    response.Text = "Hi! I am Bot. Here's what you can do...";

                    await connector.Conversations.ReplyToActivityAsync(response);
                }
    }
    else if (message.Type == ActivityTypes.ContactRelationUpdate)
    {
        if (Activity.AsContactRelationUpdateActivity().Action ==                ContactRelationUpdateActionTypes.Add)
        {
                    var connector = new ConnectorClient(new Uri(activity.ServiceUrl));

                    var response = activity.CreateReply();
                    response.Text = "Hi! I am Bot. Thanks for adding me. Here's what you can do...";

        }
    }
    return null;
}

Upvotes: 3

sGambolati
sGambolati

Reputation: 782

I think the acid answer is not.

But you can intercept the IConversationUpdateActivity type message to know if the user has added the bot to a conversation. In the C# project template you can find a code block that ask for this message type but do nothing.

Upvotes: 2

Related Questions