From f80f407ae07adc12a90b428f0d2d7ca972a9ea9e Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sat, 27 Jan 2018 16:34:26 +0200 Subject: [PATCH] Update README and clean up new methods --- README.md | 13 ++++++++++--- mautrix_telegram/commands.py | 32 +++++++++++++++++++++++--------- mautrix_telegram/portal.py | 1 - mautrix_telegram/user.py | 21 ++++++--------------- 4 files changed, 39 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 06ce81b9..4368d23f 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # mautrix-telegram -**This is the python rewrite. If you want more features (and more bugs), check the JavaScript version on the master branch.** +**This is the python rewrite. The JavaScript version on the master branch has different features and more bugs** A Matrix-Telegram puppeting bridge. @@ -88,8 +88,8 @@ does not do this automatically. * [x] Audio messages * [x] Video messages * [x] Documents - * [ ] Message deletions - * [ ] Message edits + * [ ] Message deletions (no way to tell difference between user-specific deletion and global deletion) + * [ ] Message edits (not supported in Matrix) * [x] Avatars * [x] Presence * [x] Typing notifications @@ -114,3 +114,10 @@ does not do this automatically. * Misc * [ ] Use optional bot to relay messages for unauthenticated Matrix users * [ ] Command to upgrade chat to supergroup from Matrix +* Commands + * [x] Logging in and out + * [ ] Registering + * [ ] Searching for users + * [ ] Starting a private chat with users + * [ ] Creating a Telegram chat for an existing Matrix room + * [ ] Upgrading the chat of a portal room into a supergroup diff --git a/mautrix_telegram/commands.py b/mautrix_telegram/commands.py index b10e1d72..06808067 100644 --- a/mautrix_telegram/commands.py +++ b/mautrix_telegram/commands.py @@ -33,6 +33,8 @@ class CommandHandler: self._is_management = False self._is_portal = False + # region Utility functions for handling commands + def handle(self, room, sender, command, args, is_management, is_portal): with self.handler(sender, room, command, args, is_management, is_portal) as handle_command: handle_command(self, sender, args) @@ -70,6 +72,20 @@ class CommandHandler: html = message self.az.intent.send_notice(self._room_id, message, html=html) + # endregion + # region Command handlers + + @command_handler + def ping(self, sender, args): + if not sender.logged_in: + return self.reply("You're not logged in.") + me = sender.client.get_me() + if me: + return self.reply(f"You're logged in as @{me.username}") + else: + return self.reply("You're not logged in.") + + # region Authentication commands @command_handler def register(self, sender, args): self.reply("Not yet implemented.") @@ -160,15 +176,8 @@ class CommandHandler: return self.reply("Logged out successfully.") return self.reply("Failed to log out.") - @command_handler - def ping(self, sender, args): - if not sender.logged_in: - return self.reply("You're not logged in.") - me = sender.client.get_me() - if me: - return self.reply(f"You're logged in as @{me.username}") - else: - return self.reply("You're not logged in.") + # endregion + # region Telegram interaction commands @command_handler def search(self, sender, args): @@ -186,6 +195,8 @@ class CommandHandler: def upgrade(self, sender, args): self.reply("Not yet implemented.") + # endregion + # region Command-related commands @command_handler def cancel(self, sender, args): if sender.command_status: @@ -230,3 +241,6 @@ _**Telegram actions**: commands for using the bridge to interact with Telegram._ **upgrade** - Upgrade a normal Telegram group to a supergroup. """ return self.reply(management_status + help) + + # endregion + # endregion diff --git a/mautrix_telegram/portal.py b/mautrix_telegram/portal.py index 7c68bf98..5eb7abf7 100644 --- a/mautrix_telegram/portal.py +++ b/mautrix_telegram/portal.py @@ -300,7 +300,6 @@ class Portal: "formatted_body": formatted_body, }) - def handle_telegram_message(self, source, sender, evt): if not self.mxid: self.create_room(source, invites=[source.mxid]) diff --git a/mautrix_telegram/user.py b/mautrix_telegram/user.py index b5f3e667..94997e8e 100644 --- a/mautrix_telegram/user.py +++ b/mautrix_telegram/user.py @@ -126,24 +126,17 @@ class User: return self.client._get_response_message(request, result) - def send_file(self, entity, file, mime_type=None, caption=None, - attributes=None, file_name=None, reply_to=None): + def send_file(self, entity, file, mime_type=None, caption=None, attributes=[], file_name=None, + reply_to=None): entity = self.client.get_input_entity(entity) reply_to = self.client._get_reply_to(reply_to) file_handle = self.client.upload_file(file, file_name=file_name, use_cache=False) - print(entity, mime_type, caption) - for a in attributes: - print(a) - print(file_handle) if mime_type.startswith("image/"): media = InputMediaUploadedPhoto(file_handle, caption or "") else: - attr_dict = {} - if attributes: - for a in attributes: - attr_dict[type(a)] = a + attr_dict = {type(attr): attr for attr in attributes} media = InputMediaUploadedDocument( file=file_handle, @@ -173,11 +166,9 @@ class User: dialogs = self.client.get_dialogs(limit=30) for dialog in dialogs: entity = dialog.entity - if isinstance(entity, User): - continue - elif isinstance(entity, Chat) and entity.deactivated: - continue - elif isinstance(entity, ChannelForbidden): + if isinstance(entity, User) \ + or (isinstance(entity, Chat) and entity.deactivated) \ + or isinstance(entity, ChannelForbidden): continue portal = po.Portal.get_by_entity(entity) portal.create_room(self, entity, invites=[self.mxid])