Browse Source

Make commands not bound to a certain chat available as transport commands too

master
Bohdan Horbeshko 2 months ago
parent
commit
ddc9c8ff76
1 changed files with 102 additions and 66 deletions
  1. +102
    -66
      telegram/commands.go

+ 102
- 66
telegram/commands.go View File

@ -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 ""
}

Loading…
Cancel
Save