def __init__(self, argument_parser=None): FlaskRestBotClient.__init__(self, 'facebook', argument_parser) YLogger.debug(self, "Facebook Client is running....") self._facebook_bot = self.create_facebook_bot() self._renderer = FacebookRenderer(self) print("Facebook Client loaded")
def get_default_renderer(self): return FacebookRenderer(self)
def __init__(self, client): FacebookRenderer.__init__(self, client) self._payload = None
def get_default_renderer(self, callback=True): return FacebookRenderer(self)
class FacebookBotClient(FlaskRestBotClient): def __init__(self, argument_parser=None): FlaskRestBotClient.__init__(self, 'facebook', argument_parser) YLogger.debug(self, "Facebook Client is running....") self._facebook_bot = self.create_facebook_bot() self._renderer = FacebookRenderer(self) print("Facebook Client loaded") @property def facebook_bot(self): return self._facebook_bot def get_description(self): return 'ProgramY AIML2.0 Facebook Client' def get_license_keys(self): self._access_token = self.license_keys.get_key("FACEBOOK_ACCESS_TOKEN") self._verify_token = self.license_keys.get_key("FACEBOOK_VERIFY_TOKEN") def get_client_configuration(self): return FacebookConfiguration() def create_facebook_bot(self): return Bot(self._access_token) def get_hub_challenge(self, request): return request.args.get("hub.challenge") def get_hub_verify_token(self, request): return request.args.get("hub.verify_token") def verify_fb_token(self, token_sent, request): # take token sent by facebook and verify it matches the verify token you sent # if they match, allow the request, else return an error if token_sent is None: YLogger.error(self, "Verify Token is None!") if token_sent == self._verify_token: return self.get_hub_challenge(request) YLogger.error(self, "Facebook verify token failed received [%s]", token_sent) return 'Invalid verification token' def return_hub_challenge(self, request): """Before allowing people to message your bot, Facebook has implemented a verify token that confirms all requests that your bot receives came from Facebook.""" token_sent = self.get_hub_verify_token(request) return self.verify_fb_token(token_sent, request) def render_response(self, client_context, response): # sends user the text message provided via input response parameter self._renderer.render(client_context, response) return "success" def receive_message(self, request): if request.method == 'GET': return self.return_hub_challenge(request) else: data = request.get_json() self.process_facebook_request(data) return "Message Processed" def get_recipitent_id(self, message): if 'sender' in message: if 'id' in message['sender']: return message['sender']['id'] return None def get_message_text(self, message): if 'message' in message: return message['message'].get('text') return None def get_postback_text(self, message): if 'postback' in message: return message['postback'].get('payload') return None def has_attachements(self, message): if 'message' in message: if message['message'].get('attachments') is not None: return True return False def process_facebook_request(self, request): if 'entry' in request: for event in request['entry']: if 'messaging' in event: messaging = event['messaging'] self.process_facebook_message(messaging) def process_facebook_message(self, messaging): for message in messaging: if message.get('message'): self.handle_message(message) elif message.get('postback'): self.handle_postback(message) def ask_question(self, client_context, question): response = "" try: response = client_context.bot.ask_question(client_context, question, responselogger=self) except Exception as e: print(e) return response def handle_message(self, message): try: if self.configuration.client_configuration.debug is True: self.dump_request(message) # Facebook Messenger ID for user so we know where to send response back to recipient_id = self.get_recipitent_id(message) if recipient_id: client_context = self.create_client_context(recipient_id) message_text = self.get_message_text(message) # We have been send a text message, we can respond if message_text is not None: YLogger.debug("Facebook sent message: [%s]", message_text) response_text = self.ask_question(client_context, message_text) # else if user sends us a GIF, photo,video, or any other non-text item elif self.has_attachements(message): YLogger.error("Facebook client cannot handle attachments at this time") response_text = "Sorry, I cannot handle attachements right now!" # otherwise its a general error else: YLogger.error("Facebook general error handling message") response_text = "Sorry, I do not understand you!" YLogger.debug("Facebook message response: [%s]", response_text) self.render_response(client_context, response_text) except Exception as e: import traceback traceback.print_exc() def handle_postback(self, message): if self.configuration.client_configuration.debug is True: self.dump_request(message) # Facebook Messenger ID for user so we know where to send response back to recipient_id = self.get_recipitent_id(message) if recipient_id: client_context = self.create_client_context(recipient_id) message_text = self.get_postback_text(message) # We have been send a text message, we can respond if message_text is not None: YLogger.debug("Facebook sent postback: [%s]", message_text) response_text = self.ask_question(client_context, message_text) # otherwise its a general error else: YLogger.error("Facebook general error handling postback") response_text = "Sorry, I do not understand you!" YLogger.debug("Facebook postback response: [%s]", response_text) self.render_response(client_context, response_text)