Add delete command

This commit is contained in:
bodqhrohro 2019-12-06 01:21:39 +02:00
parent cb8e7f4fef
commit 0013baa247
2 changed files with 48 additions and 5 deletions

View file

@ -8,6 +8,7 @@ import (
"dev.narayana.im/narayana/telegabber/xmpp/gateway" "dev.narayana.im/narayana/telegabber/xmpp/gateway"
log "github.com/sirupsen/logrus"
"github.com/zelenin/go-tdlib/client" "github.com/zelenin/go-tdlib/client"
) )
@ -27,7 +28,7 @@ var transportCommands = map[string]command{
} }
var chatCommands = map[string]command{ var chatCommands = map[string]command{
//"d": command{"[n]", "delete your last message(s)"}, "d": command{"[n]", "delete your last message(s)"},
//"s": command{"regex replace", "edit your last message"}, //"s": command{"regex replace", "edit your last message"},
//"add": command{"@username", "add @username to your chat list"}, //"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"},
@ -242,12 +243,54 @@ func (c *Client) ProcessTransportCommand(cmdline string) string {
// ProcessChatCommand executes a command sent in a mapped chat // ProcessChatCommand executes a command sent in a mapped chat
// and returns a response and the status of command support // and returns a response and the status of command support
func (c *Client) ProcessChatCommand(cmdline string) (string, bool) { func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) {
cmd, _ := parseCommand(cmdline) cmd, args := parseCommand(cmdline)
switch cmd { switch cmd {
case "help": // delete last message(s)
return helpString(helpTypeChat), true case "d":
if c.me == nil {
return "@me is not initialized", true
}
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
} }
messages, err := c.client.SearchChatMessages(&client.SearchChatMessagesRequest{
ChatId: chatID,
Limit: limit,
SenderUserId: c.me.Id,
Filter: &client.SearchMessagesFilterEmpty{},
})
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 {
messageIds = append(messageIds, message.Id)
}
_, err = c.client.DeleteMessages(&client.DeleteMessagesRequest{
ChatId: chatID,
MessageIds: messageIds,
Revoke: true,
})
if err != nil {
return err.Error(), true
}
case "help":
return helpString(helpTypeChat), true
default:
return "", false return "", false
} }
return "", true
}

View file

@ -413,7 +413,7 @@ func (c *Client) messageToPrefix(message *client.Message, fileString string) str
func (c *Client) ProcessOutgoingMessage(chatID int64, text string, messageID int64, returnJid string) { func (c *Client) ProcessOutgoingMessage(chatID int64, text string, messageID int64, returnJid string) {
if strings.HasPrefix(text, "/") { if strings.HasPrefix(text, "/") {
// try to execute a command // try to execute a command
response, isCommand := c.ProcessChatCommand(text) response, isCommand := c.ProcessChatCommand(chatID, text)
if response != "" { if response != "" {
gateway.SendMessage(returnJid, strconv.FormatInt(chatID, 10), response, c.xmpp) gateway.SendMessage(returnJid, strconv.FormatInt(chatID, 10), response, c.xmpp)
} }