What is Scope, and why is it important? | Binary Computer Solutions

What is Scope, and why is it important?

Blaine's picture

What is Scope, and why is it important?

There has been a lot of chatter lately about “Chat Bots”, 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!

“scope”
/skōp/
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-referenc...) “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-referen...) 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!

Disclaimer: 
The statements of the author of this blog are the opinion of the author, and do not necessarily reflect the views or opinions of Binary Computer Solutions, Inc