def get_user_profile(self, fb_user_id): r = Api.get('https://graph.facebook.com/v2.6/%s' % fb_user_id, params={'access_token': self.access_token}, headers=HEADERS) if r.status_code != OK: log.error(r.text) return return json.loads(r.text)
def _send_profile_settings(self, data, method='post'): r = Api.route(method, 'https://graph.facebook.com/v2.6/me/messenger_profile', params={'access_token': self.access_token}, data=data, headers=HEADERS) if r.status_code != OK: log.error(r.text) log.info(r.text)
def _fetch_page_info(self): r = Api.get('https://graph.facebook.com/v2.6/me', params={'access_token': self.access_token}, headers=HEADERS) if r.status_code != OK: log.error(r.text) return data = json.loads(r.text) if 'id' not in data or 'name' not in data: raise ValueError('Could not fetch data : GET /v2.6/me') self._page_id = data['id'] self._page_name = data['name']
def _send(self, payload, callback=None): assert type(payload) == dict, 'tipe payload harus dict' if type(payload) == object: payload = to_json(payload) log.info(payload) r = Api.route('post', 'https://graph.facebook.com/v2.6/me/messages', params={'access_token': self.access_token}, data=json.dumps(payload), headers=HEADERS) if r.status_code != OK: log.error(r.text) return if callback is not None: callback(payload, r) return json.loads(r.text)
def handle_webhook(self, payload, message_callback=None, postback_callback=None): _data = payload if type(payload) is not object and isinstance(payload, str): _data = json.loads(payload) # Memastikan Page sudah di subscribe if _data.get('object') != 'page': log.error('Webhook failed, only support page subscription') return False # men-routing tipe pesan yang datang dari webhook def get_events(_data_): for entry in _data_.get("entry"): for messaging in entry.get("messaging"): event = Event(messaging) yield event for event in get_events(_data): if event.is_message and not event.is_echo and not event.is_quick_reply: if message_callback is None: log.error('message_callback is None') continue message_callback(event) elif event.is_postback: if postback_callback is None: log.error('postback_callback is None') continue postback_callback(event) else: log.error('Webhook received unknown messagingEvent:', event)