2020-01-01 20:59:22 +01:00
|
|
|
import re
|
|
|
|
|
|
|
|
from typing import Type
|
2020-01-01 15:40:42 +01:00
|
|
|
|
|
|
|
from platypush.backend import Backend
|
|
|
|
from platypush.context import get_plugin
|
2020-01-01 20:59:22 +01:00
|
|
|
from platypush.message.event.chat.telegram import MessageEvent, CommandMessageEvent, TextMessageEvent, \
|
2020-01-01 21:06:51 +01:00
|
|
|
PhotoMessageEvent, VideoMessageEvent, ContactMessageEvent, DocumentMessageEvent, LocationMessageEvent
|
2020-01-01 15:40:42 +01:00
|
|
|
from platypush.plugins.chat.telegram import ChatTelegramPlugin
|
|
|
|
|
|
|
|
|
|
|
|
class ChatTelegramBackend(Backend):
|
|
|
|
"""
|
|
|
|
Telegram bot that listens for messages and updates.
|
|
|
|
|
|
|
|
Triggers:
|
|
|
|
|
2020-01-01 20:59:22 +01:00
|
|
|
* :class:`platypush.message.event.chat.telegram.TextMessageEvent` when a text message is received.
|
|
|
|
* :class:`platypush.message.event.chat.telegram.PhotoMessageEvent` when a photo is received.
|
|
|
|
* :class:`platypush.message.event.chat.telegram.VideoMessageEvent` when a video is received.
|
2020-01-01 21:06:51 +01:00
|
|
|
* :class:`platypush.message.event.chat.telegram.LocationMessageEvent` when a location is received.
|
2020-01-01 20:59:22 +01:00
|
|
|
* :class:`platypush.message.event.chat.telegram.ContactMessageEvent` when a contact is received.
|
|
|
|
* :class:`platypush.message.event.chat.telegram.DocumentMessageEvent` when a document is received.
|
2020-01-01 17:55:19 +01:00
|
|
|
* :class:`platypush.message.event.chat.telegram.CommandMessageEvent` when a command message is received.
|
2020-01-01 15:40:42 +01:00
|
|
|
|
|
|
|
Requires:
|
|
|
|
|
|
|
|
* The :class:`platypush.plugins.chat.telegram.ChatTelegramPlugin` plugin configured
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
2020-01-01 20:59:22 +01:00
|
|
|
def __init__(self, **kwargs):
|
2020-01-01 15:40:42 +01:00
|
|
|
super().__init__(**kwargs)
|
|
|
|
self._plugin: ChatTelegramPlugin = get_plugin('chat.telegram')
|
|
|
|
|
2020-01-01 20:59:22 +01:00
|
|
|
def _msg_hook(self, cls: Type[MessageEvent]):
|
2020-01-01 15:40:42 +01:00
|
|
|
# noinspection PyUnusedLocal
|
|
|
|
def hook(update, context):
|
2020-01-01 20:59:22 +01:00
|
|
|
self.bus.post(cls(chat_id=update.effective_chat.id,
|
|
|
|
message=self._plugin.parse_msg(update.effective_message).output,
|
|
|
|
user=self._plugin.parse_user(update.effective_user).output))
|
|
|
|
|
2020-01-01 15:40:42 +01:00
|
|
|
return hook
|
|
|
|
|
2020-01-01 20:59:22 +01:00
|
|
|
def _command_hook(self):
|
2020-01-01 15:40:42 +01:00
|
|
|
# noinspection PyUnusedLocal
|
|
|
|
def hook(update, context):
|
2020-01-01 20:59:22 +01:00
|
|
|
msg = update.effective_message
|
2020-01-01 22:33:57 +01:00
|
|
|
m = re.match('\s*/([0-9a-zA-Z_-]+)\s*(.*)', msg.text)
|
2020-01-01 20:59:22 +01:00
|
|
|
cmd = m.group(1).lower()
|
|
|
|
args = [arg for arg in re.split('\s+', m.group(2)) if len(arg)]
|
|
|
|
self.bus.post(CommandMessageEvent(chat_id=update.effective_chat.id,
|
|
|
|
command=cmd,
|
|
|
|
cmdargs=args,
|
|
|
|
message=self._plugin.parse_msg(msg).output,
|
2020-01-01 17:55:19 +01:00
|
|
|
user=self._plugin.parse_user(update.effective_user).output))
|
2020-01-01 15:40:42 +01:00
|
|
|
|
|
|
|
return hook
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
# noinspection PyPackageRequirements
|
2020-01-01 20:59:22 +01:00
|
|
|
from telegram.ext import MessageHandler, Filters
|
2020-01-01 15:40:42 +01:00
|
|
|
|
|
|
|
super().run()
|
|
|
|
telegram = self._plugin.get_telegram()
|
|
|
|
dispatcher = telegram.dispatcher
|
|
|
|
|
2020-01-01 20:59:22 +01:00
|
|
|
dispatcher.add_handler(MessageHandler(Filters.text, self._msg_hook(TextMessageEvent)))
|
|
|
|
dispatcher.add_handler(MessageHandler(Filters.photo, self._msg_hook(PhotoMessageEvent)))
|
|
|
|
dispatcher.add_handler(MessageHandler(Filters.video, self._msg_hook(VideoMessageEvent)))
|
2020-01-01 21:06:51 +01:00
|
|
|
dispatcher.add_handler(MessageHandler(Filters.contact, self._msg_hook(ContactMessageEvent)))
|
|
|
|
dispatcher.add_handler(MessageHandler(Filters.location, self._msg_hook(LocationMessageEvent)))
|
2020-01-01 20:59:22 +01:00
|
|
|
dispatcher.add_handler(MessageHandler(Filters.document, self._msg_hook(DocumentMessageEvent)))
|
|
|
|
dispatcher.add_handler(MessageHandler(Filters.command, self._command_hook()))
|
2020-01-01 15:40:42 +01:00
|
|
|
|
|
|
|
self.logger.info('Initialized Telegram backend')
|
|
|
|
telegram.start_polling()
|
|
|
|
|
|
|
|
|
|
|
|
# vim:sw=4:ts=4:et:
|