1
votes

I want to init some data for facebook user base on link they use to access my bot. I found this article: https://developers.facebook.com/docs/messenger-platform/referral-params but I dont know how to get referral params from bot framework (C#). My purpose to set user data base on link like this: https://m.me/{BOT_ID}?ref={SOME_MESSAGE}

Any help will be appreciated.

Thanks

1

1 Answers

2
votes

(also replying here after the issue tracker on github to share my solution)

I successfully tried to do the same. Weird point first: note that the value is not at the same place if you previously talk to the bot or not...

So I'm doing the following:

  • defined a FacebookChannelData class, where RefParameter is the parameter you want, in order to get these fields from the json message received

Code:

public class FacebookChannelData
{
    [JsonProperty("Sender")]
    public Sender Sender { get; set; }

    [JsonProperty("Recipient")]
    public Recipient Recipient { get; set; }

    [JsonProperty("Timestamp")]
    public long Timestamp { get; set; }

    [JsonProperty("Postback")]
    public Postback Postback { get; set; }

    [JsonProperty("Referral")]
    public Referral Referral { get; set; }

    public string RefParameter
    {
        get
        {
            string val = "";

            if (Postback != null && Postback.Referral != null && !String.IsNullOrWhiteSpace(Postback.Referral.Reference))
            {
                val = Postback.Referral.Reference;
            }
            else if (Referral != null && !String.IsNullOrWhiteSpace(Referral.Reference))
            {
                val = Referral.Reference;
            }
            return val;
        }
    }
}

public class Recipient
{
    [JsonProperty("Id")]
    public string Id { get; set; }
    [JsonProperty("Name")]
    public string Name { get; set; }
}

public class Sender
{
    [JsonProperty("Id")]
    public string Id { get; set; }

    [JsonProperty("Name")]
    public string Name { get; set; }
}

public class Postback
{
    [JsonProperty("Payload")]
    public string Payload { get; set; }

    [JsonProperty("Referral")]
    public Referral Referral { get; set; }
}

    public class Referral
    {
        [JsonProperty("Ref")]
        public string Reference { get; set; }

        [JsonProperty("Source")]
        public string Source { get; set; }

        [JsonProperty("Type")]
        public string Type { get; set; }
}
  • so getting the ChannelData of the facebook incoming message is like:

Code:

if (incomingMessage.TryGetChannelData(out FacebookChannelData channelDataInfo))
{
    return channelDataInfo.RefParameter;
}
else
{
    return String.Empty;
}

As you can see, the interesting value is the field "Postback.Referral.Reference" OR "Referral.Reference" in this ChannelData (given the fact that it is a new conversation or not), so I'm checking if the 1st one is null and trying to get the 2nd one in that case

Hope it will be helpful.

PS : don't forget to set a Start Button on your Messenger bot settings.