def send_tweet(self, chat, tweet):
        try:
            self.logger.debug("Sending tweet {} to chat {}...".format(
                tweet.tw_id, chat.chat_id
            ))

            '''
            Use a soft-hyphen to put an invisible link to the first
            image in the tweet, which will then be displayed as preview
            '''
            photo_url = ''
            if tweet.photo_url:
                photo_url = '[\xad](%s)' % tweet.photo_url

            created_dt = utc.localize(tweet.created_at)
            if chat.timezone_name is not None:
                tz = timezone(chat.timezone_name)
                created_dt = created_dt.astimezone(tz)
            created_at = created_dt.strftime('%Y-%m-%d %H:%M:%S %Z')
            self.sendMessage(
                chat_id=chat.chat_id,
                disable_web_page_preview=not photo_url,
                text="""
{link_preview}*{name}* ([@{screen_name}](https://twitter.com/{screen_name})) :
{text}
"""
                    # -- [Enllaç a la piulada original](https://twitter.com/{screen_name}/status/{tw_id})
                    .format(
                    link_preview=photo_url,
                    text=prepare_tweet_text(tweet.text),
                    name=escape_markdown(tweet.name),
                    screen_name=tweet.screen_name,
                    # created_at=created_at,
                    # tw_id=tweet.tw_id,
                ),
                parse_mode=telegram.ParseMode.MARKDOWN)

        except TelegramError as e:
            self.logger.info("Couldn't send tweet {} to chat {}: {}".format(
                tweet.tw_id, chat.chat_id, e.message
            ))

            delet_this = None

            if e.message == 'Bad Request: group chat was migrated to a supergroup chat':
                delet_this = True

            if e.message == "Unauthorized":
                delet_this = True

            if delet_this:
                self.logger.info("Marking chat for deletion")
                chat.delete_soon = True
                chat.save()
    def send_tweet(self, chat, tweet):
        try:
            self.logger.debug("Sending tweet {} to chat {}...".format(
                tweet.tw_id, chat.chat_id
            ))

            '''
            Use a soft-hyphen to put an invisible link to the first
            image in the tweet, which will then be displayed as preview
            '''
            photo_url = ''
            if tweet.photo_url:
                photo_url = '[\xad](%s)' % tweet.photo_url

            created_dt = utc.localize(tweet.created_at)
            if chat.timezone_name is not None:
                tz = timezone(chat.timezone_name)
                created_dt = created_dt.astimezone(tz)
            created_at = created_dt.strftime('%Y-%m-%d %H:%M:%S %Z')
            self.sendMessage(
                chat_id=chat.chat_id,
                disable_web_page_preview=not photo_url,
                text="""
{link_preview}*{name}* ([@{screen_name}](https://twitter.com/{screen_name})) at {created_at}:
{text}
-- [Link to this Tweet](https://twitter.com/{screen_name}/status/{tw_id})
"""
                    .format(
                    link_preview=photo_url,
                    text=prepare_tweet_text(tweet.text),
                    name=escape_markdown(tweet.name),
                    screen_name=tweet.screen_name,
                    created_at=created_at,
                    tw_id=tweet.tw_id,
                ),
                parse_mode=telegram.ParseMode.MARKDOWN)

        except TelegramError as e:
            self.logger.info("Couldn't send tweet {} to chat {}: {}".format(
                tweet.tw_id, chat.chat_id, e.message
            ))

            delet_this = None

            if e.message == 'Bad Request: group chat was migrated to a supergroup chat':
                delet_this = True

            if e.message == "Unauthorized":
                delet_this = True

            if delet_this:
                self.logger.info("Marking chat for deletion")
                chat.delete_soon = True
                chat.save()
    def send_tweet(self, chat, tweet: Tweet):
        try:
            self.logger.debug("Sending tweet {} to chat {}...".format(
                tweet.id, chat.chat_id))
            '''
            Use a soft-hyphen to put an invisible link to the first
            image in the tweet, which will then be displayed as preview
            '''
            media_url = ''
            if len(tweet.media_list) == 1:
                media_url = '[\xad](%s)' % tweet.media_list[0].url
            elif tweet.link_url:
                media_url = '[\xad](%s)' % tweet.link_url

            self.sendMessage(chat_id=chat.chat_id,
                             disable_web_page_preview=not media_url,
                             text=self.MESSAGE_TEMPLATE.format(
                                 link_preview=media_url,
                                 text=prepare_tweet_text(tweet.text),
                                 name=escape_markdown(tweet.user_name),
                                 screen_name=tweet.user_screen_name,
                                 id=tweet.id,
                             ),
                             parse_mode=telegram.ParseMode.MARKDOWN)

            if len(tweet.media_list) > 1:
                self.sendMediaGroup(
                    chat_id=chat.chat_id,
                    media=map(self.create_input_media, tweet.media_list),
                )

        except TelegramError as e:
            self.logger.info("Couldn't send tweet {} to chat {}: {}".format(
                tweet.id, chat.chat_id, e.message))

            delete_this = None

            if e.message == 'Bad Request: group chat was migrated to a supergroup chat':
                delete_this = True

            if e.message == "Unauthorized":
                delete_this = True

            if delete_this:
                self.logger.info("Marking chat for deletion")
                chat.delete_soon = True
                chat.save()
Esempio n. 4
0
    def send_tweet(self, chat, tweet):
        try:
            self.logger.debug("Sending tweet {} to chat {}...".format(
                tweet.tw_id, chat.chat_id))
            '''
            Use a soft-hyphen to put an invisible link to the first
            image in the tweet, which will then be displayed as preview
            '''
            photo_url = ''
            if tweet.photo_url:
                photo_url = '[\xad](%s)' % tweet.photo_url

            created_dt = utc.localize(tweet.created_at)
            if chat.timezone_name is not None:
                tz = timezone(chat.timezone_name)
                created_dt = created_dt.astimezone(tz)
            created_at = created_dt.strftime('%Y-%m-%d %H:%M:%S %Z')
            for id in [chat.chat_id, '@whale_trades_alerts']:
                text = prepare_tweet_text(tweet.text)
                if text:
                    self.sendMessage(chat_id=id,
                                     disable_web_page_preview=not photo_url,
                                     text=text,
                                     parse_mode=telegram.ParseMode.MARKDOWN)

        except TelegramError as e:
            self.logger.info("Couldn't send tweet {} to chat {}: {}".format(
                tweet.tw_id, chat.chat_id, e.message))

            delet_this = None

            if e.message == 'Bad Request: group chat was migrated to a supergroup chat':
                delet_this = True

            if e.message == "Unauthorized":
                delet_this = True

            if delet_this:
                self.logger.info("Marking chat for deletion")
                chat.delete_soon = True
                chat.save()
    def send_tweet(self, chat, tweet):
        try:
            self.logger.debug('Sending tweet {} to chat {}...'.format(
                tweet.tw_id, chat.chat_id))
            '''
			Use a soft-hyphen to put an invisible link to the first
			image in the tweet, which will then be displayed as preview
			'''
            photo_url = ''
            if tweet.photo_url:
                photo_url = '[\xad](%s)' % tweet.photo_url

            created_dt = utc.localize(tweet.created_at)
            if chat.timezone_name is not None:
                tz = timezone(chat.timezone_name)
                created_dt = created_dt.astimezone(tz)
            created_at = created_dt.strftime('%Y-%m-%d %H:%M:%S %Z')
            retweet_text = ''
            if tweet.original_name:
                retweet_text = 'retweeted from *{}* '.format(
                    tweet.original_name)
            self.sendMessage(chat_id=chat.chat_id,
                             disable_web_page_preview=not photo_url,
                             text='''
{link_preview}*{name}* ([@{screen_name}](https://twitter.com/{screen_name})) {retweet_text}at {created_at}:
{text}
— [Link to this Tweet](https://twitter.com/{screen_name}/status/{tw_id})
'''.format(
                                 link_preview=photo_url,
                                 text=prepare_tweet_text(tweet.text),
                                 name=escape_markdown(tweet.name),
                                 screen_name=tweet.screen_name,
                                 created_at=created_at,
                                 tw_id=tweet.tw_id,
                                 retweet_text=retweet_text,
                             ),
                             parse_mode=telegram.ParseMode.MARKDOWN)

            return True

        except TelegramError as e:
            self.logger.info('Couldn\'t send tweet {} to chat {}: {}'.format(
                tweet.tw_id, chat.chat_id, e.message))
            #self.logger.debug('\ttweet text: {}'.format(prepare_tweet_text(tweet.text)))

            delet_this = None

            if e.message == 'Forbidden: bot was blocked by the user':
                delet_this = True

            if e.message == 'Bad Request: group chat was migrated to a supergroup chat':
                delet_this = True

            if e.message == 'Unauthorized':
                delet_this = True

            if delet_this:
                self.logger.info('Marking chat for deletion')
                chat.delete_soon = True
                chat.save()

            return False
Esempio n. 6
0
    def send_tweet(self, chat, tweet, sub_kind):
        try:
            if sub_kind == 1:
                if tweet.text[0:1] == 'RT':
                    return
            elif sub_kind == 2:
                if tweet.text[0] == '@':
                    return
            elif sub_kind == 3:
                if len(eval(tweet.photo_url)) == 0:
                    return

            self.logger.debug("Sending tweet {} to chat {}...".format(
                tweet.tw_id, chat.chat_id))
            '''
            Use a soft-hyphen to put an invisible link to the first
            image in the tweet, which will then be displayed as preview
            '''
            photo_url = ''
            video_url = ''

            if len(eval(tweet.photo_url)) != 0:
                photo_url = '[\xad](%s)' % eval(tweet.photo_url)[0]
            if tweet.video_url:
                video_url = tweet.video_url

            created_dt = utc.localize(tweet.created_at)
            #created_dt = tweet.created_at
            if chat.timezone_name is not None:
                tz = timezone(chat.timezone_name)
                created_dt = created_dt.astimezone(tz)
            created_at = created_dt.strftime('%Y-%m-%d %H:%M:%S %Z')
            self.sendMessage(chat_id=chat.chat_id,
                             disable_web_page_preview=not photo_url,
                             text="""
{link_preview}*{name}* ([@{screen_name}](https://twitter.com/{screen_name})) at {created_at}:
{text}
-- [Link to this Tweet](https://twitter.com/{screen_name}/status/{tw_id})
""".format(
                                 link_preview=photo_url,
                                 text=prepare_tweet_text(tweet.text),
                                 name=escape_markdown(tweet.name),
                                 screen_name=tweet.screen_name,
                                 created_at=created_at,
                                 tw_id=tweet.tw_id,
                             ),
                             parse_mode=telegram.ParseMode.MARKDOWN)

            if video_url:
                try:
                    self.sendVideo(chat_id=chat.chat_id, video=tweet.video_url)
                except TelegramError as e:
                    self.logger.warning(
                        "Sending tweet {} video failed, video url is {}, error is {}"
                        .format(tweet.tw_id, tweet.video_url, e))
                    file = open("video_url.txt", "a")
                    file.write('\n')
                    file.write(str(video_url))
                    file.write('\n')
                    file.write(str(tweet.video_url))
                    file.close()
            else:
                try:
                    if photo_url:
                        for img in eval(tweet.photo_url):
                            self.sendPhoto(chat_id=chat.chat_id, photo=img)
                except TelegramError as e:
                    self.logger.warning(
                        "Sending tweet {} img failed, img url is {}, error is {}"
                        .format(tweet.tw_id, tweet.photo_url, e))
                    file = open("photo_url.txt", "a")
                    file.write('\n')
                    file.write(str(photo_url))
                    file.write('\n')
                    file.write(str(tweet.photo_url))
                    file.close()

        except TelegramError as e:
            self.logger.info("Couldn't send tweet {} to chat {}: {}".format(
                tweet.tw_id, chat.chat_id, e.message))

            delet_this = None

            if e.message == 'Bad Request: group chat was migrated to a supergroup chat':
                delet_this = True

            if e.message == "Unauthorized":
                delet_this = True

            if delet_this:
                self.logger.info("Marking chat for deletion")
                chat.delete_soon = True
                chat.save()