From ebc2f244d75edf338142f9f023d984b3c9605a67 Mon Sep 17 00:00:00 2001
From: Bohdan Horbeshko <bodqhrohro@gmail.com>
Date: Wed, 22 Jun 2022 14:03:18 -0400
Subject: Support /join @publicgroup syntax

---
 telegram/commands.go | 32 +++++++++++++++++++++++++-------
 1 file changed, 25 insertions(+), 7 deletions(-)

(limited to 'telegram')

diff --git a/telegram/commands.go b/telegram/commands.go
index b2396b8..bd9f0f8 100644
--- a/telegram/commands.go
+++ b/telegram/commands.go
@@ -61,7 +61,7 @@ var chatCommands = map[string]command{
 	"schedule":   command{"{online | 2006-01-02T15:04:05 | 15:04:05} message", "schedules a message either to timestamp or to whenever the user goes online"},
 	"forward":    command{"message_id target_chat", "forwards a message"},
 	"add":        command{"@username", "add @username to your chat list"},
-	"join":       command{"https://t.me/invite_link", "join to chat via invite link"},
+	"join":       command{"https://t.me/invite_link", "join to chat via invite link or @publicname"},
 	"group":      command{"title", "create groupchat «title» with current user"},
 	"supergroup": command{"title description", "create new supergroup «title» with «description»"},
 	"channel":    command{"title description", "create new channel «title» with «description»"},
@@ -623,17 +623,35 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
 		}
 
 		c.subscribeToID(chat.Id, chat)
-	// join https://t.me/publichat
+	// join https://t.me/publichat or @publicchat
 	case "join":
 		if len(args) < 1 {
 			return notEnoughArguments, true
 		}
 
-		_, err := c.client.JoinChatByInviteLink(&client.JoinChatByInviteLinkRequest{
-			InviteLink: args[0],
-		})
-		if err != nil {
-			return err.Error(), 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
+			}
 		}
 	// create new supergroup
 	case "supergroup":
-- 
cgit v1.2.3