async def __aexit__(self, exc_type, exc_val, exc_tb) -> None: _CONV_DICT[self._chat_id].put_nowait(None) del _CONV_DICT[self._chat_id] error = '' if isinstance(exc_val, asyncio.exceptions.TimeoutError): error = f"ended conversation with {self._chat_id}, timeout reached!" if isinstance(exc_val, _MsgLimitReached): error = f"ended conversation with {self._chat_id}, message limit reached!" if error: _LOG.error(_LOG_STR, error) raise StopConversation(error)
async def broadcast(msg: PyroMessage): global IN_CONVO # pylint: disable=global-statement if len(_USERS) < 1: return await bot.send_message(msg.chat.id, "No one Started your bot. 🤭") IN_CONVO = True temp_msgs = [] async with userge.bot.conversation( msg.chat.id, timeout=30, limit=7) as conv: # 5 post msgs and 2 command msgs await conv.send_message(MESSAGE) filter_ = filters.create( lambda _, __, ___: filters.incoming & ~filters.edited) while True: response = await conv.get_response(filters=filter_) if response.text and response.text.startswith("/cancel"): IN_CONVO = False return await msg.reply("Broadcast process Cancelled.") if len(temp_msgs) >= 1 and response.text == "/done": break if len(temp_msgs) >= 1 and response.text == "/preview": conv._count -= 1 for i in temp_msgs: if i.poll: await conv.send_message("Poll Message.") continue await i.copy(conv.chat_id) await conv.send_message( CONTINUE_MESSAGE.format(PREVIEW_MESSAGE)) continue if len(temp_msgs) >= 5: raise StopConversation("message limit reached!") temp_msgs.append(response) await response.copy(conv.chat_id) await conv.send_message(CONTINUE_MESSAGE.format(NEXT_MESSAGE)) confirm_text = CONFIRM_TEXT.format(len(temp_msgs)) await conv.send_message(confirm_text) response = await conv.get_response(filters=filter_) while True: if response.text == "/send": await send_broadcast_post(msg, temp_msgs) IN_CONVO = False return if response.text == "/cancel": await conv.send_message("Broadcast process Cancelled.") IN_CONVO = False return conv._count -= 1 await conv.send_message("Invalid Arguments!") await conv.send_message(confirm_text) response = await conv.get_response(filters=filter_)
async def __aenter__(self) -> 'Conversation': self._chat_id = int(self._chat) if isinstance(self._chat, int) else \ (await self._client.get_chat(self._chat)).id if self._chat_id in _CONV_DICT: error = f"already started conversation with {self._chat_id} !" _LOG.error(_LOG_STR, error) raise StopConversation(error) if self._user: self._user_id = int(self._user) if isinstance(self._user, int) else \ (await self._client.get_users(self._user)).id _CONV_DICT[self._chat_id] = (self._user_id, asyncio.Queue(self._limit)) else: _CONV_DICT[self._chat_id] = asyncio.Queue(self._limit) return self
async def __aexit__(self, exc_type, exc_val, exc_tb) -> None: pack = (self._chat_id, self._client) queue = _CONV_DICT[pack] if isinstance(queue, tuple): queue = queue[1] queue.put_nowait(None) del _CONV_DICT[pack] error = '' if isinstance(exc_val, asyncio.exceptions.TimeoutError): error = (f"ended conversation {self._client} with {self._chat_id}, " "timeout reached!") if isinstance(exc_val, _MsgLimitReached): error = (f"ended conversation {self._client} with {self._chat_id}, " "message limit reached!") if error: _LOG.error(_LOG_STR, error) raise StopConversation(error)