async def send(self, reply_to: Union[int, types.Message, None] = None) \ -> Optional[Union[types.Message, list[types.Message]]]: msg_lock, flood_lock = locks.user_msg_locks(self.user_id) while True: try: async with flood_lock: pass # wait for flood wait async with msg_lock: # acquire a msg lock async with self.__overall_semaphore: # only acquire overall semaphore when sending if self.media_type == MEDIA_GROUP: media = [] for medium in self.media: _, fm, _ = await env.bot._file_to_media(medium) media.append( types.InputSingleMedia(fm, message='')) media[ -1].message = self.plain_text if self.plain_text else '' media[ -1].entities = self.format_entities if self.format_entities else None entity = await env.bot.get_input_entity( self.user_id) reply_to = get_message_id(reply_to) request = functions.messages.SendMultiMediaRequest( entity, reply_to_msg_id=reply_to, multi_media=media, silent=self.silent) result = await env.bot(request) random_ids = [m.random_id for m in media] ret = env.bot._get_response_message( random_ids, result, entity) return ret # non-album return await env.bot.send_message( entity=self.user_id, message=self.plain_text, formatting_entities=self.format_entities, file=self.media, attributes=self.attributes, reply_to=reply_to, link_preview=self.link_preview, silent=self.silent) except (FloodWaitError, SlowModeWaitError) as e: # telethon has retried for us, but we release locks and retry again here to see if it will be better if self.retries >= 1: logger.error( f'Msg dropped due to too many flood control retries ({self.user_id})' ) return None self.retries += 1 await locks.user_flood_wait(self.user_id, seconds=e.seconds ) # acquire a flood wait except ServerError as e: # telethon has retried for us, so we just retry once more if self.retries >= 1: raise e self.retries += 1
async def send_media(self, entity: Union[TypeInputPeer, TypePeer], media: Union[TypeInputMedia, TypeMessageMedia], caption: str = None, entities: List[TypeMessageEntity] = None, reply_to: int = None) -> Optional[Message]: entity = await self.get_input_entity(entity) reply_to = utils.get_message_id(reply_to) request = SendMediaRequest(entity, media, message=caption or "", entities=entities or [], reply_to_msg_id=reply_to) return self._get_response_message(request, await self(request), entity)
csv_writer.writerow([str(temp_id),-1]) f1.close() idn=df['group_id'] last_message_id=df['last_message_id'] for name in idn: group_id=int(name) try: channel_item = client.get_entity(group_id) #获取channel这个entity的信息 messages = client.get_messages(group_id) mid=last_message_id[idn.index(group_id)] print('The last message id crawled last time: ',mid) for message in messages: if int(utils.get_message_id(message)) > mid: speak=str(message.message).replace('\r', '').replace('\n', '').replace('\t', '') sender=str(utils.get_display_name(message.sender)) #对字段长度进行截断,防止数据库报错 if len(speak)>500: speak=speak[:500] if len(sender)>80: sender=sender[:80] if len(speak)!=0 and speak!='None': cursor.execute("INSERT INTO rawData VALUES(null,%s,%s,%s,%s);", (str(group_id), str(sender), str(str(message.date)[:19]),str(speak))) else: break #获取图片 photos = client.get_messages(group_id, None, filter=InputMessagesFilterPhotos) for photo in photos: