|
|
|
@ -25,7 +25,7 @@ import (
|
|
|
|
|
var errOffline = errors.New("TDlib instance is offline")
|
|
|
|
|
|
|
|
|
|
var spaceRegex = regexp.MustCompile(`\s+`)
|
|
|
|
|
var replyRegex = regexp.MustCompile("> ?([0-9]{10,})")
|
|
|
|
|
var replyRegex = regexp.MustCompile("\\A>>? ?([0-9]+)\\n")
|
|
|
|
|
|
|
|
|
|
const newlineChar string = "\n"
|
|
|
|
|
|
|
|
|
@ -483,83 +483,68 @@ func (c *Client) messageToPrefix(message *client.Message, fileString string) str
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ProcessOutgoingMessage executes commands or sends messages to mapped chats
|
|
|
|
|
func (c *Client) ProcessOutgoingMessage(chatID int64, text string, messageID int64, returnJid string) {
|
|
|
|
|
if messageID == 0 && strings.HasPrefix(text, "/") {
|
|
|
|
|
// try to execute a command
|
|
|
|
|
func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid string) client.InputMessageContent {
|
|
|
|
|
if !c.Online() {
|
|
|
|
|
// we're offline
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if returnJid != "" && strings.HasPrefix(text, "/") {
|
|
|
|
|
// try to execute commands
|
|
|
|
|
response, isCommand := c.ProcessChatCommand(chatID, text)
|
|
|
|
|
if response != "" {
|
|
|
|
|
gateway.SendMessage(returnJid, strconv.FormatInt(chatID, 10), response, c.xmpp)
|
|
|
|
|
}
|
|
|
|
|
// do not send on success
|
|
|
|
|
if isCommand {
|
|
|
|
|
return
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !c.Online() {
|
|
|
|
|
// we're offline
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.Warnf("Send message to chat %v", chatID)
|
|
|
|
|
log.Warnf("Sending message to chat %v", chatID)
|
|
|
|
|
|
|
|
|
|
if messageID != 0 {
|
|
|
|
|
formattedText := &client.FormattedText{
|
|
|
|
|
Text: text,
|
|
|
|
|
}
|
|
|
|
|
// quotations
|
|
|
|
|
var reply int64
|
|
|
|
|
replySlice := replyRegex.FindStringSubmatch(text)
|
|
|
|
|
if len(replySlice) > 1 {
|
|
|
|
|
reply, _ = strconv.ParseInt(replySlice[1], 10, 64)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// compile our message
|
|
|
|
|
message := &client.InputMessageText{
|
|
|
|
|
Text: formattedText,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.client.EditMessageText(&client.EditMessageTextRequest{
|
|
|
|
|
ChatID: chatID,
|
|
|
|
|
MessageID: messageID,
|
|
|
|
|
InputMessageContent: message,
|
|
|
|
|
})
|
|
|
|
|
} 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 chatID != 0 && c.content.Upload != "" && strings.HasPrefix(text, c.content.Upload) {
|
|
|
|
|
file = &client.InputFileRemote{
|
|
|
|
|
ID: text,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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:]
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
// compile our message
|
|
|
|
|
message = &client.InputMessageText{
|
|
|
|
|
Text: formattedText,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if chatID != 0 {
|
|
|
|
|
_, err := c.client.SendMessage(&client.SendMessageRequest{
|
|
|
|
|
ChatID: chatID,
|
|
|
|
|
ReplyToMessageID: reply,
|
|
|
|
@ -569,10 +554,13 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, messageID int
|
|
|
|
|
gateway.SendMessage(
|
|
|
|
|
returnJid,
|
|
|
|
|
strconv.FormatInt(chatID, 10),
|
|
|
|
|
fmt.Sprintf("Message not sent: %s", err.Error()),
|
|
|
|
|
fmt.Sprintf("Not sent: %s", err.Error()),
|
|
|
|
|
c.xmpp,
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
} else {
|
|
|
|
|
return message
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|