Exemple #1
0
 def save(self, refresh_meta=True, **kwargs):
     if refresh_meta:
         # bot = bot or get_bot()
         chat = bot.get_chat(chat_id=self.chat_id)
         self.identifier = chat.id
         self.username = chat.username
         self.title = chat.title
     super(Channel, self).save(**kwargs)
Exemple #2
0
    def clean(self):
        # no either id or username should be specified
        if not self.identifier and not self.username:
            raise ValidationError(
                "Identifier or username should be provided for channel.")
        # no access to channel
        try:
            bot.get_chat(chat_id=self.chat_id)
            admins = bot.get_chat_administrators(chat_id=self.chat_id)

            for admin in admins:
                if admin.user.username == bot.username:
                    if not admin.can_post_messages:
                        raise ValidationError("Bot can't post messages.")
                    break
            else:
                raise ValidationError("Bot is not admin.")
        except TelegramError:
            raise ValidationError("Bot doesn't have access to this channel.")
Exemple #3
0
def refresh_channels_meta() -> list:
    logger.info('Refreshing channels info...')
    channels = Channel.objects.all()
    stats = []
    for channel in channels:
        chat = bot.get_chat(chat_id=channel.chat_id)

        refreshed = any(
            map(
                lambda a: a[0] != a[1],
                zip([channel.identifier, channel.username, channel.title],
                    [str(chat.id), chat.username, chat.title])))
        if refreshed:
            stats.append(channel.title)

        channel.save(bot=bot)
    logger.info('Done.')
    return stats
Exemple #4
0
 def save(self, *args, **kwargs):
     if self.channel_username:
         chat = bot.get_chat(chat_id=self.channel_username)
         self.chat_id = chat.id
     return super().save(*args, **kwargs)
Exemple #5
0
 def _bot_has_access(self):
     try:
         bot.get_chat(chat_id=self.channel_username)
     except TelegramError:
         raise ValidationError("Bot doesn't have access to this channel.")