Make commands not bound to a certain chat available as transport commands too
This commit is contained in:
parent
e9e65b6778
commit
ddc9c8ff76
|
@ -52,6 +52,10 @@ var transportCommands = map[string]command{
|
||||||
"setpassword": command{"[old] [new]", "set or remove password"},
|
"setpassword": command{"[old] [new]", "set or remove password"},
|
||||||
"config": command{"[param] [value]", "view or update configuration options"},
|
"config": command{"[param] [value]", "view or update configuration options"},
|
||||||
"report": command{"[chat] [comment]", "report a chat by id or @username"},
|
"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{
|
var chatCommands = map[string]command{
|
||||||
|
@ -395,6 +399,14 @@ func (c *Client) ProcessTransportCommand(cmdline string, resource string) string
|
||||||
} else {
|
} else {
|
||||||
return "Reported"
|
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":
|
case "help":
|
||||||
return helpString(helpTypeTransport)
|
return helpString(helpTypeTransport)
|
||||||
}
|
}
|
||||||
|
@ -616,78 +628,16 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
|
||||||
}
|
}
|
||||||
// add @contact
|
// add @contact
|
||||||
case "add":
|
case "add":
|
||||||
if len(args) < 1 {
|
return c.cmdAdd(args), true
|
||||||
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)
|
|
||||||
// join https://t.me/publichat or @publicchat
|
// join https://t.me/publichat or @publicchat
|
||||||
case "join":
|
case "join":
|
||||||
if len(args) < 1 {
|
return c.cmdJoin(args), true
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// create new supergroup
|
// create new supergroup
|
||||||
case "supergroup":
|
case "supergroup":
|
||||||
if len(args) < 1 {
|
return c.cmdSupergroup(args, cmdline), true
|
||||||
return notEnoughArguments, true
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := c.client.CreateNewSupergroupChat(&client.CreateNewSupergroupChatRequest{
|
|
||||||
Title: args[0],
|
|
||||||
Description: rawCmdArguments(cmdline, 1),
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
return err.Error(), true
|
|
||||||
}
|
|
||||||
// create new channel
|
// create new channel
|
||||||
case "channel":
|
case "channel":
|
||||||
if len(args) < 1 {
|
return c.cmdChannel(args, cmdline), true
|
||||||
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
|
|
||||||
}
|
|
||||||
// create new secret chat with current user
|
// create new secret chat with current user
|
||||||
case "secret":
|
case "secret":
|
||||||
_, err := c.client.CreateNewSecretChat(&client.CreateNewSecretChatRequest{
|
_, err := c.client.CreateNewSecretChat(&client.CreateNewSecretChatRequest{
|
||||||
|
@ -1084,3 +1034,89 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
|
||||||
|
|
||||||
return "", true
|
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…
Reference in a new issue