Send messages to Telegram

This commit is contained in:
bodqhrohro 2019-12-03 18:48:41 +02:00
parent 90f0490e16
commit 9d31a390a8
3 changed files with 68 additions and 4 deletions

View file

@ -141,8 +141,8 @@ func (c *Client) ProcessChatCommand(cmdline string) (string, bool) {
cmd, _ := parseCommand(cmdline)
switch cmd {
case "help":
return helpString(helpTypeChat), false
return helpString(helpTypeChat), true
}
return "", true
return "", false
}

View file

@ -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,
})
}
}

View file

@ -64,7 +64,7 @@ func HandleMessage(s xmpp.Sender, p stanza.Packet) {
toParts := strings.Split(msg.To, "@")
toID := toParts[0]
if len(toParts) > 1 {
toIDInt, err := strconv.Atoi(toID)
toIDInt, err := strconv.ParseInt(toID, 10, 64)
if err == nil {
session.ProcessOutgoingMessage(toIDInt, msg.Body, 0, msg.From)
return