Created
November 18, 2019 16:09
-
-
Save robertvunabandi/5a06f1d278f23c059228f87e73ae9bae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const INFO = { | |
APP_ID: "MY-APP-ID-HERE", | |
isMessenger: null, | |
thread_type: null, | |
tid: null, | |
psid: null, | |
}; | |
/** | |
* Thread types returned when getting the context | |
* @enum {String} | |
*/ | |
const ThreadType_T = { | |
USER_TO_PAGE: "USER_TO_PAGE", | |
USER_TO_USER: "USER_TO_USER", | |
GROUP: "GROUP", | |
}; | |
/** | |
* @typedef {{thread_type: ThreadType_T, tid: String, psid: String, signed_request: String}} | |
*/ | |
let ThreadContext_T; | |
// set up the Messenger Chat Extension SDK | |
(function(d, s, id){ | |
let js, fjs = d.getElementsByTagName(s)[0]; | |
if (d.getElementById(id)) {return;} | |
js = d.createElement(s); js.id = id; | |
js.src = "//connect.facebook.net/en_US/messenger.Extensions.js"; | |
fjs.parentNode.insertBefore(js, fjs); | |
}(document, 'script', 'Messenger')); | |
window.extAsyncInit = function() { | |
// The Messenger Extensions JS SDK is done loading, call setup. | |
setUp(); | |
}; | |
/** | |
* Sets up whatever is needed to run Sunny. | |
*/ | |
function setUp() { | |
INFO.isMessenger = window.name === "messenger_ref"; | |
if (!INFO.isMessenger) { | |
// Check that if it's not messenger, it's Facebook. | |
console.log("should be true (isFacebook):", window.name === "facebook_ref"); | |
} | |
// retrieve the context and figure out what to do about it | |
MessengerExtensions.getContext( | |
INFO.APP_ID, | |
function success(thread_context) { | |
const {thread_type, tid, psid} = thread_context; | |
// Update info before handling the thread based on the thread type. | |
INFO.thread_type = thread_type; | |
INFO.tid = tid; | |
INFO.psid = psid; | |
switch (thread_type) { | |
case ThreadType_T.USER_TO_PAGE: | |
handleUserToPageThreadContext(thread_context); | |
break; | |
case ThreadType_T.USER_TO_USER: | |
handleUserToUserThreadContext(thread_context); | |
break; | |
case ThreadType_T.GROUP: | |
handleUserToGroupThreadContext(thread_context); | |
break; | |
default: | |
handleUnknownThreadContext(thread_context); | |
} | |
}, | |
function failure(error) { | |
console.error("The following error happened:"); | |
console.error(error); | |
}, | |
); | |
} | |
/** | |
* @param {ThreadContext_T} thread_context | |
*/ | |
function handleUserToPageThreadContext(thread_context) { | |
const {thread_type, tid, psid, signed_request} = thread_context; | |
// TODO: This should explain how to use Sunny... It should explain that | |
// one needs to sign up as a group. | |
} | |
/** | |
* @param {ThreadContext_T} thread_context | |
*/ | |
function handleUserToUserThreadContext(thread_context) { | |
const {thread_type, tid, psid, signed_request} = thread_context; | |
// TODO: | |
} | |
/** | |
* @param {ThreadContext_T} thread_context | |
*/ | |
function handleUserToGroupThreadContext(thread_context) { | |
const {thread_type, tid, psid, signed_request} = thread_context; | |
// TODO: | |
} | |
/** | |
* @param {ThreadContext_T} thread_context | |
*/ | |
function handleUnknownThreadContext(thread_context) { | |
const {thread_type, tid, psid, signed_request} = thread_context; | |
// TODO: | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment