What is Scope, and why is it important?

Blaine's picture
Scope, in terms of a chatbot specifically on Facebook, is important for many reasons, including user_id handling. Without understanding scope, things break.

Scope - as it pertains to Facebook Chatbots - and general programming scope concepts

There has been a lot of chatter lately about “Chatbots”, especially with the new wave of Facebook Messenger bots coming to market. While this is a very cool thing happening, and has even been coined “The next gold rush of Silicon Valley”, I’m still surprised how many people are missing the very basics which have been explained umpteen million times, and is even included in the very basic docs.

So today, I thought I would spell it out for those that are missing the concept of “scope”. First off, what does it mean when someone refers to something being in or out of scope? Glad you asked!

noun: scope; plural noun: scopes
The extent of the area or subject matter that something deals with or to which it is relevant.
"We widened the scope of our investigation"
synonyms:extent, range, breadth, width, reach, sweep, purview, span, horizon

So what does all that rigmarole mean? It means that the context in which you are dealing with something stays associated within the context you are set. Think of it like programming.

$SomeVariable = “foo”;
function doSomethingCool() {
$SomeVariable = “bar”;
$result = doSomethingCool();
echo $SomeVariable;

In this case, is the variable “foo” or “bar”? Well, since the function has a scope, and the variable is changed inside the function, it is “bar”; but only inside the function. When echoed outside the function, it is “foo” because it does not have the same scope as the function which changed it to “bar”. Sure, there are ways you can make it so stuff inside the function is passed back (such as returning a variable, or making it global), but for the purpose of the explanation here, the scope is limited to inside the function.

So coming full circle to our chat bots on Facebook Messenger, why am I even talking about scope? There have been a bunch of people trying to figure out why they can’t get a Facebook user_id working inside their chat bot.
For example: “I know the ID of my friend, but my chat bot can’t send them a message – why not?”

The quick and easy answer is the ID you are using is not the scoped ID given to the chat bot. As per Facebook documentation (see: https://developers.facebook.com/docs/messenger-platform/webhook-reference#user_ids) “All user ids returned in callbacks are page-scoped ids. This means that the ids are unique for a given page.” So in short, unless they messaged your chat bot, you will not be able to push a message to them. The ID given is associated with the page which they contacted the chat bot.

So when you try to get a User Profile using the code provided again by Facebook documentation (see: https://developers.facebook.com/docs/messenger-platform/send-api-reference#user_profile_request) and can’t seem to get relevant information, it’s because the is the scoped ID sent to your Webhook by Facebook, and is unique to your page. There is a lot of useful information you can pull from the scoped ID, though. First and last name, a gender, the timezone, even locale and profile picture URL are all provided. That information is only provided if the user sends a message to your chat bot, though. Grabbing a Facebook id for a user that was not sent via the Webhook will fail every time.

So remember kids, Scope is important!

Recent Posts

Have you ever wanted to disable offloading of all your Xen DomU's? How about doing it without having to think... Read more
Video Website Themes It is essential to know how big the video market is. You have a well-operating platform with... Read more
How to create a marketing video that will stand above the rest The business world seems to be advancing every... Read more

Need Support?

Every video, every website, every chatbot, every client - 100% custom products and solutions to make your business run successfully

145 Pine Haven Shores Road #1205 Shelburne, VT

Socialize with us


Go to top