コード例 #1
0
    def send(self, channel, msg, text):
        twitter = TembaTwython.from_channel(channel)
        start = time.time()

        try:
            urn = getattr(msg, "urn", URN.from_twitter(msg.urn_path))
            (scheme, path, query, display) = URN.to_parts(urn)

            # this is a legacy URN (no display), the path is our screen name
            if scheme == TWITTER_SCHEME:
                dm = twitter.send_direct_message(screen_name=path, text=text)
                external_id = dm["id"]

            # this is a new twitterid URN, our path is our user id
            else:
                metadata = msg.metadata if hasattr(msg, "metadata") else {}
                quick_replies = metadata.get("quick_replies", [])
                formatted_replies = [dict(label=item[: self.quick_reply_text_size]) for item in quick_replies]

                if quick_replies:
                    params = {
                        "event": {
                            "type": "message_create",
                            "message_create": {
                                "target": {"recipient_id": path},
                                "message_data": {
                                    "text": text,
                                    "quick_reply": {"type": "options", "options": formatted_replies},
                                },
                            },
                        }
                    }
                    dm = twitter.post("direct_messages/events/new", params=params)
                    external_id = dm["event"]["id"]
                else:
                    dm = twitter.send_direct_message(user_id=path, text=text)
                    external_id = dm["id"]

        except Exception as e:
            error_code = getattr(e, "error_code", 400)
            fatal = False

            if error_code == 404:  # handle doesn't exist
                fatal = True
            elif error_code == 403:
                for err in self.FATAL_403S:
                    if str(e).find(err) >= 0:
                        fatal = True
                        break

            # if message can never be sent, stop them contact
            if fatal:
                contact = Contact.objects.get(id=msg.contact)
                contact.stop(contact.modified_by)

            raise SendException(str(e), events=twitter.events, fatal=fatal, start=start)

        Channel.success(channel, msg, WIRED, start, events=twitter.events, external_id=external_id)
コード例 #2
0
    def send(self, channel, msg, text):
        twitter = TembaTwython.from_channel(channel)
        start = time.time()

        try:
            urn = getattr(msg, 'urn', URN.from_twitter(msg.urn_path))
            (scheme, path, display) = URN.to_parts(urn)

            # this is a legacy URN (no display), the path is our screen name
            if scheme == TWITTER_SCHEME:
                dm = twitter.send_direct_message(screen_name=path, text=text)

            # this is a new twitterid URN, our path is our user id
            else:
                dm = twitter.send_direct_message(user_id=path, text=text)

        except Exception as e:
            error_code = getattr(e, 'error_code', 400)
            fatal = False

            if error_code == 404:  # handle doesn't exist
                fatal = True
            elif error_code == 403:
                for err in self.FATAL_403S:
                    if six.text_type(e).find(err) >= 0:
                        fatal = True
                        break

            # if message can never be sent, stop them contact
            if fatal:
                contact = Contact.objects.get(id=msg.contact)
                contact.stop(contact.modified_by)

            raise SendException(str(e),
                                events=twitter.events,
                                fatal=fatal,
                                start=start)

        external_id = dm['id']
        Channel.success(channel,
                        msg,
                        WIRED,
                        start,
                        events=twitter.events,
                        external_id=external_id)
コード例 #3
0
ファイル: __init__.py プロジェクト: leandroneves/rapidpro
    def send(self, channel, msg, text):
        twitter = TembaTwython.from_channel(channel)
        start = time.time()

        try:
            dm = twitter.send_direct_message(screen_name=msg.urn_path,
                                             text=text)
        except Exception as e:
            error_code = getattr(e, 'error_code', 400)
            fatal = False

            if error_code == 404:  # handle doesn't exist
                fatal = True
            elif error_code == 403:
                for err in self.FATAL_403S:
                    if six.text_type(e).find(err) >= 0:
                        fatal = True
                        break

            # if message can never be sent, stop them contact
            if fatal:
                contact = Contact.objects.get(id=msg.contact)
                contact.stop(contact.modified_by)

            raise SendException(str(e),
                                events=twitter.events,
                                fatal=fatal,
                                start=start)

        external_id = dm['id']
        Channel.success(channel,
                        msg,
                        WIRED,
                        start,
                        events=twitter.events,
                        external_id=external_id)
コード例 #4
0
    def send(self, channel, msg, text):
        twitter = TembaTwython.from_channel(channel)
        start = time.time()

        try:
            urn = getattr(msg, 'urn', URN.from_twitter(msg.urn_path))
            (scheme, path, display) = URN.to_parts(urn)
            print("scheme %s" % (scheme))
            print("path %s" % (path))
            print("display %s" % (display))
            # this is a legacy URN (no display), the path is our screen name
            if scheme == TWITTER_SCHEME:
                dm = twitter.send_direct_message(screen_name=path, text=text)
                external_id = dm['id']

            # this is a new twitterid URN, our path is our user id
            else:
                metadata = msg.metadata if hasattr(msg, 'metadata') else {}
                quick_replies = metadata.get('quick_replies', [])
                formatted_replies = [
                    dict(label=item[:self.quick_reply_text_size])
                    for item in quick_replies
                ]

                if quick_replies:
                    params = {
                        'event': {
                            'type': 'message_create',
                            'message_create': {
                                'target': {
                                    'recipient_id': path
                                },
                                'message_data': {
                                    'text': text,
                                    'quick_reply': {
                                        'type': 'options',
                                        'options': formatted_replies
                                    }
                                }
                            }
                        }
                    }
                    dm = twitter.post('direct_messages/events/new',
                                      params=params)
                    external_id = dm['event']['id']
                else:
                    print(path)
                    print(text)
                    dm = twitter.send_direct_message(user_id=path, text=text)
                    external_id = dm['id']

        except Exception as e:
            error_code = getattr(e, 'error_code', 400)
            fatal = False
            print("Twitter type")
            print(e)

            if error_code == 404:  # handle doesn't exist
                fatal = True
            elif error_code == 403:
                for err in self.FATAL_403S:
                    if six.text_type(e).find(err) >= 0:
                        fatal = True
                        break

            # if message can never be sent, stop them contact
            if fatal:
                contact = Contact.objects.get(id=msg.contact)
                contact.stop(contact.modified_by)

            raise SendException(str(e),
                                events=twitter.events,
                                fatal=fatal,
                                start=start)

        Channel.success(channel,
                        msg,
                        WIRED,
                        start,
                        events=twitter.events,
                        external_id=external_id)