Assign IDs from Telegram to XMPP messages

calls
Bohdan Horbeshko 1 year ago
parent b3d66993e5
commit 6e32c62f8d

@ -183,10 +183,13 @@ func (c *Client) unsubscribe(chatID int64) error {
func (c *Client) sendMessagesReverse(chatID int64, messages []*client.Message) {
for i := len(messages) - 1; i >= 0; i-- {
message := messages[i]
gateway.SendMessage(
c.jid,
strconv.FormatInt(chatID, 10),
c.formatMessage(0, 0, false, messages[i]),
c.formatMessage(0, 0, false, message),
strconv.FormatInt(message.Id, 10),
c.xmpp,
)
}

@ -219,20 +219,20 @@ func (c *Client) interactor() {
if c.Session.Login != "" {
c.authorizer.PhoneNumber <- c.Session.Login
} else {
gateway.SendMessage(c.jid, "", "Please, enter your Telegram login via /login 12345", c.xmpp)
gateway.SendMessage(c.jid, "", "Please, enter your Telegram login via /login 12345", "", c.xmpp)
}
// stage 1: wait for auth code
case client.TypeAuthorizationStateWaitCode:
log.Warn("Waiting for authorization code...")
gateway.SendMessage(c.jid, "", "Please, enter authorization code via /code 12345", c.xmpp)
gateway.SendMessage(c.jid, "", "Please, enter authorization code via /code 12345", "", c.xmpp)
// stage 1b: wait for registration
case client.TypeAuthorizationStateWaitRegistration:
log.Warn("Waiting for full name...")
gateway.SendMessage(c.jid, "", "This number is not registered yet! Please, enter your name via /setname John Doe", c.xmpp)
gateway.SendMessage(c.jid, "", "This number is not registered yet! Please, enter your name via /setname John Doe", "", c.xmpp)
// stage 2: wait for 2fa
case client.TypeAuthorizationStateWaitPassword:
log.Warn("Waiting for 2FA password...")
gateway.SendMessage(c.jid, "", "Please, enter 2FA passphrase via /password 12345", c.xmpp)
gateway.SendMessage(c.jid, "", "Please, enter 2FA passphrase via /password 12345", "", c.xmpp)
}
}
}

@ -242,7 +242,7 @@ func (c *Client) updateMessageContent(update *client.UpdateMessageContent) {
textContent.Text.Entities,
markupFunction,
))
gateway.SendMessage(c.jid, strconv.FormatInt(update.ChatId, 10), text, c.xmpp)
gateway.SendMessage(c.jid, strconv.FormatInt(update.ChatId, 10), text, "e" + strconv.FormatInt(update.MessageId, 10), c.xmpp)
}
}
@ -256,7 +256,7 @@ func (c *Client) updateDeleteMessages(update *client.UpdateDeleteMessages) {
deleteChar = "✗ "
}
text := deleteChar + strings.Join(int64SliceToStringSlice(update.MessageIds), ",")
gateway.SendMessage(c.jid, strconv.FormatInt(update.ChatId, 10), text, c.xmpp)
gateway.SendMessage(c.jid, strconv.FormatInt(update.ChatId, 10), text, "", c.xmpp)
}
}

@ -806,9 +806,11 @@ func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) {
ForceRead: true,
})
// forward message to XMPP
gateway.SendMessageWithOOB(c.jid, strconv.FormatInt(chatId, 10), text, c.xmpp, oob)
sId := strconv.FormatInt(message.Id, 10)
sChatId := strconv.FormatInt(chatId, 10)
gateway.SendMessageWithOOB(c.jid, sChatId, text, sId, c.xmpp, oob)
if auxText != "" {
gateway.SendMessage(c.jid, strconv.FormatInt(chatId, 10), auxText, c.xmpp)
gateway.SendMessage(c.jid, sChatId, auxText, sId, c.xmpp)
}
}
@ -823,7 +825,7 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
// try to execute commands
response, isCommand := c.ProcessChatCommand(chatID, text)
if response != "" {
gateway.SendMessage(returnJid, strconv.FormatInt(chatID, 10), response, c.xmpp)
gateway.SendMessage(returnJid, strconv.FormatInt(chatID, 10), response, "", c.xmpp)
}
// do not send on success
if isCommand {
@ -849,6 +851,7 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
returnJid,
strconv.FormatInt(chatID, 10),
fmt.Sprintf("Failed to fetch the uploaded file: %s", err.Error()),
"",
c.xmpp,
)
return nil
@ -861,6 +864,7 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
returnJid,
strconv.FormatInt(chatID, 10),
fmt.Sprintf("Received status code %v", response.StatusCode),
"",
c.xmpp,
)
return nil
@ -872,6 +876,7 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
returnJid,
strconv.FormatInt(chatID, 10),
fmt.Sprintf("Failed to create a temporary directory: %s", err.Error()),
"",
c.xmpp,
)
return nil
@ -882,6 +887,7 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
returnJid,
strconv.FormatInt(chatID, 10),
fmt.Sprintf("Failed to create a temporary file: %s", err.Error()),
"",
c.xmpp,
)
return nil
@ -893,6 +899,7 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
returnJid,
strconv.FormatInt(chatID, 10),
fmt.Sprintf("Failed to write a temporary file: %s", err.Error()),
"",
c.xmpp,
)
return nil
@ -943,6 +950,7 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
returnJid,
strconv.FormatInt(chatID, 10),
fmt.Sprintf("Not sent: %s", err.Error()),
"",
c.xmpp,
)
}

@ -27,16 +27,16 @@ var Jid *stanza.Jid
var DirtySessions = false
// SendMessage creates and sends a message stanza
func SendMessage(to string, from string, body string, component *xmpp.Component) {
sendMessageWrapper(to, from, body, component, "")
func SendMessage(to string, from string, body string, id string, component *xmpp.Component) {
sendMessageWrapper(to, from, body, id, component, "")
}
// SendMessageWithOOB creates and sends a message stanza with OOB URL
func SendMessageWithOOB(to string, from string, body string, component *xmpp.Component, oob string) {
sendMessageWrapper(to, from, body, component, oob)
func SendMessageWithOOB(to string, from string, body string, id string, component *xmpp.Component, oob string) {
sendMessageWrapper(to, from, body, id, component, oob)
}
func sendMessageWrapper(to string, from string, body string, component *xmpp.Component, oob string) {
func sendMessageWrapper(to string, from string, body string, id string, component *xmpp.Component, oob string) {
componentJid := Jid.Full()
var logFrom string
@ -59,6 +59,7 @@ func sendMessageWrapper(to string, from string, body string, component *xmpp.Com
From: messageFrom,
To: to,
Type: "chat",
Id: id,
},
Body: body,
}

@ -106,7 +106,7 @@ func HandleMessage(s xmpp.Sender, p stanza.Packet) {
if err == nil && toJid.Bare() == gatewayJid && (strings.HasPrefix(msg.Body, "/") || strings.HasPrefix(msg.Body, "!")) {
response := session.ProcessTransportCommand(msg.Body, resource)
if response != "" {
gateway.SendMessage(msg.From, "", response, component)
gateway.SendMessage(msg.From, "", response, "", component)
}
return
}

Loading…
Cancel
Save