当前位置: 首页>>代码示例>>Python>>正文


Python Message.text方法代码示例

本文整理汇总了Python中telegram.Message.text方法的典型用法代码示例。如果您正苦于以下问题:Python Message.text方法的具体用法?Python Message.text怎么用?Python Message.text使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在telegram.Message的用法示例。


在下文中一共展示了Message.text方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_conversation_timeout_keeps_extending

# 需要导入模块: from telegram import Message [as 别名]
# 或者: from telegram.Message import text [as 别名]
    def test_conversation_timeout_keeps_extending(self, dp, bot, user1):
        handler = ConversationHandler(entry_points=self.entry_points, states=self.states,
                                      fallbacks=self.fallbacks, conversation_timeout=0.5)
        dp.add_handler(handler)

        # Start state machine, wait, do something, verify the timeout is extended.
        # t=0 /start (timeout=.5)
        # t=.25 /brew (timeout=.75)
        # t=.5 original timeout
        # t=.6 /pourCoffee (timeout=1.1)
        # t=.75 second timeout
        # t=1.1 actual timeout
        message = Message(0, user1, None, self.group, text='/start', bot=bot)
        dp.process_update(Update(update_id=0, message=message))
        assert handler.conversations.get((self.group.id, user1.id)) == self.THIRSTY
        sleep(0.25)  # t=.25
        dp.job_queue.tick()
        assert handler.conversations.get((self.group.id, user1.id)) == self.THIRSTY
        message.text = '/brew'
        dp.process_update(Update(update_id=0, message=message))
        assert handler.conversations.get((self.group.id, user1.id)) == self.BREWING
        sleep(0.35)  # t=.6
        dp.job_queue.tick()
        assert handler.conversations.get((self.group.id, user1.id)) == self.BREWING
        message.text = '/pourCoffee'
        dp.process_update(Update(update_id=0, message=message))
        assert handler.conversations.get((self.group.id, user1.id)) == self.DRINKING
        sleep(.4)  # t=1
        dp.job_queue.tick()
        assert handler.conversations.get((self.group.id, user1.id)) == self.DRINKING
        sleep(.1)  # t=1.1
        dp.job_queue.tick()
        assert handler.conversations.get((self.group.id, user1.id)) is None
开发者ID:technoekat,项目名称:prodeda1,代码行数:35,代码来源:test_conversationhandler.py

示例2: test_conversation_handler_fallback

# 需要导入模块: from telegram import Message [as 别名]
# 或者: from telegram.Message import text [as 别名]
    def test_conversation_handler_fallback(self, dp, bot, user1, user2):
        handler = ConversationHandler(entry_points=self.entry_points, states=self.states,
                                      fallbacks=self.fallbacks)
        dp.add_handler(handler)

        # first check if fallback will not trigger start when not started
        message = Message(0, user1, None, self.group, text='/eat', bot=bot)
        dp.process_update(Update(update_id=0, message=message))
        with pytest.raises(KeyError):
            self.current_state[user1.id]

        # User starts the state machine.
        message.text = '/start'
        dp.process_update(Update(update_id=0, message=message))
        assert self.current_state[user1.id] == self.THIRSTY

        # The user is thirsty and wants to brew coffee.
        message.text = '/brew'
        dp.process_update(Update(update_id=0, message=message))
        assert self.current_state[user1.id] == self.BREWING

        # Now a fallback command is issued
        message.text = '/eat'
        dp.process_update(Update(update_id=0, message=message))
        assert self.current_state[user1.id] == self.THIRSTY
开发者ID:technoekat,项目名称:prodeda1,代码行数:27,代码来源:test_conversationhandler.py

示例3: test_CommandHandler_commandList

# 需要导入模块: from telegram import Message [as 别名]
# 或者: from telegram.Message import text [as 别名]
    def test_CommandHandler_commandList(self):
        self._setup_updater('', messages=0)
        handler = CommandHandler(['foo', 'bar', 'spameggs'], self.telegramHandlerTest)
        self.updater.dispatcher.add_handler(handler)
        bot = self.updater.bot
        user = User(0, 'TestUser')
        queue = self.updater.start_polling(0.01)

        message = Message(0, user, 0, None, text='/foo', bot=bot)
        queue.put(Update(0, message=message))
        sleep(.1)
        self.assertEqual(self.received_message, '/foo')

        message.text = '/bar'
        queue.put(Update(1, message=message))
        sleep(.1)
        self.assertEqual(self.received_message, '/bar')

        message.text = '/spameggs'
        queue.put(Update(2, message=message))
        sleep(.1)
        self.assertEqual(self.received_message, '/spameggs')

        self.reset()
        message.text = '/not_in_list'
        queue.put(Update(3, message=message))
        sleep(.1)
        self.assertTrue(self.received_message is None)
开发者ID:KenOokamiHoro,项目名称:python-telegram-bot,代码行数:30,代码来源:test_updater.py

示例4: test_conversation_handler

# 需要导入模块: from telegram import Message [as 别名]
# 或者: from telegram.Message import text [as 别名]
    def test_conversation_handler(self, dp, bot, user1, user2):
        handler = ConversationHandler(entry_points=self.entry_points, states=self.states,
                                      fallbacks=self.fallbacks)
        dp.add_handler(handler)

        # User one, starts the state machine.
        message = Message(0, user1, None, self.group, text='/start', bot=bot)
        dp.process_update(Update(update_id=0, message=message))
        assert self.current_state[user1.id] == self.THIRSTY

        # The user is thirsty and wants to brew coffee.
        message.text = '/brew'
        dp.process_update(Update(update_id=0, message=message))
        assert self.current_state[user1.id] == self.BREWING

        # Lets see if an invalid command makes sure, no state is changed.
        message.text = '/nothing'
        dp.process_update(Update(update_id=0, message=message))
        assert self.current_state[user1.id] == self.BREWING

        # Lets see if the state machine still works by pouring coffee.
        message.text = '/pourCoffee'
        dp.process_update(Update(update_id=0, message=message))
        assert self.current_state[user1.id] == self.DRINKING

        # Let's now verify that for another user, who did not start yet,
        # the state has not been changed.
        message.from_user = user2
        dp.process_update(Update(update_id=0, message=message))
        with pytest.raises(KeyError):
            self.current_state[user2.id]
开发者ID:technoekat,项目名称:prodeda1,代码行数:33,代码来源:test_conversationhandler.py

示例5: test_webhook_no_ssl

# 需要导入模块: from telegram import Message [as 别名]
# 或者: from telegram.Message import text [as 别名]
    def test_webhook_no_ssl(self):
        print("Testing Webhook without SSL")
        bot = MockBot("", messages=0)
        self.updater.bot = bot
        d = self.updater.dispatcher
        d.addTelegramMessageHandler(self.telegramHandlerTest)

        # Select random port for travis
        port = randrange(1024, 49152)
        self.updater.start_webhook("127.0.0.1", port)
        sleep(0.5)

        # Now, we send an update to the server via urlopen
        message = Message(1, User(1, "Tester 2"), datetime.now(), GroupChat(1, "Test Group 2"))

        message.text = "Webhook Test 2"
        update = Update(1)
        update.message = message

        try:
            payload = bytes(update.to_json(), encoding="utf-8")
        except TypeError:
            payload = bytes(update.to_json())

        header = {"content-type": "application/json", "content-length": str(len(payload))}

        r = Request("http://127.0.0.1:%d/" % port, data=payload, headers=header)

        urlopen(r)
        sleep(1)
        self.assertEqual(self.received_message, "Webhook Test 2")
开发者ID:gisho,项目名称:python-telegram-bot,代码行数:33,代码来源:test_updater.py

示例6: test_webhook

# 需要导入模块: from telegram import Message [as 别名]
# 或者: from telegram.Message import text [as 别名]
    def test_webhook(self):
        print('Testing Webhook')
        self._setup_updater('', messages=0)
        d = self.updater.dispatcher
        d.addTelegramMessageHandler(
            self.telegramHandlerTest)

        # Select random port for travis
        port = randrange(1024, 49152)
        self.updater.start_webhook('127.0.0.1', port,
                                   url_path='TOKEN',
                                   cert='./tests/test_updater.py',
                                   key='./tests/test_updater.py')
        sleep(0.5)
        # SSL-Wrapping will fail, so we start the server without SSL
        Thread(target=self.updater.httpd.serve_forever).start()

        # Now, we send an update to the server via urlopen
        message = Message(1, User(1, "Tester"), datetime.now(),
                          Chat(1, "group", title="Test Group"))

        message.text = "Webhook Test"
        update = Update(1)
        update.message = message

        try:
            payload = bytes(update.to_json(), encoding='utf-8')
        except TypeError:
            payload = bytes(update.to_json())

        header = {
            'content-type': 'application/json',
            'content-length': str(len(payload))
        }

        r = Request('http://127.0.0.1:%d/TOKEN' % port,
                    data=payload,
                    headers=header)

        urlopen(r)

        sleep(1)
        self.assertEqual(self.received_message, 'Webhook Test')

        print("Test other webhook server functionalities...")
        request = Request('http://localhost:%d/webookhandler.py' % port)
        response = urlopen(request)
        self.assertEqual(b'', response.read())
        self.assertEqual(200, response.code)

        request.get_method = lambda: 'HEAD'

        response = urlopen(request)
        self.assertEqual(b'', response.read())
        self.assertEqual(200, response.code)

        # Test multiple shutdown() calls
        self.updater.httpd.shutdown()
        self.updater.httpd.shutdown()
        self.assertTrue(True)
开发者ID:azigran,项目名称:python-telegram-bot,代码行数:62,代码来源:test_updater.py

示例7: test_conversation_handler_end

# 需要导入模块: from telegram import Message [as 别名]
# 或者: from telegram.Message import text [as 别名]
    def test_conversation_handler_end(self, caplog, dp, bot, user1):
        handler = ConversationHandler(entry_points=self.entry_points, states=self.states,
                                      fallbacks=self.fallbacks)
        dp.add_handler(handler)

        message = Message(0, user1, None, self.group, text='/start', bot=bot)
        dp.process_update(Update(update_id=0, message=message))
        message.text = '/brew'
        dp.process_update(Update(update_id=0, message=message))
        message.text = '/pourCoffee'
        dp.process_update(Update(update_id=0, message=message))
        message.text = '/end'
        with caplog.at_level(logging.ERROR):
            dp.process_update(Update(update_id=0, message=message))
        assert len(caplog.records) == 0
        assert self.current_state[user1.id] == self.END
        with pytest.raises(KeyError):
            print(handler.conversations[(self.group.id, user1.id)])
开发者ID:technoekat,项目名称:prodeda1,代码行数:20,代码来源:test_conversationhandler.py

示例8: mockUpdate

# 需要导入模块: from telegram import Message [as 别名]
# 或者: from telegram.Message import text [as 别名]
    def mockUpdate(self, text):
        message = Message(0, None, None, None)
        message.text = text
        update = Update(0)

        if self.edited:
            update.edited_message = message
        else:
            update.message = message

        return update
开发者ID:Clifford-Beta,项目名称:python-telegram-bot,代码行数:13,代码来源:test_updater.py

示例9: mock_update

# 需要导入模块: from telegram import Message [as 别名]
# 或者: from telegram.Message import text [as 别名]
    def mock_update(self, text):
        message = Message(0, User(0, 'Testuser'), None, Chat(0, Chat.GROUP), bot=self)
        message.text = text
        update = Update(0)

        if self.edited:
            update.edited_message = message
        else:
            update.message = message

        return update
开发者ID:KenOokamiHoro,项目名称:python-telegram-bot,代码行数:13,代码来源:test_updater.py

示例10: test_addRemoveTelegramCommandHandler

# 需要导入模块: from telegram import Message [as 别名]
# 或者: from telegram.Message import text [as 别名]
    def test_addRemoveTelegramCommandHandler(self):
        self._setup_updater('', messages=0)
        d = self.updater.dispatcher
        handler = CommandHandler('test', self.telegramHandlerTest)
        self.updater.dispatcher.add_handler(handler)
        user = User(first_name="singelton", id=404)
        bot = self.updater.bot
        queue = self.updater.start_polling(0.01)

        # regular use
        message = Message(0, user, None, None, text="/test", bot=bot)
        queue.put(Update(update_id=0, message=message))
        sleep(.1)
        self.assertEqual(self.received_message, '/test')

        # assigned use
        message = Message(0, user, None, None, text="/[email protected]", bot=bot)
        queue.put(Update(update_id=0, message=message))
        sleep(.1)
        self.assertEqual(self.received_message, '/[email protected]MockBot')
        message.text = "/[email protected]"
        queue.put(Update(update_id=0, message=message))
        sleep(.1)
        self.assertEqual(self.received_message, '/[email protected]')

        # directed at other bot
        self.reset()
        message = Message(0, user, None, None, text="/[email protected]", bot=bot)
        queue.put(Update(update_id=0, message=message))
        sleep(.1)
        self.assertTrue(None is self.received_message)

        # case insensitivity
        self.reset()
        message = Message(0, user, None, None, text="/Test", bot=bot)
        queue.put(Update(update_id=0, message=message))
        sleep(.1)
        self.assertTrue(self.received_message, '/Test')
        handler = CommandHandler('Test', self.telegramHandlerTest)
        self.updater.dispatcher.add_handler(handler)
        message = Message(0, user, None, None, text="/test", bot=bot)
        queue.put(Update(update_id=0, message=message))
        sleep(.1)
        self.assertTrue(self.received_message, '/test')

        # Remove handler
        d.remove_handler(handler)
        handler = CommandHandler('test', self.telegramHandlerEditedTest, allow_edited=False)
        d.add_handler(handler)
        self.reset()

        self.updater.bot.send_messages = 1
        sleep(.1)
        self.assertTrue(None is self.received_message)
开发者ID:KenOokamiHoro,项目名称:python-telegram-bot,代码行数:56,代码来源:test_updater.py

示例11: test_webhook

# 需要导入模块: from telegram import Message [as 别名]
# 或者: from telegram.Message import text [as 别名]
    def test_webhook(self):
        print("Testing Webhook")
        bot = MockBot("", messages=0)
        self.updater.bot = bot
        d = self.updater.dispatcher
        d.addTelegramMessageHandler(self.telegramHandlerTest)

        # Select random port for travis
        port = randrange(1024, 49152)
        self.updater.start_webhook(
            "127.0.0.1", port, url_path="TOKEN", cert="./tests/test_updater.py", key="./tests/test_updater.py"
        )
        sleep(0.5)
        # SSL-Wrapping will fail, so we start the server without SSL
        Thread(target=self.updater.httpd.serve_forever).start()

        # Now, we send an update to the server via urlopen
        message = Message(1, User(1, "Tester"), datetime.now(), GroupChat(1, "Test Group"))

        message.text = "Webhook Test"
        update = Update(1)
        update.message = message

        try:
            payload = bytes(update.to_json(), encoding="utf-8")
        except TypeError:
            payload = bytes(update.to_json())

        header = {"content-type": "application/json", "content-length": str(len(payload))}

        r = Request("http://127.0.0.1:%d/TOKEN" % port, data=payload, headers=header)

        urlopen(r)

        sleep(1)
        self.assertEqual(self.received_message, "Webhook Test")

        print("Test other webhook server functionalities...")
        request = Request("http://localhost:%d/webookhandler.py" % port)
        response = urlopen(request)
        self.assertEqual(b"", response.read())
        self.assertEqual(200, response.code)

        request.get_method = lambda: "HEAD"

        response = urlopen(request)
        self.assertEqual(b"", response.read())
        self.assertEqual(200, response.code)

        # Test multiple shutdown() calls
        self.updater.httpd.shutdown()
        self.updater.httpd.shutdown()
        self.assertTrue(True)
开发者ID:gisho,项目名称:python-telegram-bot,代码行数:55,代码来源:test_updater.py

示例12: test_conversation_timeout_two_users

# 需要导入模块: from telegram import Message [as 别名]
# 或者: from telegram.Message import text [as 别名]
    def test_conversation_timeout_two_users(self, dp, bot, user1, user2):
        handler = ConversationHandler(entry_points=self.entry_points, states=self.states,
                                      fallbacks=self.fallbacks, conversation_timeout=0.5)
        dp.add_handler(handler)

        # Start state machine, do something as second user, then reach timeout
        message = Message(0, user1, None, self.group, text='/start', bot=bot)
        dp.process_update(Update(update_id=0, message=message))
        assert handler.conversations.get((self.group.id, user1.id)) == self.THIRSTY
        message.text = '/brew'
        message.from_user = user2
        dp.job_queue.tick()
        dp.process_update(Update(update_id=0, message=message))
        assert handler.conversations.get((self.group.id, user2.id)) is None
        message.text = '/start'
        dp.job_queue.tick()
        dp.process_update(Update(update_id=0, message=message))
        assert handler.conversations.get((self.group.id, user2.id)) == self.THIRSTY
        sleep(0.5)
        dp.job_queue.tick()
        assert handler.conversations.get((self.group.id, user1.id)) is None
        assert handler.conversations.get((self.group.id, user2.id)) is None
开发者ID:technoekat,项目名称:prodeda1,代码行数:24,代码来源:test_conversationhandler.py

示例13: test_webhook

# 需要导入模块: from telegram import Message [as 别名]
# 或者: from telegram.Message import text [as 别名]
    def test_webhook(self):
        self._setup_updater('', messages=0)
        d = self.updater.dispatcher
        handler = MessageHandler([], self.telegramHandlerTest)
        d.addHandler(handler)

        ip = '127.0.0.1'
        port = randrange(1024, 49152)  # Select random port for travis
        self.updater.start_webhook(ip,
                                   port,
                                   url_path='TOKEN',
                                   cert='./tests/test_updater.py',
                                   key='./tests/test_updater.py',
                                   webhook_url=None)
        sleep(0.5)
        # SSL-Wrapping will fail, so we start the server without SSL
        Thread(target=self.updater.httpd.serve_forever).start()

        # Now, we send an update to the server via urlopen
        message = Message(1,
                          User(1, "Tester"),
                          datetime.now(),
                          Chat(1, "group", title="Test Group"))

        message.text = "Webhook Test"
        update = Update(1)
        update.message = message

        self._send_webhook_msg(ip, port, update.to_json(), 'TOKEN')

        sleep(1)
        self.assertEqual(self.received_message, 'Webhook Test')

        print("Test other webhook server functionalities...")
        response = self._send_webhook_msg(ip, port, None, 'webookhandler.py')
        self.assertEqual(b'', response.read())
        self.assertEqual(200, response.code)

        response = self._send_webhook_msg(
            ip, port,
            None, 'webookhandler.py',
            get_method=lambda: 'HEAD')

        self.assertEqual(b'', response.read())
        self.assertEqual(200, response.code)

        # Test multiple shutdown() calls
        self.updater.httpd.shutdown()
        self.updater.httpd.shutdown()
        self.assertTrue(True)
开发者ID:Gurzo,项目名称:python-telegram-bot,代码行数:52,代码来源:test_updater.py

示例14: test_conversation_handler_per_chat

# 需要导入模块: from telegram import Message [as 别名]
# 或者: from telegram.Message import text [as 别名]
    def test_conversation_handler_per_chat(self, dp, bot, user1, user2):
        handler = ConversationHandler(
            entry_points=self.entry_points,
            states=self.states,
            fallbacks=self.fallbacks,
            per_user=False)
        dp.add_handler(handler)

        # User one, starts the state machine.
        message = Message(0, user1, None, self.group, text='/start', bot=bot)
        dp.process_update(Update(update_id=0, message=message))

        # The user is thirsty and wants to brew coffee.
        message.text = '/brew'
        dp.process_update(Update(update_id=0, message=message))

        # Let's now verify that for another user, who did not start yet,
        # the state will be changed because they are in the same group.
        message.from_user = user2
        message.text = '/pourCoffee'
        dp.process_update(Update(update_id=0, message=message))

        assert handler.conversations[(self.group.id,)] == self.DRINKING
开发者ID:technoekat,项目名称:prodeda1,代码行数:25,代码来源:test_conversationhandler.py

示例15: test_conversation_handler_per_user

# 需要导入模块: from telegram import Message [as 别名]
# 或者: from telegram.Message import text [as 别名]
    def test_conversation_handler_per_user(self, dp, bot, user1):
        handler = ConversationHandler(
            entry_points=self.entry_points,
            states=self.states,
            fallbacks=self.fallbacks,
            per_chat=False)
        dp.add_handler(handler)

        # User one, starts the state machine.
        message = Message(0, user1, None, self.group, text='/start', bot=bot)
        dp.process_update(Update(update_id=0, message=message))

        # The user is thirsty and wants to brew coffee.
        message.text = '/brew'
        dp.process_update(Update(update_id=0, message=message))

        # Let's now verify that for the same user in a different group, the state will still be
        # updated
        message.chat = self.second_group
        message.text = '/pourCoffee'
        dp.process_update(Update(update_id=0, message=message))

        assert handler.conversations[(user1.id,)] == self.DRINKING
开发者ID:technoekat,项目名称:prodeda1,代码行数:25,代码来源:test_conversationhandler.py


注:本文中的telegram.Message.text方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。