Esempio n. 1
0
    def test_switch_mode(self):
        bot_user = BotUser(**self.default_user_data)
        bot = EvernoteBot(self.config)
        with self.assertRaises(EvernoteBotException) as ctx:
            bot.switch_mode(bot_user, "invalid")
        self.assertEqual(str(ctx.exception), "Unknown mode 'invalid'")

        bot.api = mock.Mock()
        bot.api.sendMessage = mock.Mock()
        bot.switch_mode(bot_user, "multiple_notes")
        bot.api.sendMessage.assert_called_once_with(
            1, "The bot already in 'multiple_notes' mode.",
            "{\"hide_keyboard\": true}")

        bot.switch_mode_one_note = mock.Mock()
        bot.switch_mode(bot_user, "one_note")
        bot.switch_mode_one_note.assert_called_once()

        bot_user.bot_mode = "one_note"
        bot_user.evernote.shared_note_id = 123
        bot.api.sendMessage = mock.Mock()
        bot.switch_mode(bot_user, "multiple_notes")
        bot.api.sendMessage.assert_called_once_with(
            1, "The bot has switched to 'multiple_notes' mode.",
            "{\"hide_keyboard\": true}")
        self.assertIsNone(bot_user.evernote.shared_note_id)
        self.assertEqual(bot_user.bot_mode, "multiple_notes")
Esempio n. 2
0
 def switch_mode(self, bot_user: BotUser, selected_mode_str: str):
     new_mode, new_mode_title = self._validate_mode(selected_mode_str)
     if bot_user.bot_mode == new_mode:
         text = f'The bot already in `{new_mode_title}` mode.'
         self.send_message(text)
         return
     if new_mode == 'one_note':
         self.switch_mode_one_note(bot_user)
     elif new_mode == 'multiple_notes':
         bot_user.evernote.shared_note_id = None
         bot_user.bot_mode = new_mode
         self.send_message(
             f'The bot has switched to `{new_mode_title}` mode.')
     raise EvernoteBotException(f'Unknown mode `{new_mode}`')
Esempio n. 3
0
 def switch_mode(self, bot_user: BotUser, selected_mode_str: str):
     new_mode, new_mode_title = self._validate_mode(selected_mode_str)
     chat_id = bot_user.telegram.chat_id
     if bot_user.bot_mode == new_mode:
         text = f"The bot already in '{new_mode_title}' mode."
         self.api.sendMessage(chat_id, text, json.dumps({"hide_keyboard": True}))
         return
     if new_mode == "one_note":
         self.switch_mode_one_note(bot_user)
         return
     # switching to 'multiple_notes' mode
     bot_user.evernote.shared_note_id = None
     bot_user.bot_mode = new_mode
     text = f"The bot has switched to '{new_mode_title}' mode."
     self.api.sendMessage(chat_id, text, json.dumps({"hide_keyboard": True}))
Esempio n. 4
0
 def switch_mode_one_note(self, bot_user: BotUser):
     chat_id = bot_user.telegram.chat_id
     evernote_data = bot_user.evernote
     if evernote_data.access.permission == 'full':
         note = self.evernote(bot_user).create_note(
             evernote_data.notebook.guid,
             title='Telegram bot notes'
         )
         bot_user.bot_mode = 'one_note' # TODO: move up
         evernote_data.shared_note_id = note.guid
         note_url = self.evernote(bot_user).get_note_link(note.guid)
         text = f'Your notes will be saved to <a href="{note_url}">this note</a>'
         self.api.sendMessage(chat_id, text, json.dumps({'hide_keyboard': True}), parse_mode='Html')
     else:
         text = 'To enable "One note" mode you have to allow to the bot both reading and updating your notes'
         self.api.sendMessage(chat_id, text, json.dumps({'hide_keyboard': True}))
         message_text = 'Please, sign in and give the permissions to the bot.'
         bot_user.evernote.oauth = get_evernote_oauth_data(self, bot_user,
             message_text, access='full')