From ddc9c8ff76ee6675e138250763903cffe8899ed7 Mon Sep 17 00:00:00 2001 From: Bohdan Horbeshko Date: Mon, 15 Aug 2022 06:51:09 -0400 Subject: [PATCH] Make commands not bound to a certain chat available as transport commands too --- telegram/commands.go | 168 ++++++++++++++++++++++++++----------------- 1 file changed, 102 insertions(+), 66 deletions(-) diff --git a/telegram/commands.go b/telegram/commands.go index 55267ab..95afd8a 100644 --- a/telegram/commands.go +++ b/telegram/commands.go @@ -52,6 +52,10 @@ var transportCommands = map[string]command{ "setpassword": command{"[old] [new]", "set or remove password"}, "config": command{"[param] [value]", "view or update configuration options"}, "report": command{"[chat] [comment]", "report a chat by id or @username"}, + "add": command{"@username", "add @username to your chat list"}, + "join": command{"https://t.me/invite_link", "join to chat via invite link or @publicname"}, + "supergroup": command{"title description", "create new supergroup «title» with «description»"}, + "channel": command{"title description", "create new channel «title» with «description»"}, } var chatCommands = map[string]command{ @@ -395,6 +399,14 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string } else { return "Reported" } + case "add": + return c.cmdAdd(args) + case "join": + return c.cmdJoin(args) + case "supergroup": + return c.cmdSupergroup(args, cmdline) + case "channel": + return c.cmdChannel(args, cmdline) case "help": return helpString(helpTypeTransport) } @@ -616,78 +628,16 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) } // add @contact case "add": - if len(args) < 1 { - return notEnoughArguments, true - } - - chat, err := c.client.SearchPublicChat(&client.SearchPublicChatRequest{ - Username: args[0], - }) - if err != nil { - return err.Error(), true - } - if chat == nil { - return "No error, but chat is nil", true - } - - c.subscribeToID(chat.Id, chat) + return c.cmdAdd(args), true // join https://t.me/publichat or @publicchat case "join": - if len(args) < 1 { - return notEnoughArguments, true - } - - if strings.HasPrefix(args[0], "@") { - chat, err := c.client.SearchPublicChat(&client.SearchPublicChatRequest{ - Username: args[0], - }) - if err != nil { - return err.Error(), true - } - if chat == nil { - return "No error, but chat is nil", true - } - _, err = c.client.JoinChat(&client.JoinChatRequest{ - ChatId: chat.Id, - }) - if err != nil { - return err.Error(), true - } - } else { - _, err := c.client.JoinChatByInviteLink(&client.JoinChatByInviteLinkRequest{ - InviteLink: args[0], - }) - if err != nil { - return err.Error(), true - } - } + return c.cmdJoin(args), true // create new supergroup case "supergroup": - if len(args) < 1 { - return notEnoughArguments, true - } - - _, err := c.client.CreateNewSupergroupChat(&client.CreateNewSupergroupChatRequest{ - Title: args[0], - Description: rawCmdArguments(cmdline, 1), - }) - if err != nil { - return err.Error(), true - } + return c.cmdSupergroup(args, cmdline), true // create new channel case "channel": - if len(args) < 1 { - return notEnoughArguments, true - } - - _, err := c.client.CreateNewSupergroupChat(&client.CreateNewSupergroupChatRequest{ - Title: args[0], - Description: rawCmdArguments(cmdline, 1), - IsChannel: true, - }) - if err != nil { - return err.Error(), true - } + return c.cmdChannel(args, cmdline), true // create new secret chat with current user case "secret": _, err := c.client.CreateNewSecretChat(&client.CreateNewSecretChatRequest{ @@ -1084,3 +1034,89 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) return "", true } + +func (c *Client) cmdAdd(args []string) string { + if len(args) < 1 { + return notEnoughArguments + } + + chat, err := c.client.SearchPublicChat(&client.SearchPublicChatRequest{ + Username: args[0], + }) + if err != nil { + return err.Error() + } + if chat == nil { + return "No error, but chat is nil" + } + + c.subscribeToID(chat.Id, chat) + + return "" +} + +func (c *Client) cmdJoin(args []string) string { + if len(args) < 1 { + return notEnoughArguments + } + + if strings.HasPrefix(args[0], "@") { + chat, err := c.client.SearchPublicChat(&client.SearchPublicChatRequest{ + Username: args[0], + }) + if err != nil { + return err.Error() + } + if chat == nil { + return "No error, but chat is nil" + } + _, err = c.client.JoinChat(&client.JoinChatRequest{ + ChatId: chat.Id, + }) + if err != nil { + return err.Error() + } + } else { + _, err := c.client.JoinChatByInviteLink(&client.JoinChatByInviteLinkRequest{ + InviteLink: args[0], + }) + if err != nil { + return err.Error() + } + } + + return "" +} + +func (c *Client) cmdSupergroup(args []string, cmdline string) string { + if len(args) < 1 { + return notEnoughArguments + } + + _, err := c.client.CreateNewSupergroupChat(&client.CreateNewSupergroupChatRequest{ + Title: args[0], + Description: rawCmdArguments(cmdline, 1), + }) + if err != nil { + return err.Error() + } + + return "" +} + +func (c *Client) cmdChannel(args []string, cmdline string) string { + if len(args) < 1 { + return notEnoughArguments + } + + _, err := c.client.CreateNewSupergroupChat(&client.CreateNewSupergroupChatRequest{ + Title: args[0], + Description: rawCmdArguments(cmdline, 1), + IsChannel: true, + }) + if err != nil { + return err.Error() + } + + return "" +}