Beispiel #1
0
    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.')
Beispiel #2
0
 def set_menu(self, menu_data):
     """
     https://developers.facebook.com/docs/messenger-platform/messenger-profile/persistent-menu has a detailed documentation
     on the menu data and formats.
     :param menu_data:
     :return:
     """
     request = base.exec_request('POST', self.graphAPIURL, data=menu_data)
     if request:
         return request
Beispiel #3
0
 def set_menu(self, menu_data):
     """
     https://developers.facebook.com/docs/messenger-platform/messenger-profile/persistent-menu has a detailed documentation
     on the menu data and formats.
     :param menu_data:
     :return:
     """
     request = base.exec_request('POST', self.graphAPIURL, data=menu_data)
     if request:
         return request
Beispiel #4
0
    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.')
Beispiel #5
0
    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.')
Beispiel #6
0
def update_white_listed_domains():
    """https://graph.facebook.com/v2.6/me/messenger_profile?access_token=PAGE_ACCESS_TOKEN"""
    graph_api_url = FBConfig.GRAPH_API_URL.replace('messages', 'messenger_profile')
    data = {
        "setting_type": "domain_whitelisting",
        "whitelisted_domains": FBConfig.WHITE_LISTED_DOMAINS,
        "domain_action_type": "add"
            }
    try:
        request = base.exec_request('POST', graph_api_url, data=data)
        return request
    except HttpMethodError:
        return 'Error'
Beispiel #7
0
def update_white_listed_domains():
    """https://graph.facebook.com/v2.6/me/messenger_profile?access_token=PAGE_ACCESS_TOKEN"""
    graph_api_url = FBConfig.GRAPH_API_URL.replace('messages',
                                                   'messenger_profile')
    data = {
        "setting_type": "domain_whitelisting",
        "whitelisted_domains": FBConfig.WHITE_LISTED_DOMAINS,
        "domain_action_type": "add"
    }
    try:
        request = base.exec_request('POST', graph_api_url, data=data)
        return request
    except HttpMethodError:
        return 'Error'
Beispiel #8
0
    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.')
Beispiel #9
0
    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.')
Beispiel #10
0
    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.')
Beispiel #11
0
    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.')
Beispiel #12
0
 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.')
Beispiel #13
0
 def get_menu(self):
     request = base.exec_request('GET', self.graphAPIURLGET)
     if request:
         return request
Beispiel #14
0
 def get_text(self):
     request = base.exec_request('GET', self.graphAPIURLGET)
     if request:
         return request
     else:
         raise HttpError('Unable to complete request.')
Beispiel #15
0
 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.')
Beispiel #16
0
 def delete_message(self):
     request = base.exec_request('DELETE', self.graphAPIURLGET)
     if request:
         return request
     else:
         raise HttpError('Unable to complete request.')
Beispiel #17
0
 def delete_menu(self):
     request = base.exec_request('DELETE', self.graphAPIURLGET)
     if request:
         return request
Beispiel #18
0
 def get_menu(self):
     request = base.exec_request('GET', self.graphAPIURLGET)
     if request:
         return request
Beispiel #19
0
 def delete_menu(self):
     request = base.exec_request('DELETE', self.graphAPIURLGET)
     if request:
         return request
Beispiel #20
0
 def get_text(self):
     request = base.exec_request('GET', self.graphAPIURLGET)
     if request:
         return request
     else:
         raise HttpError('Unable to complete request.')
Beispiel #21
0
 def delete_message(self):
         request = base.exec_request('DELETE', self.graphAPIURLGET)
         if request:
             return request
         else:
             raise HttpError('Unable to complete request.')