示例#1
0
 def handle_user_removed_from_chat_update(self, update):
     # type: (UserRemovedFromChatUpdate) -> bool
     self.change_subscriber(UpdateCmn(update, self),
                            True,
                            recreate_cache=False)
     return super(TamTamBotDj,
                  self).handle_user_removed_from_chat_update(update)
示例#2
0
 def prev_step_get(self, index):
     # type: (str) -> Update
     prev_steps = TtbPrevStep.objects.filter(index=index)
     if prev_steps.exists() and isinstance(prev_steps[0], TtbPrevStep):
         update = self.deserialize_update(prev_steps[0].update)
         TtbUser.update_or_create_by_update(UpdateCmn(update, self))
         return update
示例#3
0
 def handle_message_chat_created_update(self, update):
     # type: (MessageChatCreatedUpdate) -> bool
     self.change_subscriber(UpdateCmn(update, self),
                            True,
                            recreate_cache=False)
     return super(TamTamBotDj,
                  self).handle_message_chat_created_update(update)
示例#4
0
 def handle_user_added_to_chat_update(self, update):
     # type: (UserAddedToChatUpdate) -> bool
     self.change_subscriber(UpdateCmn(update, self),
                            True,
                            recreate_cache=False)
     return super(TamTamBotDj,
                  self).handle_user_added_to_chat_update(update)
示例#5
0
 def set_user_language_by_update(self, update, language):
     # type: (Update, str) -> None
     language = language or self.get_default_language()
     update = UpdateCmn(update)
     if update:
         ttb_user, created = TtbUser.update_or_create_by_update(update)
         if isinstance(ttb_user, TtbUser):
             ttb_user.language = language
             ttb_user.save()
示例#6
0
 def get_user_language_by_update(self, update):
     # type: (Update) -> str
     language = self.get_default_language()
     update = UpdateCmn(update)
     if update:
         ttb_user, created = TtbUser.update_or_create_by_update(update)
         if isinstance(ttb_user, TtbUser):
             language = ttb_user.language or self.get_default_language()
     return language
示例#7
0
 def get_user_language_by_update(self, update):
     # type: (Update) -> str
     update = UpdateCmn(update, self)
     language = update.user_locale or self.get_default_language()
     if language[:2] not in self.languages_dict.keys():
         language = self.get_default_language()
     if update:
         ttb_user, created = TtbUser.update_or_create_by_update(update)
         if isinstance(ttb_user, TtbUser):
             language = ttb_user.language or language
     return language
示例#8
0
 def prev_step_write(self, index, update):
     # type: (str, Update) -> None
     if not self.prev_step_exists(index):
         b_obj = self.serialize_update(update)
         ttb_user, created = TtbUser.update_or_create_by_update(
             UpdateCmn(update, self))
         if isinstance(ttb_user, TtbUser):
             TtbPrevStep.objects.update_or_create(index=index,
                                                  defaults={
                                                      'user': ttb_user,
                                                      'update': b_obj,
                                                      'updated': now()
                                                  })
示例#9
0
 def set_user_language_by_update(self,
                                 update,
                                 language,
                                 soft_setting=False):
     # type: (Update, str, bool) -> None
     update = UpdateCmn(update, self)
     language = language or self.get_default_language()
     if language[:2] not in self.languages_dict.keys():
         language = self.get_default_language()
     if update:
         ttb_user, created = TtbUser.update_or_create_by_update(update)
         if isinstance(ttb_user, TtbUser):
             if ttb_user.language:
                 if not soft_setting:
                     ttb_user.language = language
             else:
                 ttb_user.language = language
             ttb_user.save()
示例#10
0
 def handle_bot_added_to_chat_update(self, update):
     # type: (BotAddedToChatUpdate) -> bool
     update = UpdateCmn(update, self)
     chat = self.chats.get_chat(update.chat_id)
     if isinstance(chat, Chat):
         admins_chats = self.admins_contacts.get('chats') or []
         if update.chat_id not in admins_chats:
             chat_ext = self.chat_is_available(chat, update.user.user_id)
             if chat_ext and self.chat_is_allowed_for_add(
                     chat_ext, update.user.user_id):
                 return bool(self.change_subscriber(update, True))
             res = self.chats.leave_chat(update.chat_id)
             if isinstance(
                     res,
                     SimpleQueryResult) and not res.success and isinstance(
                         update.user, User):
                 self.send_admin_message(
                     f'Error leaving chat {chat.chat_id} ({chat.title}/{chat.link}): {res.message}\nTry adding user {update.user.user_id} - {update.user.name}(@{update.user.username})'
                 )
             elif isinstance(
                     res, SimpleQueryResult) and res.success and isinstance(
                         update.user, User):
                 if not chat_ext:
                     chat_ext = ChatExt(chat, self.title)
                 try:
                     # noinspection PyTypeChecker
                     self.send_message(NewMessageBody(
                         _('The bot %(bot_name)s cannot be added to %(chat_name)s, because it cannot work correctly under the current environment (rights, chat type, etc.).'
                           ) % {
                               'bot_name':
                               f'<{self.name} (@{self.username})>',
                               'chat_name': chat_ext.chat_name_ext,
                           }),
                                       user_id=update.user.user_id)
                 except (ApiException, ValueError):
                     pass
     return False
示例#11
0
 def handle_bot_removed_from_chat_update(self, update):
     # type: (BotRemovedFromChatUpdate) -> bool
     return bool(self.change_subscriber(UpdateCmn(update, self), False))
示例#12
0
 def handle_bot_started_update(self, update):
     # type: (BotStartedUpdate) -> bool
     return super(
         TamTamBotDj,
         self).handle_bot_started_update(update) and self.change_subscriber(
             UpdateCmn(update, self), False)
示例#13
0
 def process_command(self, update):
     # type: (Update) -> bool
     uc = UpdateCmn(update, self)
     uc.user.disable = False
     self.change_subscriber(uc, True, recreate_cache=False)
     return super(TamTamBotDj, self).process_command(update)