telegabber/telegram/commands.go

879 lines
21 KiB
Go
Raw Normal View History

2019-11-24 17:10:29 +00:00
package telegram
2019-11-26 00:04:11 +00:00
import (
2019-12-05 19:56:12 +00:00
"fmt"
2019-12-05 18:13:17 +00:00
"github.com/pkg/errors"
2019-12-04 23:10:08 +00:00
"strconv"
2019-11-26 00:04:11 +00:00
"strings"
2019-12-08 14:24:51 +00:00
"time"
"unicode"
2019-12-04 23:10:08 +00:00
"dev.narayana.im/narayana/telegabber/xmpp/gateway"
2019-12-05 23:21:39 +00:00
log "github.com/sirupsen/logrus"
2022-01-17 20:45:40 +00:00
"github.com/zelenin/go-tdlib/client"
2019-11-26 00:04:11 +00:00
)
2019-11-24 17:10:29 +00:00
const notEnoughArguments string = "Not enough arguments"
const telegramNotInitialized string = "Telegram connection is not initialized yet"
const notOnline string = "Not online"
2022-02-03 18:51:27 +00:00
var permissionsAdmin = client.ChatMemberStatusAdministrator{
CanBeEdited: true,
CanChangeInfo: true,
CanPostMessages: true,
CanEditMessages: true,
CanDeleteMessages: true,
CanInviteUsers: true,
CanRestrictMembers: true,
CanPinMessages: true,
CanPromoteMembers: false,
}
2022-02-01 04:57:17 +00:00
var permissionsMember = client.ChatPermissions{
CanSendMessages: true,
CanSendMediaMessages: true,
CanSendPolls: true,
CanSendOtherMessages: true,
CanAddWebPagePreviews: true,
CanChangeInfo: true,
CanInviteUsers: true,
CanPinMessages: true,
}
var permissionsReadonly = client.ChatPermissions{}
2019-11-24 17:10:29 +00:00
2019-11-26 00:04:11 +00:00
var transportCommands = map[string]command{
2019-12-05 18:13:17 +00:00
"login": command{"phone", "sign in"},
"logout": command{"", "sign out"},
"code": command{"", "check one-time code"},
"password": command{"", "check 2fa password"},
"setusername": command{"", "update @username"},
"setname": command{"first last", "update name"},
"setbio": command{"", "update about"},
"setpassword": command{"[old] [new]", "set or remove password"},
2019-12-05 19:56:12 +00:00
"config": command{"[param] [value]", "view or update configuration options"},
2019-11-26 00:04:11 +00:00
}
var chatCommands = map[string]command{
2019-12-08 14:24:51 +00:00
"d": command{"[n]", "delete your last message(s)"},
2022-01-24 16:02:50 +00:00
"s": command{"edited message", "edit your last message"},
2019-12-08 14:24:51 +00:00
"add": command{"@username", "add @username to your chat list"},
"join": command{"https://t.me/invite_link", "join to chat via invite link"},
"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»"},
"secret": command{"", "create secretchat with current user"},
2019-12-08 16:04:26 +00:00
"search": command{"string [limit]", "search <string> in current chat"},
2019-12-08 16:19:35 +00:00
"history": command{"[limit]", "get last [limit] messages from current chat"},
"block": command{"", "blacklist current user"},
"unblock": command{"", "unblacklist current user"},
"invite": command{"id or @username", "add user to current chat"},
"link": command{"", "get invite link for current chat"},
2019-12-08 16:19:35 +00:00
"kick": command{"id or @username", "remove user to current chat"},
2022-02-01 04:57:17 +00:00
"mute": command{"id or @username [hours]", "mute user in current chat"},
"unmute": command{"id or @username", "unrestrict user from current chat"},
2019-12-08 16:19:35 +00:00
"ban": command{"id or @username [hours]", "restrict @username from current chat for [hours] or forever"},
2022-02-03 18:51:27 +00:00
"unban": command{"id or @username", "unbans @username in current chat (and devotes from admins)"},
"promote": command{"id or @username [title]", "promote user to admin in current chat"},
2019-12-08 16:19:35 +00:00
"leave": command{"", "leave current chat"},
"leave!": command{"", "leave current chat (for owners)"},
2019-12-08 16:19:35 +00:00
"close": command{"", "close current secret chat"},
"delete": command{"", "delete current chat from chat list"},
2019-12-08 18:44:17 +00:00
"members": command{"[query]", "search members [by optional query] in current chat (requires admin rights)"},
2019-11-26 00:04:11 +00:00
}
var transportConfigurationOptions = map[string]configurationOption{
2022-01-05 21:04:22 +00:00
"timezone": configurationOption{"<timezone>", "adjust timezone for Telegram user statuses (example: +02:00)"},
"keeponline": configurationOption{"<bool>", "always keep telegram session online and rely on jabber offline messages (example: true)"},
2019-11-26 00:04:11 +00:00
}
type command struct {
arguments string
description string
}
type configurationOption command
type helpType int
const (
helpTypeTransport helpType = iota
helpTypeChat
)
func helpString(ht helpType) string {
var str strings.Builder
var commandMap map[string]command
switch ht {
case helpTypeTransport:
commandMap = transportCommands
case helpTypeChat:
commandMap = chatCommands
}
str.WriteString("Available commands:\n")
for name, command := range commandMap {
str.WriteString("/")
str.WriteString(name)
if command.arguments != "" {
str.WriteString(" ")
str.WriteString(command.arguments)
}
str.WriteString(" — ")
str.WriteString(command.description)
str.WriteString("\n")
}
if ht == helpTypeTransport {
str.WriteString("Configuration options\n")
for name, option := range transportConfigurationOptions {
str.WriteString(name)
str.WriteString(" ")
str.WriteString(option.arguments)
str.WriteString(" — ")
str.WriteString(option.description)
str.WriteString("\n")
}
}
return str.String()
}
2019-12-03 00:32:53 +00:00
func parseCommand(cmdline string) (string, []string) {
bodyFields := strings.Fields(cmdline)
return bodyFields[0][1:], bodyFields[1:]
}
func rawCmdArguments(cmdline string, start uint8) (string) {
var state uint
// /cmd ababa galamaga
// 01 2 3 45
startState := uint(3 + 2 * start)
for i, r := range cmdline {
isOdd := state % 2 == 1
isSpace := unicode.IsSpace(r)
if (!isOdd && !isSpace) || (isOdd && isSpace) {
state += 1
}
if state == startState {
return cmdline[i:]
}
}
return ""
}
func (c *Client) unsubscribe(chatID int64) error {
return gateway.SendPresence(
2019-12-08 15:35:27 +00:00
c.xmpp,
c.jid,
gateway.SPFrom(strconv.FormatInt(chatID, 10)),
gateway.SPType("unsubscribed"),
)
}
2019-12-08 16:19:35 +00:00
func (c *Client) sendMessagesReverse(chatID int64, messages []*client.Message) {
for i := len(messages) - 1; i >= 0; i-- {
gateway.SendMessage(
c.jid,
strconv.FormatInt(chatID, 10),
c.formatMessage(0, 0, false, messages[i]),
c.xmpp,
)
}
}
2021-12-04 18:10:54 +00:00
func (c *Client) usernameOrIDToID(username string) (int64, error) {
userID, err := strconv.ParseInt(username, 10, 64)
2019-12-08 13:58:17 +00:00
// couldn't parse the id, try to lookup as a username
if err != nil {
chat, err := c.client.SearchPublicChat(&client.SearchPublicChatRequest{
Username: username,
})
if err != nil {
return 0, err
}
2022-01-17 20:45:40 +00:00
userID = chat.Id
2021-12-04 18:10:54 +00:00
if userID <= 0 {
2019-12-08 13:58:17 +00:00
return 0, errors.New("Not a user")
}
}
2021-12-04 18:10:54 +00:00
return userID, nil
2019-12-08 13:58:17 +00:00
}
2019-12-03 00:32:53 +00:00
// ProcessTransportCommand executes a command sent directly to the component
// and returns a response
2022-01-03 03:54:13 +00:00
func (c *Client) ProcessTransportCommand(cmdline string, resource string) string {
2019-12-03 00:32:53 +00:00
cmd, args := parseCommand(cmdline)
2019-11-24 17:10:29 +00:00
switch cmd {
case "login", "code", "password":
if cmd == "login" && c.Session.Login != "" {
return ""
2019-11-24 17:10:29 +00:00
}
if len(args) < 1 {
return notEnoughArguments
}
if cmd == "login" {
wasSessionLoginEmpty := c.Session.Login == ""
c.Session.Login = args[0]
if wasSessionLoginEmpty && c.authorizer == nil {
go func() {
2022-01-03 03:54:13 +00:00
err := c.Connect(resource)
if err != nil {
log.Error(errors.Wrap(err, "TDlib connection failure"))
}
}()
// a quirk for authorizer to become ready. If it's still not,
// nothing bad: the command just needs to be resent again
time.Sleep(1e5)
}
}
2019-11-24 17:10:29 +00:00
if c.authorizer == nil {
return telegramNotInitialized
}
switch cmd {
2019-12-04 23:10:08 +00:00
// sign in
2019-11-24 17:10:29 +00:00
case "login":
c.authorizer.PhoneNumber <- args[0]
2019-12-04 23:10:08 +00:00
// check auth code
2019-11-24 17:10:29 +00:00
case "code":
c.authorizer.Code <- args[0]
2019-12-04 23:10:08 +00:00
// check auth password
2019-11-24 17:10:29 +00:00
case "password":
c.authorizer.Password <- args[0]
}
2019-12-04 23:10:08 +00:00
// sign out
case "logout":
if !c.Online() {
return notOnline
}
2019-12-28 02:35:40 +00:00
for _, id := range c.cache.ChatsKeys() {
2019-12-18 21:00:23 +00:00
c.unsubscribe(id)
}
2019-12-04 23:10:08 +00:00
_, err := c.client.LogOut()
if err != nil {
2019-12-18 21:00:23 +00:00
c.forceClose()
2019-12-05 18:13:17 +00:00
return errors.Wrap(err, "Logout error").Error()
2019-12-04 23:10:08 +00:00
}
c.Session.Login = ""
2019-12-05 18:13:17 +00:00
// set @username
case "setusername":
if !c.Online() {
return notOnline
}
2019-12-05 18:13:17 +00:00
var username string
if len(args) > 0 {
username = args[0]
}
_, err := c.client.SetUsername(&client.SetUsernameRequest{
Username: username,
})
if err != nil {
return errors.Wrap(err, "Couldn't set username").Error()
}
// set My Name
case "setname":
if !c.Online() {
return notOnline
}
2019-12-05 18:13:17 +00:00
var firstname string
var lastname string
if len(args) > 0 {
firstname = args[0]
}
if len(args) > 1 {
lastname = args[1]
}
_, err := c.client.SetName(&client.SetNameRequest{
FirstName: firstname,
LastName: lastname,
})
if err != nil {
return errors.Wrap(err, "Couldn't set name").Error()
}
// set About
case "setbio":
if !c.Online() {
return notOnline
}
2019-12-05 18:13:17 +00:00
_, err := c.client.SetBio(&client.SetBioRequest{
Bio: rawCmdArguments(cmdline, 0),
2019-12-05 18:13:17 +00:00
})
if err != nil {
return errors.Wrap(err, "Couldn't set bio").Error()
}
// set password
case "setpassword":
if !c.Online() {
return notOnline
}
2019-12-05 18:13:17 +00:00
var oldPassword string
var newPassword string
// 0 or 1 argument is ignored and the password is reset
if len(args) > 1 {
oldPassword = args[0]
newPassword = args[1]
}
_, err := c.client.SetPassword(&client.SetPasswordRequest{
OldPassword: oldPassword,
NewPassword: newPassword,
})
if err != nil {
return errors.Wrap(err, "Couldn't set password").Error()
}
2019-12-05 19:56:12 +00:00
case "config":
if len(args) > 1 {
value, err := c.Session.Set(args[0], args[1])
if err != nil {
return err.Error()
}
2022-01-05 21:04:22 +00:00
gateway.DirtySessions = true
2019-12-05 19:56:12 +00:00
return fmt.Sprintf("%s set to %s", args[0], value)
} else if len(args) > 0 {
value, err := c.Session.Get(args[0])
if err != nil {
return err.Error()
}
return fmt.Sprintf("%s is set to %s", args[0], value)
}
var entries []string
for key, value := range c.Session.ToMap() {
entries = append(entries, fmt.Sprintf("%s is set to %s", key, value))
}
return strings.Join(entries, "\n")
2019-11-26 00:04:11 +00:00
case "help":
return helpString(helpTypeTransport)
2019-11-24 17:10:29 +00:00
}
return ""
}
2019-12-03 00:32:53 +00:00
// ProcessChatCommand executes a command sent in a mapped chat
// and returns a response and the status of command support
2019-12-05 23:21:39 +00:00
func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) {
if !c.Online() {
return notOnline, true
}
2019-12-05 23:21:39 +00:00
cmd, args := parseCommand(cmdline)
2019-12-03 00:32:53 +00:00
switch cmd {
2022-01-06 12:13:57 +00:00
// delete message
2019-12-05 23:21:39 +00:00
case "d":
if c.me == nil {
return "@me is not initialized", true
}
2019-12-05 23:21:39 +00:00
var limit int32
if len(args) > 0 {
limit64, err := strconv.ParseInt(args[0], 10, 32)
if err != nil {
return err.Error(), true
}
limit = int32(limit64)
} else {
limit = 1
}
2022-01-17 20:45:40 +00:00
messages, err := c.getLastMessages(chatID, "", c.me.Id, limit)
2019-12-05 23:21:39 +00:00
if err != nil {
return err.Error(), true
}
log.Debugf("pre-deletion query: %#v %#v", messages, messages.Messages)
var messageIds []int64
for _, message := range messages.Messages {
if message != nil {
2022-01-17 20:45:40 +00:00
messageIds = append(messageIds, message.Id)
}
2019-12-05 23:21:39 +00:00
}
_, err = c.client.DeleteMessages(&client.DeleteMessagesRequest{
2022-01-17 20:45:40 +00:00
ChatId: chatID,
MessageIds: messageIds,
2019-12-05 23:21:39 +00:00
Revoke: true,
})
if err != nil {
return err.Error(), true
}
2022-01-06 12:13:57 +00:00
// edit message
2019-12-07 16:37:14 +00:00
case "s":
if c.me == nil {
return "@me is not initialized", true
}
if len(args) < 1 {
2019-12-07 16:37:14 +00:00
return "Not enough arguments", true
}
2022-01-17 20:45:40 +00:00
messages, err := c.getLastMessages(chatID, "", c.me.Id, 1)
2019-12-07 16:37:14 +00:00
if err != nil {
return err.Error(), true
}
if len(messages.Messages) == 0 {
return "No last message", true
}
message := messages.Messages[0]
if message == nil {
return "Last message is empty", true
}
content := c.ProcessOutgoingMessage(0, rawCmdArguments(cmdline, 0), "")
2019-12-07 16:37:14 +00:00
if content != nil {
c.client.EditMessageText(&client.EditMessageTextRequest{
2022-01-17 20:45:40 +00:00
ChatId: chatID,
MessageId: message.Id,
InputMessageContent: content,
})
} else {
return "Message processing error", true
}
2019-12-07 17:56:53 +00:00
// 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
}
gateway.SendPresence(
c.xmpp,
c.jid,
2022-01-17 20:45:40 +00:00
gateway.SPFrom(strconv.FormatInt(chat.Id, 10)),
2019-12-07 17:56:53 +00:00
gateway.SPType("subscribe"),
)
2019-12-07 21:08:12 +00:00
// join https://t.me/publichat
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
}
2019-12-07 21:26:58 +00:00
// 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),
2019-12-07 21:26:58 +00:00
})
if err != nil {
return err.Error(), 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),
2019-12-07 21:26:58 +00:00
IsChannel: true,
})
if err != nil {
return err.Error(), true
}
2019-12-07 23:36:29 +00:00
// create new secret chat with current user
case "secret":
2022-01-17 19:58:16 +00:00
_, err := c.client.CreateNewSecretChat(&client.CreateNewSecretChatRequest{
2022-01-17 20:45:40 +00:00
UserId: chatID,
2019-12-07 23:36:29 +00:00
})
if err != nil {
return err.Error(), true
}
// create group chat with current user
case "group":
if len(args) < 1 {
return notEnoughArguments, true
}
2022-01-17 19:58:16 +00:00
_, err := c.client.CreateNewBasicGroupChat(&client.CreateNewBasicGroupChatRequest{
2022-01-17 20:45:40 +00:00
UserIds: []int64{chatID},
2022-01-17 19:58:16 +00:00
Title: args[0],
})
if err != nil {
return err.Error(), true
2019-12-07 23:36:29 +00:00
}
2019-12-08 01:54:09 +00:00
// blacklists current user
case "block":
2022-01-17 19:58:16 +00:00
_, err := c.client.ToggleMessageSenderIsBlocked(&client.ToggleMessageSenderIsBlockedRequest{
2022-01-17 20:45:40 +00:00
SenderId: &client.MessageSenderUser{UserId: chatID},
2022-01-17 19:58:16 +00:00
IsBlocked: true,
})
if err != nil {
return err.Error(), true
2019-12-08 01:54:09 +00:00
}
// unblacklists current user
case "unblock":
2022-01-17 19:58:16 +00:00
_, err := c.client.ToggleMessageSenderIsBlocked(&client.ToggleMessageSenderIsBlockedRequest{
2022-01-17 20:45:40 +00:00
SenderId: &client.MessageSenderUser{UserId: chatID},
2022-01-17 19:58:16 +00:00
IsBlocked: false,
})
if err != nil {
return err.Error(), true
2019-12-08 01:54:09 +00:00
}
2019-12-08 13:32:43 +00:00
// invite @username to current groupchat
case "invite":
if len(args) < 1 {
return notEnoughArguments, true
}
2022-01-17 19:58:16 +00:00
contact, _, err := c.GetContactByUsername(args[0])
if err != nil {
return err.Error(), true
}
2019-12-08 13:32:43 +00:00
2022-01-17 19:58:16 +00:00
_, err = c.client.AddChatMember(&client.AddChatMemberRequest{
2022-01-17 20:45:40 +00:00
ChatId: chatID,
UserId: contact.Id,
2022-01-17 19:58:16 +00:00
ForwardLimit: 100,
})
if err != nil {
return err.Error(), true
2019-12-08 13:58:17 +00:00
}
// get link to current chat
case "link":
link, err := c.client.CreateChatInviteLink(&client.CreateChatInviteLinkRequest{
ChatId: chatID,
})
if err != nil {
return err.Error(), true
}
return link.InviteLink, true
2019-12-08 13:58:17 +00:00
// kick @username from current group chat
case "kick":
if len(args) < 1 {
return notEnoughArguments, true
}
2022-01-17 19:58:16 +00:00
contact, _, err := c.GetContactByUsername(args[0])
if err != nil {
return err.Error(), true
}
2019-12-08 13:58:17 +00:00
2022-01-17 19:58:16 +00:00
_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
2022-01-17 20:45:40 +00:00
ChatId: chatID,
MemberId: &client.MessageSenderUser{UserId: contact.Id},
2022-02-01 04:57:17 +00:00
Status: &client.ChatMemberStatusLeft{},
})
if err != nil {
return err.Error(), true
}
// mute @username [n hours]
case "mute":
if len(args) < 1 {
return notEnoughArguments, true
}
contact, _, err := c.GetContactByUsername(args[0])
if err != nil {
return err.Error(), true
}
var hours int64
if len(args) > 1 {
hours, err = strconv.ParseInt(args[1], 10, 32)
if err != nil {
return "Invalid number of hours", true
}
}
_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
ChatId: chatID,
MemberId: &client.MessageSenderUser{UserId: contact.Id},
Status: &client.ChatMemberStatusRestricted{
IsMember: true,
RestrictedUntilDate: c.formatBantime(hours),
Permissions: &permissionsReadonly,
},
})
if err != nil {
return err.Error(), true
}
// unmute @username
case "unmute":
if len(args) < 1 {
return notEnoughArguments, true
}
contact, _, err := c.GetContactByUsername(args[0])
if err != nil {
return err.Error(), true
}
_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
ChatId: chatID,
MemberId: &client.MessageSenderUser{UserId: contact.Id},
Status: &client.ChatMemberStatusRestricted{
IsMember: true,
RestrictedUntilDate: 0,
Permissions: &permissionsMember,
},
2022-01-17 19:58:16 +00:00
})
if err != nil {
return err.Error(), true
2019-12-08 13:32:43 +00:00
}
2019-12-08 14:24:51 +00:00
// ban @username from current chat [for N hours]
case "ban":
if len(args) < 1 {
return notEnoughArguments, true
}
2022-01-17 19:58:16 +00:00
contact, _, err := c.GetContactByUsername(args[0])
if err != nil {
return err.Error(), true
}
2019-12-08 14:24:51 +00:00
2022-01-17 19:58:16 +00:00
var hours int64
if len(args) > 1 {
hours, err = strconv.ParseInt(args[1], 10, 32)
2019-12-08 14:24:51 +00:00
if err != nil {
2022-01-17 19:58:16 +00:00
return "Invalid number of hours", true
2019-12-08 14:24:51 +00:00
}
}
2022-01-17 19:58:16 +00:00
_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
2022-01-17 20:45:40 +00:00
ChatId: chatID,
MemberId: &client.MessageSenderUser{UserId: contact.Id},
2022-02-01 04:57:17 +00:00
Status: &client.ChatMemberStatusBanned{
BannedUntilDate: c.formatBantime(hours),
},
2022-01-17 19:58:16 +00:00
})
if err != nil {
return err.Error(), true
}
2022-02-03 18:51:27 +00:00
// unban @username
case "unban":
if len(args) < 1 {
return notEnoughArguments, true
}
contact, _, err := c.GetContactByUsername(args[0])
if err != nil {
return err.Error(), true
}
_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
ChatId: chatID,
MemberId: &client.MessageSenderUser{UserId: contact.Id},
Status: &client.ChatMemberStatusMember{},
})
if err != nil {
return err.Error(), true
}
// promote @username to admin
case "promote":
if len(args) < 1 {
return notEnoughArguments, true
}
contact, _, err := c.GetContactByUsername(args[0])
if err != nil {
return err.Error(), true
}
// clone the permissions
status := permissionsAdmin
if len(args) > 1 {
status.CustomTitle = args[1]
}
_, err = c.client.SetChatMemberStatus(&client.SetChatMemberStatusRequest{
ChatId: chatID,
MemberId: &client.MessageSenderUser{UserId: contact.Id},
Status: &status,
})
if err != nil {
return err.Error(), true
}
2019-12-08 15:08:55 +00:00
// leave current chat
case "leave":
2022-01-17 19:58:16 +00:00
_, err := c.client.LeaveChat(&client.LeaveChatRequest{
2022-01-17 20:45:40 +00:00
ChatId: chatID,
2022-01-17 19:58:16 +00:00
})
2019-12-08 15:08:55 +00:00
if err != nil {
return err.Error(), true
}
err = c.unsubscribe(chatID)
if err != nil {
return err.Error(), true
}
// leave current chat (for owners)
case "leave!":
_, err := c.client.DeleteChat(&client.DeleteChatRequest{
ChatId: chatID,
})
if err != nil {
return err.Error(), true
}
err = c.unsubscribe(chatID)
if err != nil {
return err.Error(), true
}
2019-12-08 15:25:29 +00:00
// close secret chat
case "close":
chat, _, err := c.GetContactByID(chatID, nil)
if err != nil {
return err.Error(), true
}
chatType := chat.Type.ChatTypeType()
if chatType == client.TypeChatTypeSecret {
chatTypeSecret, _ := chat.Type.(*client.ChatTypeSecret)
_, err = c.client.CloseSecretChat(&client.CloseSecretChatRequest{
2022-01-17 20:45:40 +00:00
SecretChatId: chatTypeSecret.SecretChatId,
2019-12-08 15:25:29 +00:00
})
if err != nil {
return err.Error(), true
}
err = c.unsubscribe(chatID)
if err != nil {
return err.Error(), true
}
2019-12-08 15:08:55 +00:00
}
2019-12-08 15:35:27 +00:00
// delete current chat
case "delete":
_, err := c.client.DeleteChatHistory(&client.DeleteChatHistoryRequest{
2022-01-17 20:45:40 +00:00
ChatId: chatID,
2019-12-08 15:35:27 +00:00
RemoveFromChatList: true,
2022-01-17 19:58:16 +00:00
Revoke: true,
2019-12-08 15:35:27 +00:00
})
if err != nil {
return err.Error(), true
}
err = c.unsubscribe(chatID)
if err != nil {
return err.Error(), true
}
2022-01-06 12:13:57 +00:00
// message search
2019-12-08 16:04:26 +00:00
case "search":
2022-01-17 19:58:16 +00:00
var limit int32 = 100
2019-12-08 16:04:26 +00:00
if len(args) > 1 {
newLimit, err := strconv.ParseInt(args[1], 10, 32)
if err == nil {
limit = int32(newLimit)
}
}
var query string
if len(args) > 0 {
query = args[0]
}
2022-01-06 12:13:57 +00:00
messages, err := c.getLastMessages(chatID, query, 0, limit)
2019-12-08 16:04:26 +00:00
if err != nil {
return err.Error(), true
}
2019-12-08 16:19:35 +00:00
c.sendMessagesReverse(chatID, messages.Messages)
// get latest entries from history
case "history":
var limit int32 = 10
if len(args) > 0 {
newLimit, err := strconv.ParseInt(args[0], 10, 32)
if err == nil {
limit = int32(newLimit)
}
}
var messages *client.Messages
var err error
for _ = range make([]struct{}, 2) {
messages, err = c.client.GetChatHistory(&client.GetChatHistoryRequest{
2022-01-17 20:45:40 +00:00
ChatId: chatID,
Limit: limit,
})
if err != nil {
return err.Error(), true
}
// TDlib yields only the latest message on the first request
if !(len(messages.Messages) == 1 && limit > 1) {
break
}
2019-12-08 16:04:26 +00:00
}
2019-12-08 16:19:35 +00:00
c.sendMessagesReverse(chatID, messages.Messages)
2022-01-17 19:58:16 +00:00
// chat members
2019-12-08 18:44:17 +00:00
case "members":
var query string
if len(args) > 0 {
query = args[0]
}
members, err := c.client.SearchChatMembers(&client.SearchChatMembersRequest{
2022-01-17 20:45:40 +00:00
ChatId: chatID,
2019-12-08 18:44:17 +00:00
Limit: 9999,
Query: query,
Filter: &client.ChatMembersFilterMembers{},
})
if err != nil {
return err.Error(), true
}
var entries []string
for _, member := range members.Members {
2021-12-04 18:10:54 +00:00
var senderId int64
2022-01-17 20:45:40 +00:00
switch member.MemberId.MessageSenderType() {
2021-12-04 18:10:54 +00:00
case client.TypeMessageSenderUser:
2022-01-17 20:45:40 +00:00
memberUser, _ := member.MemberId.(*client.MessageSenderUser)
senderId = memberUser.UserId
2021-12-04 18:10:54 +00:00
case client.TypeMessageSenderChat:
2022-01-17 20:45:40 +00:00
memberChat, _ := member.MemberId.(*client.MessageSenderChat)
senderId = memberChat.ChatId
2021-12-04 18:10:54 +00:00
}
2019-12-08 18:44:17 +00:00
entries = append(entries, fmt.Sprintf(
"%v | role: %v",
2021-12-04 18:10:54 +00:00
c.formatContact(senderId),
2019-12-08 18:44:17 +00:00
member.Status.ChatMemberStatusType(),
))
}
2022-02-01 03:23:37 +00:00
return strings.Join(entries, "\n"), true
2019-12-03 00:32:53 +00:00
case "help":
2019-12-03 16:48:41 +00:00
return helpString(helpTypeChat), true
2019-12-05 23:21:39 +00:00
default:
return "", false
2019-12-03 00:32:53 +00:00
}
2019-12-05 23:21:39 +00:00
return "", true
2019-12-03 00:32:53 +00:00
}