botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

facebook referral params

Open bmperrea opened this issue 9 years ago • 10 comments

A newer feature of the facebook send api is to have referral parameters that can help qualify a user when they arrive at the bot. Currently the payload is either (1) attached to the get started button, or (2) sent as a messaging_referral event that the webhook must subscribe to.

I can receive the payload in case (1) [I'm using Node.js] in the session.message.sourcEvent. In case (2), the payload gets received if I am waiting for a message (as in a prompt). However, I have check for it manually in every waterfall step of my bot. It would be much more civil is there was a way to directly subscribe to the messaging_referral and to make an action using the referral.

https://developers.facebook.com/docs/messenger-platform/referral-params

bmperrea avatar Feb 16 '17 19:02 bmperrea

Would the send/session middleware help you intercept all messages?

willportnoy avatar Feb 17 '17 03:02 willportnoy

Thanks for the tags @willportnoy. I think so -- is there a link to something I am missing?

bmperrea avatar Feb 17 '17 15:02 bmperrea

@willportnoy 's comment lead me to try using bot.use. My remaining issue is that I would like to be able to pass the data to the corresponding session for that message, like in the following pseudocode, but the session is not available in UniversalBot.use. Does anyone have any ideas?

bot.use({
    receive: function (event, next) {
        if(event.sourceEvent.postback.referral.ref) {
	    session.userData.ref = event.sourceEvent.postback.referral.ref;   // won't work
        } else {
	     next();
        }
});

bmperrea avatar Feb 21 '17 23:02 bmperrea

This was quite easy in the end -- just needed to read the api for bot.use and middleware a little more closely. Here's my solution below. See also.

bot.use({
	botbuilder: function (session, next) {
	        var event = session.message;
		// check for referral
		if(event.sourceEvent.postback && event.sourceEvent.postback.referral)	{
			session.userData.referral = event.sourceEvent.postback.referral.ref;
		} else if (event.sourceEvent.referral) {
			session.userData.referral = event.sourceEvent.referral;
		}
		session.save();
		next();
       }
});

bmperrea avatar Feb 22 '17 06:02 bmperrea

How do we achieve this from a C# bot?

haligasd avatar Feb 25 '17 20:02 haligasd

@haligasd you can use IPostToBot as a type of receive middleware, or use IActivityLogger to inspect all activities.

willportnoy avatar Feb 28 '17 02:02 willportnoy

@bmperrea this solution crashes because on the emulator sourceEvent seems to be null :) just a headsup, you should probably check if(event && event.sourceEvent && event.sourceEvent.postback)

slidenerd avatar Mar 23 '17 16:03 slidenerd

@bmperrea Great solution thanks! However it only works for me for users that have interacted with the bot before. Not for totally new users??? Do you have the same issue?

Beuntj avatar Nov 16 '17 09:11 Beuntj

@willportnoy is it a good idea to use Scorables in C# instead of the IActivityLogger, and is there an example for "IPostToBot as a type of receive middleware"

etbuilder avatar Jan 24 '18 17:01 etbuilder

@etbuilder

LogPostToBot is registered with autofac as a keyed type: https://github.com/Microsoft/BotBuilder/blob/31048a2173313c81a2db47efce6a8a869b4ec284/CSharp/Library/Microsoft.Bot.Builder.Autofac/Dialogs/DialogModule.cs#L349

This enables supplying a custom IPostToBot:

Conversation.UpdateContainer(
    builder =>
    {
        builder.RegisterType<ActivityLogger>().AsImplementedInterfaces().InstancePerDependency();

        builder
            .RegisterType<InterceptLogPostToBot>()
            .Keyed<IPostToBot>(typeof(LogPostToBot));

    });

public class InterceptLogPostToBot: IPostToBot
{
    private readonly IPostToBot inner;
    private readonly IActivityLogger logger;
    public InterceptLogPostToBot(IPostToBot inner, IActivityLogger logger)
    {
        SetField.NotNull(out this.inner, nameof(inner), inner);
        SetField.NotNull(out this.logger, nameof(logger), logger);
    }

    async Task IPostToBot.PostAsync(IActivity activity, CancellationToken token)
    {
        //do something before calling the logger
        await this.logger.LogAsync(activity);
        await inner.PostAsync(activity, token);
    }
}

EricDahlvang avatar Jan 24 '18 18:01 EricDahlvang