예제 #1
0
 def get_chat(self, chat_uid: str, member_uid: Optional[str] = None) -> EFBChat:
     for i in self.chats:
         if chat_uid == i.chat_uid:
             if member_uid:
                 if i.chat_type == ChatType.Group:
                     for j in i.members:
                         if j.chat_uid == member_uid:
                             return j
                     raise EFBChatNotFound()
                 else:
                     raise EFBChatNotFound()
             return i
     raise EFBChatNotFound()
예제 #2
0
 def get_chat(self, chat_uid: str, member_uid: Optional[str] = None) -> EFBChat:
     if member_uid:
         chat = self.chats.search_member(uid=chat_uid, member_id=member_uid)
         if not chat:
             raise EFBChatNotFound()
         else:
             return chat
     else:
         chat = self.chats.search_chat(uid=chat_uid)
         if not chat:
             raise EFBChatNotFound()
         else:
             return chat
예제 #3
0
 def search_member(self, uid: str, member_id: str, refresh: bool = False) -> Chat:
     group = self.search_chat(uid)
     if not isinstance(group, wxpy.Group):
         raise EFBChatNotFound()
     try:
         if refresh:
             self.bot.chats(True)
         chat: wxpy.Chat = wxpy.utils.ensure_one(group.search(puid=member_id))
         return self.wxpy_chat_to_efb_chat(chat)
     except ValueError:
         if not refresh:
             return self.search_chat(uid, refresh=True)
         else:
             raise EFBChatNotFound()
예제 #4
0
 async def async_get_chat(self, chat_uid: ChatID) -> Chat:
     cache = self.get_chat_cache.get(chat_uid, None)
     if cache:
         return cache
     else:
         chat = None
         async for diag in self.client.iter_dialogs():
             if int(chat_uid) == diag.entity.id:
                 chat = self.make_efb_chat_obj(diag)
         if chat is None:
             raise EFBChatNotFound()
         self.get_chat_cache[chat_uid] = chat
         return chat
예제 #5
0
파일: chats.py 프로젝트: Z0-0Z/chat
 def search_chat(self, uid: str, refresh: bool=False) -> EFBChat:
     try:
         if refresh:
             self.bot.chats(True)
         if uid in wxpy.Chat.SYSTEM_ACCOUNTS:
             chat: wxpy.Chat = wxpy.Chat(wxpy.utils.wrap_user_name(uid), self.bot)
         else:
             chat: wxpy.Chat = wxpy.utils.ensure_one(self.bot.search(puid=uid))
         return self.wxpy_chat_to_efb_chat(chat)
     except ValueError:
         if not refresh:
             return self.search_chat(uid, refresh=True)
         else:
             raise EFBChatNotFound()
예제 #6
0
 def get_chat(self, chat_uid: str, member_uid: Optional[str] = None) -> EFBChat:
     # todo what is member_uid used for?
     chat_type = chat_uid.split('_')
     if chat_type[0] == 'private':
         qq_uid = int(chat_type[1])
         remark = self.get_friend_remark(qq_uid)
         context = {"user_id": qq_uid}
         if remark is not None:
             context['alias'] = remark
         return self.chat_manager.build_efb_chat_as_user(context, True)
     elif chat_type[0] == 'group':
         group_id = int(chat_type[1])
         context = {'message_type': 'group', 'group_id': group_id}
         return self.chat_manager.build_efb_chat_as_group(context)
     elif chat_type[0] == 'discuss':
         discuss_id = int(chat_type[1])
         context = {'message_type': 'discuss', 'discuss_id': discuss_id}
         return self.chat_manager.build_efb_chat_as_group(context)
     raise EFBChatNotFound()
예제 #7
0
    def update_group_info(self, bot: telegram.Bot, update: telegram.Update):
        """
        Update the title and profile picture of singly-linked Telegram group
        according to the linked remote chat.
        """
        if update.effective_chat.id == self.bot.get_me().id:
            return self.bot.reply_error(
                update,
                self.
                _('Send /update_info in a group where this bot is a group admin '
                  'to update group title and profile picture'))
        if update.effective_message.forward_from_chat and \
                update.effective_message.forward_from_chat.type == telegram.Chat.CHANNEL:
            tg_chat = update.effective_message.forward_from_chat.id
        else:
            tg_chat = update.effective_chat.id
        chats = self.db.get_chat_assoc(master_uid=utils.chat_id_to_str(
            channel=self.channel, chat_uid=tg_chat))
        if len(chats) != 1:
            return self.bot.reply_error(
                update,
                self.ngettext(
                    'This only works in a group linked with one chat. '
                    'Currently {0} chat linked to this group.',
                    'This only works in a group linked with one chat. '
                    'Currently {0} chats linked to this group.',
                    len(chats)).format(len(chats)))
        picture: Optional[IO] = None
        pic_resized: Optional[IO] = None
        try:
            channel_id, chat_uid = utils.chat_id_str_to_id(chats[0])
            channel = coordinator.slaves[channel_id]
            chat = channel.get_chat(chat_uid)
            if chat is None:
                raise EFBChatNotFound()
            chat = ETMChat(chat=chat, db=self.db)
            bot.set_chat_title(tg_chat, chat.chat_title)
            picture = channel.get_chat_picture(chat)
            if not picture:
                raise EFBOperationNotSupported()
            pic_img = Image.open(picture)

            if pic_img.size[0] < self.TELEGRAM_MIN_PROFILE_PICTURE_SIZE or \
                    pic_img.size[1] < self.TELEGRAM_MIN_PROFILE_PICTURE_SIZE:
                # resize
                scale = self.TELEGRAM_MIN_PROFILE_PICTURE_SIZE / min(
                    pic_img.size)
                pic_resized = io.BytesIO()
                pic_img.resize(tuple(map(lambda a: int(scale * a), pic_img.size)), Image.BICUBIC) \
                    .save(pic_resized, 'PNG')
                pic_resized.seek(0)

            picture.seek(0)

            bot.set_chat_photo(tg_chat, pic_resized or picture)
            update.message.reply_text(self._('Chat details updated.'))
        except KeyError:
            return self.bot.reply_error(update,
                                        self._('Channel linked is not found.'))
        except EFBChatNotFound:
            self.logger.exception("Chat linked is not found in channel.")
            return self.bot.reply_error(
                update, self._('Chat linked is not found in channel.'))
        except telegram.TelegramError as e:
            self.logger.exception("Error occurred while update chat details.")
            return self.bot.reply_error(
                update,
                self._('Error occurred while update chat details.\n'
                       '{0}'.format(e.message)))
        except EFBOperationNotSupported:
            return self.bot.reply_error(
                update, self._('No profile picture provided from this chat.'))
        except Exception as e:
            self.logger.exception("Unknown error caught when querying chat.")
            return self.bot.reply_error(
                update,
                self._('Error occurred while update chat details. \n'
                       '{0}'.format(e)))
        finally:
            if getattr(picture, 'close', None):
                picture.close()
            if getattr(pic_resized, 'close', None):
                pic_resized.close()
예제 #8
0
 def get_chat(self, chat_uid: str) -> Chat:
     chat = self.chats.search_chat(uid=chat_uid)
     if not chat:
         raise EFBChatNotFound()
     else:
         return chat
예제 #9
0
 def get_chat(self, chat_uid: str) -> Chat:
     for i in self.chats:
         if chat_uid == i.uid:
             return i
     raise EFBChatNotFound()