From 9f54309b3037fd963f711093536da1068e7093b8 Mon Sep 17 00:00:00 2001 From: bodqhrohro Date: Sun, 8 Dec 2019 15:32:43 +0200 Subject: [PATCH] Add /invite command --- telegram/commands.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/telegram/commands.go b/telegram/commands.go index b26a971..5f02c26 100644 --- a/telegram/commands.go +++ b/telegram/commands.go @@ -3,6 +3,7 @@ package telegram import ( "fmt" "github.com/pkg/errors" + "math" "regexp" "strconv" "strings" @@ -443,6 +444,37 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) return err.Error(), true } } + // invite @username to current groupchat + case "invite": + if len(args) < 1 { + return notEnoughArguments, true + } + + if chatID < 0 { + userID, err := strconv.ParseInt(args[0], 10, 32) + // couldn't parse the id, try to lookup as a username + if err != nil { + chat, err := c.client.SearchPublicChat(&client.SearchPublicChatRequest{ + Username: args[0], + }) + if err != nil { + return err.Error(), true + } + + userID = chat.Id + if userID <= 0 || userID > math.MaxInt32 { + return "Not a user", true + } + } + + _, err = c.client.AddChatMember(&client.AddChatMemberRequest{ + ChatId: chatID, + UserId: int32(userID), + }) + if err != nil { + return err.Error(), true + } + } case "help": return helpString(helpTypeChat), true default: