aboutsummaryrefslogtreecommitdiff
path: root/telegram/commands.go
diff options
context:
space:
mode:
authorbodqhrohro <bodqhrohro@gmail.com>2019-12-06 02:21:39 +0300
committerbodqhrohro <bodqhrohro@gmail.com>2019-12-06 02:21:39 +0300
commit0013baa247b54286fb86119b91a5180f4334f463 (patch)
treeed29cd72dda89f71941294f8df7a4023a73876a1 /telegram/commands.go
parentcb8e7f4fef060e0b8c942ca8a4dbe92ff61a0a02 (diff)
Add delete command
Diffstat (limited to 'telegram/commands.go')
-rw-r--r--telegram/commands.go51
1 files changed, 47 insertions, 4 deletions
diff --git a/telegram/commands.go b/telegram/commands.go
index d3a8ea7..86477cc 100644
--- a/telegram/commands.go
+++ b/telegram/commands.go
@@ -8,6 +8,7 @@ import (
"dev.narayana.im/narayana/telegabber/xmpp/gateway"
+ log "github.com/sirupsen/logrus"
"github.com/zelenin/go-tdlib/client"
)
@@ -27,7 +28,7 @@ var transportCommands = 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"},
//"add": command{"@username", "add @username to your chat list"},
//"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
// and returns a response and the status of command support
-func (c *Client) ProcessChatCommand(cmdline string) (string, bool) {
- cmd, _ := parseCommand(cmdline)
+func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool) {
+ cmd, args := parseCommand(cmdline)
switch cmd {
+ // delete last message(s)
+ 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
}