def get_title(bot, update, user_data): # del_update_and_message(update) chat_type = user_data['chat_type'].split(' ') chats = models.Chat.objects.filter(title=get_message(update).text, type=chat_type[0]) if chats and len(chats) == 1 and not chats.first().extra_fields['left']: chat = chats.first() api_chat = APIChat(chat.id, chat.type, bot=bot) if get_message(update).from_user.id in [ admin.user.id for admin in api_chat.get_administrators() ]: reply_message(update, text='Lucky! The secret key for your chat is:') send_message(bot, get_message(update).chat_id, text='{}'.format(chat.identifier)) if not chat.extra_fields[enabled]: chat.extra_fields[enabled] = True return ConversationHandler.END else: reply_message( update, text='Sorry, you are not an admin in this chat so I cannot give ' 'you the chat\'s secret key. Please contact your admin to get the key.' ) return ConversationHandler.END reply_message( update, text= 'Unfortunately, I couldn\'t find your chat either because no chat using my service ' 'has such title or there are many of them. Please contact my developer (@{}) ' 'and I am sure he will help you find your chat key'.format( dev_username)) return ConversationHandler.END
def get_username(bot, update, user_data): del_update_and_message(update) chat = get_or_none(models.Chat, username=get_message(update).text) if chat and not chat.extra_fields['left']: api_chat = APIChat(chat.id, chat.type, bot=bot) if get_message(update).from_user.id in [ admin.user.id for admin in api_chat.get_administrators() ]: reply_message(update, text='The secret key for your chat is:') send_message(bot, get_message(update).chat_id, text='{}'.format(chat.identifier)) if not chat.extra_fields[enabled]: chat.extra_fields[enabled] = True return ConversationHandler.END else: reply_message( update, text='Sorry, you are not an admin in this chat so I cannot give ' 'you the chat\'s secret key. Please contact your admin to get the key.' ) return ConversationHandler.END reply_message( update, text= 'Oops! This username is not in the list of groups using my service. Are you sure you ' 'it is correct? Make sure to remove the @ symbol. You can try entering the ' 'username again or use /cancel to quit ') return CHAT_USERNAME
def test_is_admin_on_channel(self): command = RestrcitCommand() chat = Chat(id=1, type="supergroup", title="channel 1") chat.get_administrators = MagicMock(return_value=[ AttrDict({"user": AttrDict({"id": 1})}), AttrDict({"user": AttrDict({"id": 2})}), ]) self.bot.get_chat = MagicMock(return_value=chat) # is not admin self.assertFalse(command.is_admin_on_channel(self.bot, chat.id, 3)) # is admin self.assertTrue(command.is_admin_on_channel(self.bot, chat.id, 1))
def del_receiver(bot, update, user_data): del_update_and_message(update) logger.debug('del_receiver called') try: sender = get_or_none(models.Chat, id=user_data['sender_id']) receiver = get_or_none(models.Chat, identifier=get_message(update).text) if receiver is None: reply_message( update, text="Unfortunately, the key you provided does not exist, " "please check that the key is correct") return DEL_RECEIVER if get_message(update).text == str(sender.identifier): reply_message( update, text= "Wow, wow, easy! The keys of the forwarder and the receiver must be different, " "please enter the correct key or use /cancel to terminate deletion" ) return DEL_RECEIVER forwarding = get_or_none(AutoForward, forwarder__id=user_data['sender_id'], receiver__id=receiver.id, enabled=True) if not forwarding: reply_message( update, text= "Oops! There is no auto forwarding set between these two chats, " "ensure that both have an active auto forwarding and " "you have entered the keys in the right order, then try again." ) return ConversationHandler.END authorized = False if forwarding.creator.id != get_message(update).from_user.id: api_sender = APIChat(sender.id, sender.type, bot=bot) api_receiver = APIChat(receiver.id, receiver.type, bot=bot) if api_sender.type != models.Chat.PRIVATE: if get_message(update).from_user.id not in [ admin.user.id for admin in api_sender.get_administrators() ]: if api_receiver.type != models.Chat.PRIVATE: if get_message(update).from_user.id in [ admin.user.id for admin in api_receiver.get_administrators() ]: authorized = True elif api_receiver.id == get_message(update).from_user.id: authorized = True else: authorized = True elif api_sender.id == get_message(update).from_user.id: authorized = True else: authorized = True if not authorized: reply_message( update, text= "Unfortunately you are neither the creator of this auto forwarding " "or an admin in one of the chats involved. Please request deactivation from " "the admin or the auto forwarding creator") return ConversationHandler.END else: forwarding.enabled = False forwarding.save() send_message(bot, get_message(update).chat_id, text="forwarding has been deleted successfully.") logger.info("forwarding from {} to {} has been deleted".format( sender.title, receiver.title)) forwardings_rec = AutoForward.objects.filter(receiver=receiver, enabled=True) forwardings_send = AutoForward.objects.filter(forwarder=sender, enabled=True) if receiver.type != models.Chat.CHANNEL and receiver.type != models.Chat.PRIVATE: text = 'The auto forwarding from {} to this {} has been deleted, ' \ 'so I will no longer forward message from there.'.format(sender.title, receiver.type) if len(forwardings_rec) == 0: text += ' Since there are no more auto forwardings involving this chat, I will now '\ 'leave. If you need me again, just holla at me (@{}) and add me '\ 'me to the chat. It was fun having you all!'.format(fd_username) send_message(bot, chat_id=receiver.id, text=text) if receiver.type != models.Chat.PRIVATE: if len(forwardings_rec) == 0: bot.leave_chat(receiver.id) disable_chat(receiver.id) logger.info('{} has left the {} {}'.format( fd_username, receiver.type, receiver.title)) # receiver.delete() if len(forwardings_send) == 0: bot.leave_chat(sender.id) disable_chat(sender.id) logger.info('{} has left the {} {}'.format( fd_username, receiver.type, receiver.title)) # sender.delete() user_data = {} return ConversationHandler.END except Exception as ex: logger.error(ex.message) reply_message( update, text="Oops! Something went wrong, please try again later!") return RECEIVER