Add replace command
This commit is contained in:
parent
3e791db201
commit
589876eef5
|
@ -3,6 +3,7 @@ package telegram
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"regexp"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
@ -289,6 +290,50 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err.Error(), true
|
return err.Error(), true
|
||||||
}
|
}
|
||||||
|
// edit last message
|
||||||
|
case "s":
|
||||||
|
if c.me == nil {
|
||||||
|
return "@me is not initialized", true
|
||||||
|
}
|
||||||
|
if len(args) < 2 {
|
||||||
|
return "Not enough arguments", true
|
||||||
|
}
|
||||||
|
regex, err := regexp.Compile(args[0])
|
||||||
|
if err != nil {
|
||||||
|
return err.Error(), true
|
||||||
|
}
|
||||||
|
|
||||||
|
messages, err := c.client.SearchChatMessages(&client.SearchChatMessagesRequest{
|
||||||
|
ChatId: chatID,
|
||||||
|
Limit: 1,
|
||||||
|
SenderUserId: c.me.Id,
|
||||||
|
Filter: &client.SearchMessagesFilterEmpty{},
|
||||||
|
})
|
||||||
|
log.Debugf("%#v", client.SearchChatMessagesRequest{
|
||||||
|
ChatId: chatID,
|
||||||
|
Limit: 1,
|
||||||
|
SenderUserId: c.me.Id,
|
||||||
|
Filter: &client.SearchMessagesFilterEmpty{},
|
||||||
|
})
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
messageText, ok := message.Content.(*client.MessageText)
|
||||||
|
if !ok {
|
||||||
|
return "Last message is not a text!", true
|
||||||
|
}
|
||||||
|
|
||||||
|
text := regex.ReplaceAllString(messageText.Text.Text, strings.Join(args[1:], " "))
|
||||||
|
c.ProcessOutgoingMessage(chatID, text, message.Id, "")
|
||||||
case "help":
|
case "help":
|
||||||
return helpString(helpTypeChat), true
|
return helpString(helpTypeChat), true
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -411,7 +411,7 @@ func (c *Client) messageToPrefix(message *client.Message, fileString string) str
|
||||||
|
|
||||||
// ProcessOutgoingMessage executes commands or sends messages to mapped chats
|
// ProcessOutgoingMessage executes commands or sends messages to mapped chats
|
||||||
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 messageID == 0 && strings.HasPrefix(text, "/") {
|
||||||
// try to execute a command
|
// try to execute a command
|
||||||
response, isCommand := c.ProcessChatCommand(chatID, text)
|
response, isCommand := c.ProcessChatCommand(chatID, text)
|
||||||
if response != "" {
|
if response != "" {
|
||||||
|
@ -430,53 +430,63 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, messageID int
|
||||||
|
|
||||||
log.Warnf("Send message to chat %v", chatID)
|
log.Warnf("Send message to chat %v", chatID)
|
||||||
|
|
||||||
// quotations
|
if messageID != 0 {
|
||||||
var reply int64
|
formattedText := &client.FormattedText{
|
||||||
replySlice := replyRegex.FindStringSubmatch(text)
|
Text: text,
|
||||||
if len(replySlice) > 1 {
|
|
||||||
reply, _ = strconv.ParseInt(replySlice[1], 10, 64)
|
|
||||||
}
|
|
||||||
|
|
||||||
// attach a file
|
|
||||||
var file *client.InputFileRemote
|
|
||||||
if c.content.Upload != "" && strings.HasPrefix(text, c.content.Upload) {
|
|
||||||
file = &client.InputFileRemote{
|
|
||||||
Id: text,
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// remove first line from text
|
|
||||||
if file != nil || reply != 0 {
|
|
||||||
newlinePos := strings.Index(text, newlineChar)
|
|
||||||
if newlinePos != -1 {
|
|
||||||
text = text[newlinePos+1:]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
formattedText := &client.FormattedText{
|
|
||||||
Text: text,
|
|
||||||
}
|
|
||||||
|
|
||||||
var message client.InputMessageContent
|
|
||||||
if file != nil {
|
|
||||||
// we can try to send a document
|
|
||||||
message = &client.InputMessageDocument{
|
|
||||||
Document: file,
|
|
||||||
Caption: formattedText,
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// compile our message
|
// compile our message
|
||||||
message = &client.InputMessageText{
|
message := &client.InputMessageText{
|
||||||
Text: formattedText,
|
Text: formattedText,
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if messageID != 0 {
|
|
||||||
c.client.EditMessageText(&client.EditMessageTextRequest{
|
c.client.EditMessageText(&client.EditMessageTextRequest{
|
||||||
ChatId: chatID,
|
ChatId: chatID,
|
||||||
MessageId: messageID,
|
MessageId: messageID,
|
||||||
InputMessageContent: message,
|
InputMessageContent: message,
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
// quotations
|
||||||
|
var reply int64
|
||||||
|
replySlice := replyRegex.FindStringSubmatch(text)
|
||||||
|
if len(replySlice) > 1 {
|
||||||
|
reply, _ = strconv.ParseInt(replySlice[1], 10, 64)
|
||||||
|
}
|
||||||
|
|
||||||
|
// attach a file
|
||||||
|
var file *client.InputFileRemote
|
||||||
|
if c.content.Upload != "" && strings.HasPrefix(text, c.content.Upload) {
|
||||||
|
file = &client.InputFileRemote{
|
||||||
|
Id: text,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove first line from text
|
||||||
|
if file != nil || reply != 0 {
|
||||||
|
newlinePos := strings.Index(text, newlineChar)
|
||||||
|
if newlinePos != -1 {
|
||||||
|
text = text[newlinePos+1:]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
formattedText := &client.FormattedText{
|
||||||
|
Text: text,
|
||||||
|
}
|
||||||
|
|
||||||
|
var message client.InputMessageContent
|
||||||
|
if file != nil {
|
||||||
|
// we can try to send a document
|
||||||
|
message = &client.InputMessageDocument{
|
||||||
|
Document: file,
|
||||||
|
Caption: formattedText,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// compile our message
|
||||||
|
message = &client.InputMessageText{
|
||||||
|
Text: formattedText,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
c.client.SendMessage(&client.SendMessageRequest{
|
c.client.SendMessage(&client.SendMessageRequest{
|
||||||
ChatId: chatID,
|
ChatId: chatID,
|
||||||
ReplyToMessageId: reply,
|
ReplyToMessageId: reply,
|
||||||
|
|
Loading…
Reference in a new issue