aboutsummaryrefslogtreecommitdiff
path: root/telegram/utils.go
diff options
context:
space:
mode:
authorbodqhrohro <bodqhrohro@gmail.com>2019-12-03 19:48:41 +0300
committerbodqhrohro <bodqhrohro@gmail.com>2019-12-03 19:48:41 +0300
commit9d31a390a8bf5c554eb0e934a2bfe2fa8feca8eb (patch)
tree0b05f0aecb4c684e183ca722239bca6197bf4e63 /telegram/utils.go
parent90f0490e1626f3e0cd753caa5a4aac7787cb6dfc (diff)
Send messages to Telegram
Diffstat (limited to 'telegram/utils.go')
-rw-r--r--telegram/utils.go66
1 files changed, 65 insertions, 1 deletions
diff --git a/telegram/utils.go b/telegram/utils.go
index d98cfb0..de70152 100644
--- a/telegram/utils.go
+++ b/telegram/utils.go
@@ -23,6 +23,7 @@ import (
var errOffline = errors.New("TDlib instance is offline")
var spaceRegex = regexp.MustCompile(`\s+`)
+var replyRegex = regexp.MustCompile("> ?([0-9]{10,})")
const newlineChar string = "\n"
@@ -406,14 +407,77 @@ func (c *Client) messageToPrefix(message *client.Message, fileString string) str
}
// ProcessOutgoingMessage executes commands or sends messages to mapped chats
-func (c *Client) ProcessOutgoingMessage(chatID int, text string, messageID int, returnJid string) {
+func (c *Client) ProcessOutgoingMessage(chatID int64, text string, messageID int64, returnJid string) {
if strings.HasPrefix(text, "/") {
+ // try to execute a command
response, isCommand := c.ProcessChatCommand(text)
if response != "" {
gateway.SendMessage(returnJid, strconv.Itoa(int(chatID)), response, c.xmpp)
}
+ // do not send on success
if isCommand {
return
}
}
+
+ if !c.online {
+ // we're offline
+ return
+ }
+
+ log.Warnf("Send message to chat %v", chatID)
+
+ // 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,
+ }
+ }
+
+ if messageID != 0 {
+ c.client.EditMessageText(&client.EditMessageTextRequest{
+ ChatId: chatID,
+ MessageId: messageID,
+ InputMessageContent: message,
+ })
+ } else {
+ c.client.SendMessage(&client.SendMessageRequest{
+ ChatId: chatID,
+ ReplyToMessageId: reply,
+ InputMessageContent: message,
+ })
+ }
}