Python telepot.loop() Examples

The following are 1 code examples of telepot.loop(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module telepot , or try the search function .
Example #1
Source File: telegram_bot_client.py    From cakechat with Apache License 2.0 5 votes vote down vote up
def run(self, session_class, **session_kwargs):
        """
        :param session_class: subclass of AbstractTelegramChat
        """
        self._logger.info('Started a new chat bot {}'.format(self._bot.getMe()))

        def _handler(msg):
            content_type, chat_type, chat_id = telepot.glance(msg)

            if chat_id not in self._chat_id_to_session:
                self._chat_id_to_session[chat_id] = self._init_chat_session(chat_id, session_class, **session_kwargs)

            session = self._chat_id_to_session[chat_id]

            if content_type == 'text' and msg['text'].startswith('/'):
                command, arg = self._parse_command(msg['text'])
                if command == 'start':
                    session = self._chat_id_to_session[chat_id] = self._init_chat_session(
                        chat_id, session_class, **session_kwargs)

                return session.handle_command(command, arg)

            if content_type == 'text':
                return session.handle_text_message(msg['text'], msg)

            if content_type == 'photo':
                photo_url = self._extract_photo_url(msg['photo'])
                return session.handle_photo_message(photo_url, msg)

            return session.default_handle_message(msg)

        telepot.loop.MessageLoop(self._bot, _handler).run_forever()