def send_message(self, message_type, message_text=None, attachment=None, notification_type=None, quick_replies=None): """ - text must be UTF-8 and has a 640 character limit - You cannot send a text and an attachment together :param quick_replies: a list of quick responses sent along with the message to the user :param message_type: text or attachment :param message_text: text to send :param attachment: a valid attachment object i.e dictionary :param notification_type: REGULAR, SILENT_PUSH, or NO_PUSH :return: json response object """ notification_type = notification_type quick_replies = quick_replies if message_type == "text": self.payload_structure['message']['text'] = message_text try: self.payload_structure['message'].pop('attachment') except KeyError: pass else: try: self.payload_structure['message'].pop('text') except KeyError: pass self.payload_structure['message']['attachment'] = attachment # clean up payload try: self.payload_structure.pop('sender_action') except KeyError: pass if quick_replies: self.payload_structure['message']['quick_replies'] = quick_replies else: try: self.payload_structure['message'].pop('quick_replies') except KeyError: pass if notification_type: self.payload_structure['notification_type'] = notification_type else: try: self.payload_structure.pop('notification_type') except KeyError: pass # connect request = base.exec_request('POST', graphAPIURL, data=self.payload_structure) if request: return request else: raise HttpError('Unable to complete request.')
def get_user_details(user_id): """ :param user_id: accepts page scope user id (PSID) :return: json representation of the profile """ para = '?fields=first_name,last_name,profile_pic,locale,timezone,gender&' graph_api_url = FBConfig.GRAPH_API_URL.replace('me/messages?', (user_id + para)) request = base.exec_request('GET', graph_api_url) if request: return json.loads(request.decode(encoding='UTF-8')) else: raise HttpError('Unable to complete request.')
def send_template_message(self, template_type, **kwargs): self.payload_structure["message"]["attachment"]["payload"][ "template_type"] = template_type if template_type == "button": self.payload_structure['message']["attachment"]["payload"][ "text"] = kwargs.get('text') self.payload_structure['message']['attachment']['payload'].pop( 'elements') elif template_type == 'generic': self.payload_structure['message']["attachment"]["payload"][ 'elements'][0] = kwargs.get('generic_info') elif template_type == 'list': self.payload_structure['message']["attachment"]["payload"][ 'elements'] = kwargs.get('list_info') self.payload_structure['message']['attachment']['payload'].pop( 'text') if kwargs.get("buttons"): self.payload_structure['message']["attachment"]["payload"][ 'buttons'] = [kwargs.get('buttons')] else: try: self.payload_structure.pop('buttons') except KeyError: pass # clean up payload self.payload_structure.pop('sender_action') notification_type = kwargs.get('notification_type') if notification_type: self.payload_structure['notification_type'] = notification_type else: self.payload_structure.pop('notification_type') quick_replies = kwargs.get('notification_type') if quick_replies: self.payload_structure['quick_replies'] = quick_replies else: self.payload_structure['message'].pop('quick_replies') self.payload_structure['message'].pop('text') print(self.payload_structure) request = base.exec_request('POST', graphAPIURL, data=self.payload_structure) if request: return request else: raise HttpError('Unable to complete request.')
def send_action(self, action): """ :param action: - typing_on, typing_off, mark_as_read """ # clean up payload # self.payload_structure.pop('message') self.payload_structure.pop('notification_type') self.payload_structure['sender_action'] = action # connect request = base.exec_request('POST', graphAPIURL, data=self.payload_structure) if request: return request else: raise HttpError('Unable to complete request.')
def get_text(self): request = base.exec_request('GET', self.graphAPIURLGET) if request: return request else: raise HttpError('Unable to complete request.')
def set_text(self, payload): request = base.exec_request('POST', self.graphAPIURL, data=payload) if request: print(request) else: raise HttpError('Unable to complete request.')
def delete_message(self): request = base.exec_request('DELETE', self.graphAPIURLGET) if request: return request else: raise HttpError('Unable to complete request.')