Exemplo n.º 1
0
    def test_questionnaire(self):
        question_node = QuestionnaireNode(['question1', 'question2'],
                                          admin_username='******')
        menu = MenuNode('💡Menu', [MenuItem('questionnaire', question_node)])
        bot = FakeBot(menu=menu)
        bot.user_id = self.bot_id

        state = bot.conversation.get_state(self.chat_id)
        assert state is None

        self.send_message(bot, 'test')
        state = bot.conversation.get_state(self.chat_id)
        assert state['node'] is menu

        self.send_message(bot, '1')
        state = bot.conversation.get_state(self.chat_id)
        assert state['node'] is question_node
        assert bot.messages[self.user_id][-1] == 'question1'

        self.send_message(bot, 'answer1')
        state = bot.conversation.get_state(self.chat_id)
        assert state['node'] is question_node
        assert bot.messages[self.user_id][-1] == 'question2'

        self.send_message(bot, 'answer2')
        state = bot.conversation.get_state(self.chat_id)
        assert state['node'] is menu
        assert 'question1' in bot.messages['admin'][-1]
        assert 'answer1' in bot.messages['admin'][-1]
        assert 'question2' in bot.messages['admin'][-1]
        assert 'answer2' in bot.messages['admin'][-1]
Exemplo n.º 2
0
    def test_dummy_node_with_trigger(self):
        node = DummyNode()
        menu = MenuNode('💡Menu', [MenuItem('dummy', node)])
        bot = FakeBot(menu=menu, trigger='/menu')
        bot.user_id = self.bot_id

        self.send_message(bot, '/menu')
        assert len(bot.messages[self.user_id]) == 1
        assert 'Menu' in bot.messages[self.user_id][-1]

        self.send_message(bot, '1')
        assert len(bot.messages[self.user_id]) == 1
Exemplo n.º 3
0
    def test_message_node(self):
        node = MessageNode(text='test message')
        menu = MenuNode('💡Menu', [MenuItem('message', node)])
        bot = FakeBot(menu=menu)
        bot.user_id = self.bot_id

        self.send_message(bot, 'test')
        assert len(bot.messages[self.user_id]) == 1

        self.send_message(bot, '1')
        assert len(bot.messages[self.user_id]) == 3
        assert 'test message' in bot.messages[self.user_id][-2]
        assert 'Menu' in bot.messages[self.user_id][-1]
Exemplo n.º 4
0
    def test_parse_messages(self):
        timestamp = 1559826742121822

        text = 'Test'

        message_body = {
            'inbox': {
                'threads': [{
                    'is_group':
                    False,
                    'items': [{
                        'item_id': ('28773724711249397008013560068964352'),
                        'item_type':
                        'text',
                        'text':
                        text,
                        'timestamp':
                        timestamp,
                        'user_id':
                        self.user_id
                    }],
                    'thread_id':
                    self.chat_id,
                    'thread_title':
                    'user',
                    'thread_type':
                    'private',
                    'thread_v2_id':
                    '18037398148086980',
                    'users': [{
                        'full_name': 'User',
                        'pk': self.user_id,
                        'username': '******'
                    }],
                }],
            },
            'viewer': {
                'full_name': 'Bot Name',
                'pk': self.bot_id,
                'username': '******'
            }
        }
        bot = FakeBot(menu=MenuNode('test', []))
        bot.user_id = self.bot_id
        messages = list(bot.parse_messages(message_body, timestamp - 1))
        assert len(messages) == 1
        assert messages[0]['text'] == text
Exemplo n.º 5
0
    def test_notify_node(self):
        admin_username = '******'
        node = NotifyAdminNode(text='notification is sent',
                               notification='notification',
                               admin_username=admin_username)
        menu = MenuNode('💡Menu', [MenuItem('notify', node)])
        bot = FakeBot(menu=menu)
        bot.user_id = self.bot_id

        self.send_message(bot, 'test')
        assert len(bot.messages[self.user_id]) == 1

        self.send_message(bot, '1')
        assert 'notification is sent' in bot.messages[self.user_id][-2]
        assert 'Menu' in bot.messages[self.user_id][-1]
        assert bot.messages[admin_username][-1] == ('notification\n@{}'.format(
            self.username))
Exemplo n.º 6
0
    def test_handle_message(self):
        msg_node = MessageNode('test message')
        menu = MenuNode('💡Menu', [MenuItem('message', msg_node)])
        bot = FakeBot(menu=menu)
        bot.user_id = self.bot_id

        state = bot.conversation.get_state(self.chat_id)
        assert state is None

        self.send_message(bot, 'test')
        state = bot.conversation.get_state(self.chat_id)
        assert state['node'] is menu

        self.send_message(bot, '1')
        state = bot.conversation.get_state(self.chat_id)
        assert state['node'] is menu
        assert 'test message' in bot.messages[self.user_id][-2]
        assert 'Menu' in bot.messages[self.user_id][-1]
Exemplo n.º 7
0
    def test_menu_trigger(self):
        node = MessageNode(text='test message')
        menu = MenuNode('💡Menu', [MenuItem('message', node)])
        bot = FakeBot(menu=menu, trigger='/menu')
        bot.user_id = self.bot_id

        self.send_message(bot, 'test')
        assert not bot.messages

        self.send_message(bot, '/menu')
        assert 'Menu' in bot.messages[self.user_id][-1]

        self.send_message(bot, '1')
        assert 'message' in bot.messages[self.user_id][-1]
        message_count = len(bot.messages[self.user_id])

        self.send_message(bot, 'test')
        assert len(bot.messages[self.user_id]) == message_count

        self.send_message(bot, 'test')
        assert len(bot.messages[self.user_id]) == message_count

        self.send_message(bot, '/menu')
        assert len(bot.messages[self.user_id]) == message_count + 1
Exemplo n.º 8
0
from instachatbot.storage import FileStorage

menu = MenuNode(
    'Is it departure or arrival?\n',
    [
        MenuItem(
            'departure 🛫',
            QuestionnaireNode(
                [
                    'Сity of arrival? 🛬'
                ],
                header='',
                admin_username='******',
                response='Gotcha! 😉')),
        MenuItem(
            'arrival 🛬',
            QuestionnaireNode(
                [
                    'Сity of departure? 🛫'
                ],
                header='',
                admin_username='******',
                response='Gotcha! 😉'))
    ],
    error_message='Hey! It\'s simple as one-two-three:\n' \
                  '1️⃣Find inspiring travel photo or video on Instagram\n' \
                  '2️⃣Share the content with me via direct message\n' \
                  '3️⃣Get a link to the cheapest flights to exact location from the content\n' \
                  'Just do it! We both know you can... 😉'
)

chatbot = InstagramChatBot(menu=menu, storage=FileStorage())