diff --git a/docs/source/backends.rst b/docs/source/backends.rst index 1ab13b225..21bba17a9 100644 --- a/docs/source/backends.rst +++ b/docs/source/backends.rst @@ -56,5 +56,4 @@ Backends platypush/backend/weather.darksky.rst platypush/backend/weather.openweathermap.rst platypush/backend/wiimote.rst - platypush/backend/zwave.rst platypush/backend/zwave.mqtt.rst diff --git a/docs/source/platypush/backend/zwave.rst b/docs/source/platypush/backend/zwave.rst deleted file mode 100644 index 876654a99..000000000 --- a/docs/source/platypush/backend/zwave.rst +++ /dev/null @@ -1,5 +0,0 @@ -``zwave`` -=========================== - -.. automodule:: platypush.backend.zwave - :members: diff --git a/docs/source/platypush/plugins/zwave.rst b/docs/source/platypush/plugins/zwave.rst deleted file mode 100644 index bcf03c7ef..000000000 --- a/docs/source/platypush/plugins/zwave.rst +++ /dev/null @@ -1,5 +0,0 @@ -``zwave`` -=========================== - -.. automodule:: platypush.plugins.zwave - :members: diff --git a/docs/source/plugins.rst b/docs/source/plugins.rst index 86a68d5d3..ecb5f6d24 100644 --- a/docs/source/plugins.rst +++ b/docs/source/plugins.rst @@ -149,5 +149,4 @@ Plugins platypush/plugins/xmpp.rst platypush/plugins/zeroconf.rst platypush/plugins/zigbee.mqtt.rst - platypush/plugins/zwave.rst platypush/plugins/zwave.mqtt.rst diff --git a/platypush/backend/chat/telegram/__init__.py b/platypush/backend/chat/telegram/__init__.py index 67ca57245..c6c29aeaf 100644 --- a/platypush/backend/chat/telegram/__init__.py +++ b/platypush/backend/chat/telegram/__init__.py @@ -4,9 +4,17 @@ from typing import Type, Optional, Union, List from platypush.backend import Backend from platypush.context import get_plugin -from platypush.message.event.chat.telegram import MessageEvent, CommandMessageEvent, TextMessageEvent, \ - PhotoMessageEvent, VideoMessageEvent, ContactMessageEvent, DocumentMessageEvent, LocationMessageEvent, \ - GroupChatCreatedEvent +from platypush.message.event.chat.telegram import ( + MessageEvent, + CommandMessageEvent, + TextMessageEvent, + PhotoMessageEvent, + VideoMessageEvent, + ContactMessageEvent, + DocumentMessageEvent, + LocationMessageEvent, + GroupChatCreatedEvent, +) from platypush.plugins.chat.telegram import ChatTelegramPlugin @@ -23,7 +31,7 @@ class ChatTelegramBackend(Backend): * :class:`platypush.message.event.chat.telegram.ContactMessageEvent` when a contact is received. * :class:`platypush.message.event.chat.telegram.DocumentMessageEvent` when a document is received. * :class:`platypush.message.event.chat.telegram.CommandMessageEvent` when a command message is received. - * :class:`platypush.message.event.chat.telegram.GroupCreatedEvent` when the bot is invited to a new group. + * :class:`platypush.message.event.chat.telegram.GroupChatCreatedEvent` when the bot is invited to a new group. Requires: @@ -31,7 +39,9 @@ class ChatTelegramBackend(Backend): """ - def __init__(self, authorized_chat_ids: Optional[List[Union[str, int]]] = None, **kwargs): + def __init__( + self, authorized_chat_ids: Optional[List[Union[str, int]]] = None, **kwargs + ): """ :param authorized_chat_ids: Optional list of chat_id/user_id which are authorized to send messages to the bot. If nothing is specified then no restrictions are applied. @@ -39,40 +49,52 @@ class ChatTelegramBackend(Backend): super().__init__(**kwargs) self.authorized_chat_ids = set(authorized_chat_ids or []) - self._plugin: ChatTelegramPlugin = get_plugin('chat.telegram') + self._plugin: ChatTelegramPlugin = get_plugin('chat.telegram') # type: ignore def _authorize(self, msg): if not self.authorized_chat_ids: return if msg.chat.type == 'private' and msg.chat.id not in self.authorized_chat_ids: - self.logger.info('Received message from unauthorized chat_id {}'.format(msg.chat.id)) - self._plugin.send_message(chat_id=msg.chat.id, text='You are not allowed to send messages to this bot') + self.logger.info( + 'Received message from unauthorized chat_id %s', msg.chat.id + ) + self._plugin.send_message( + chat_id=msg.chat.id, + text='You are not allowed to send messages to this bot', + ) raise PermissionError def _msg_hook(self, cls: Type[MessageEvent]): # noinspection PyUnusedLocal - def hook(update, context): + def hook(update, _): msg = update.effective_message try: self._authorize(msg) - self.bus.post(cls(chat_id=update.effective_chat.id, - message=self._plugin.parse_msg(msg).output, - user=self._plugin.parse_user(update.effective_user).output)) + self.bus.post( + cls( + chat_id=update.effective_chat.id, + message=self._plugin.parse_msg(msg).output, + user=self._plugin.parse_user(update.effective_user).output, + ) + ) except PermissionError: pass return hook def _group_hook(self): - # noinspection PyUnusedLocal def hook(update, context): msg = update.effective_message if msg.group_chat_created: - self.bus.post(GroupChatCreatedEvent(chat_id=update.effective_chat.id, - message=self._plugin.parse_msg(msg).output, - user=self._plugin.parse_user(update.effective_user).output)) + self.bus.post( + GroupChatCreatedEvent( + chat_id=update.effective_chat.id, + message=self._plugin.parse_msg(msg).output, + user=self._plugin.parse_user(update.effective_user).output, + ) + ) elif msg.photo: self._msg_hook(PhotoMessageEvent)(update, context) elif msg.video: @@ -92,27 +114,33 @@ class ChatTelegramBackend(Backend): return hook def _command_hook(self): - # noinspection PyUnusedLocal - def hook(update, context): + def hook(update, _): msg = update.effective_message - m = re.match('\s*/([0-9a-zA-Z_-]+)\s*(.*)', msg.text) + m = re.match(r'\s*/([0-9a-zA-Z_-]+)\s*(.*)', msg.text) + if not m: + self.logger.warning('Invalid command: %s', msg.text) + return + cmd = m.group(1).lower() - args = [arg for arg in re.split('\s+', m.group(2)) if len(arg)] + args = [arg for arg in re.split(r'\s+', m.group(2)) if len(arg)] try: self._authorize(msg) - self.bus.post(CommandMessageEvent(chat_id=update.effective_chat.id, - command=cmd, - cmdargs=args, - message=self._plugin.parse_msg(msg).output, - user=self._plugin.parse_user(update.effective_user).output)) + self.bus.post( + CommandMessageEvent( + chat_id=update.effective_chat.id, + command=cmd, + cmdargs=args, + message=self._plugin.parse_msg(msg).output, + user=self._plugin.parse_user(update.effective_user).output, + ) + ) except PermissionError: pass return hook def run(self): - # noinspection PyPackageRequirements from telegram.ext import MessageHandler, Filters super().run() @@ -120,12 +148,24 @@ class ChatTelegramBackend(Backend): dispatcher = telegram.dispatcher dispatcher.add_handler(MessageHandler(Filters.group, self._group_hook())) - 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))) - dispatcher.add_handler(MessageHandler(Filters.contact, self._msg_hook(ContactMessageEvent))) - dispatcher.add_handler(MessageHandler(Filters.location, self._msg_hook(LocationMessageEvent))) - dispatcher.add_handler(MessageHandler(Filters.document, self._msg_hook(DocumentMessageEvent))) + 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)) + ) + dispatcher.add_handler( + MessageHandler(Filters.contact, self._msg_hook(ContactMessageEvent)) + ) + dispatcher.add_handler( + MessageHandler(Filters.location, self._msg_hook(LocationMessageEvent)) + ) + dispatcher.add_handler( + MessageHandler(Filters.document, self._msg_hook(DocumentMessageEvent)) + ) dispatcher.add_handler(MessageHandler(Filters.command, self._command_hook())) self.logger.info('Initialized Telegram backend') diff --git a/platypush/backend/chat/telegram/manifest.yaml b/platypush/backend/chat/telegram/manifest.yaml index 5d8d7ac2f..70698426a 100644 --- a/platypush/backend/chat/telegram/manifest.yaml +++ b/platypush/backend/chat/telegram/manifest.yaml @@ -5,7 +5,7 @@ manifest: platypush.message.event.chat.telegram.ContactMessageEvent: when a contact is received. platypush.message.event.chat.telegram.DocumentMessageEvent: when a document is received. - platypush.message.event.chat.telegram.GroupCreatedEvent: when the bot is invited + platypush.message.event.chat.telegram.GroupChatCreatedEvent: when the bot is invited to a new group. platypush.message.event.chat.telegram.LocationMessageEvent: when a location is received. diff --git a/platypush/backend/http/app/utils/__init__.py b/platypush/backend/http/app/utils/__init__.py index cad44b269..ab19ce73d 100644 --- a/platypush/backend/http/app/utils/__init__.py +++ b/platypush/backend/http/app/utils/__init__.py @@ -4,7 +4,7 @@ from .auth import ( authenticate_user_pass, get_auth_status, ) -from .bus import bus, get_message_response, send_message, send_request +from .bus import bus, send_message, send_request from .logger import logger from .routes import ( get_http_port, @@ -25,7 +25,6 @@ __all__ = [ 'get_http_port', 'get_ip_or_hostname', 'get_local_base_url', - 'get_message_response', 'get_remote_base_url', 'get_routes', 'get_streaming_routes', diff --git a/platypush/backend/http/app/utils/bus.py b/platypush/backend/http/app/utils/bus.py index 6571599e9..72f85658f 100644 --- a/platypush/backend/http/app/utils/bus.py +++ b/platypush/backend/http/app/utils/bus.py @@ -1,11 +1,9 @@ -from redis import Redis - from platypush.bus.redis import RedisBus from platypush.config import Config from platypush.context import get_backend from platypush.message import Message from platypush.message.request import Request -from platypush.utils import get_redis_conf, get_redis_queue_name_by_message +from platypush.utils import get_redis_conf, get_message_response from .logger import logger @@ -67,24 +65,3 @@ def send_request(action, wait_for_response=True, **kwargs): msg['args'] = kwargs return send_message(msg, wait_for_response=wait_for_response) - - -def get_message_response(msg): - """ - Get the response to the given message. - - :param msg: The message to get the response for. - :return: The response to the given message. - """ - redis = Redis(**bus().redis_args) - redis_queue = get_redis_queue_name_by_message(msg) - if not redis_queue: - return None - - response = redis.blpop(redis_queue, timeout=60) - if response and len(response) > 1: - response = Message.build(response[1]) - else: - response = None - - return response diff --git a/platypush/backend/http/webapp/dist/index.html b/platypush/backend/http/webapp/dist/index.html index c9864f74a..1dd27f289 100644 --- a/platypush/backend/http/webapp/dist/index.html +++ b/platypush/backend/http/webapp/dist/index.html @@ -1 +1 @@ -
Platypush provides two types of tokens:
Generate a JWT authentication token that can be used for API calls to the /execute
endpoint.
You can include the token in your requests in any of the following ways:
Authorization: Bearer
header;X-Token
header;http://site:8008/execute?token=...
for a JWT token and ...?session_token=...
for a session token; {"type":"request", "action", "...", "token":"..."}
for a JWT token, or "session_token"
for a session token. Confirm your credentials in order to generate a new JWT token.
Show session token will instead show the token cookie associated to the current session.
Platypush provides two types of tokens:
Generate a JWT authentication token that can be used for API calls to the /execute
endpoint.
You can include the token in your requests in any of the following ways:
Authorization: Bearer
header;X-Token
header;http://site:8008/execute?token=...
for a JWT token and ...?session_token=...
for a session token; {"type":"request", "action", "...", "token":"..."}
for a JWT token, or "session_token"
for a session token. Confirm your credentials in order to generate a new JWT token.
Show session token will instead show the token cookie associated to the current session.
\n Platypush provides two types of tokens:\n\n
Generate a JWT authentication token that can be used for API calls to the /execute
endpoint.
You can include the token in your requests in any of the following ways:
\n\nAuthorization: Bearer
header;X-Token
header;http://site:8008/execute?token=...
\n for a JWT token and ...?session_token=...
for a\n session token;\n {\"type\":\"request\", \"action\", \"...\", \"token\":\"...\"}
for\n a JWT token, or \"session_token\"
for a session token.\n Confirm your credentials in order to generate a new JWT token.
\n\n Show session token will instead show the token cookie associated\n to the current session.\n
\n\n Platypush provides two types of tokens:\n\n
Generate a JWT authentication token that can be used for API calls to the /execute
endpoint.
You can include the token in your requests in any of the following ways:
\n\nAuthorization: Bearer
header;X-Token
header;http://site:8008/execute?token=...
\n for a JWT token and ...?session_token=...
for a\n session token;\n {\"type\":\"request\", \"action\", \"...\", \"token\":\"...\"}
for\n a JWT token, or \"session_token\"
for a session token.\n Confirm your credentials in order to generate a new JWT token.
\n\n Show session token will instead show the token cookie associated\n to the current session.\n
\n