aboutsummaryrefslogtreecommitdiff
path: root/telegram/utils.go
diff options
context:
space:
mode:
authorBohdan Horbeshko <bodqhrohro@gmail.com>2023-09-29 23:17:25 +0300
committerBohdan Horbeshko <bodqhrohro@gmail.com>2023-09-29 23:17:25 +0300
commit47fa7bca492f9d78c3bea009c7686a6bf4d8fc3b (patch)
treead2d78eda0561d6ab9677f5109efd27425dd1f8f /telegram/utils.go
parenta0803123b2d89c7cd9a61faeb3b2b1bcbd57dbde (diff)
Return outgoing message errors as message error stanzas (only in groupchats yet)
Diffstat (limited to 'telegram/utils.go')
-rw-r--r--telegram/utils.go35
1 files changed, 22 insertions, 13 deletions
diff --git a/telegram/utils.go b/telegram/utils.go
index 21c4ad3..eba500c 100644
--- a/telegram/utils.go
+++ b/telegram/utils.go
@@ -1188,7 +1188,7 @@ func (c *Client) PrepareOutgoingMessageContent(text string) client.InputMessageC
}
// ProcessOutgoingMessage executes commands or sends messages to mapped chats, returns message id
-func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid string, replyId int64, replaceId int64) *client.Message {
+func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid string, replyId int64, replaceId int64, isGroupchat bool) *client.Message {
if !c.Online() {
// we're offline
return nil
@@ -1198,7 +1198,7 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
// try to execute commands
response, isCommand := c.ProcessChatCommand(chatID, text)
if response != "" {
- c.returnMessage(returnJid, chatID, response)
+ c.returnMessage(returnJid, chatID, response, 0, isGroupchat)
}
// do not send on success
if isCommand {
@@ -1224,27 +1224,31 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
if c.content.Upload != "" && strings.HasPrefix(text, c.content.Upload) {
response, err := http.Get(text)
if err != nil {
- c.returnError(returnJid, chatID, "Failed to fetch the uploaded file", err)
+ c.returnError(returnJid, chatID, "Failed to fetch the uploaded file", err, 500, isGroupchat)
}
if response != nil && response.Body != nil {
defer response.Body.Close()
if response.StatusCode != 200 {
- c.returnMessage(returnJid, chatID, fmt.Sprintf("Received status code %v", response.StatusCode))
+ c.returnMessage(returnJid, chatID, fmt.Sprintf("Received status code %v", response.StatusCode), response.StatusCode, isGroupchat)
+ return nil
}
tempDir, err := ioutil.TempDir("", "telegabber-*")
if err != nil {
- c.returnError(returnJid, chatID, "Failed to create a temporary directory", err)
+ c.returnError(returnJid, chatID, "Failed to create a temporary directory", err, 500, isGroupchat)
+ return nil
}
tempFile, err := os.Create(filepath.Join(tempDir, filepath.Base(text)))
if err != nil {
- c.returnError(returnJid, chatID, "Failed to create a temporary file", err)
+ c.returnError(returnJid, chatID, "Failed to create a temporary file", err, 500, isGroupchat)
+ return nil
}
_, err = io.Copy(tempFile, response.Body)
if err != nil {
- c.returnError(returnJid, chatID, "Failed to write a temporary file", err)
+ c.returnError(returnJid, chatID, "Failed to write a temporary file", err, 500, isGroupchat)
+ return nil
}
file = &client.InputFileLocal{
@@ -1272,7 +1276,7 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
InputMessageContent: content,
})
if err != nil {
- c.returnError(returnJid, chatID, "Not edited", err)
+ c.returnError(returnJid, chatID, "Not edited", err, 400, isGroupchat)
return nil
}
return tgMessage
@@ -1284,18 +1288,23 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
InputMessageContent: content,
})
if err != nil {
- c.returnError(returnJid, chatID, "Not sent", err)
+ c.returnError(returnJid, chatID, "Not sent", err, 400, isGroupchat)
return nil
}
return tgMessage
}
-func (c *Client) returnMessage(returnJid string, chatID int64, text string) {
- gateway.SendTextMessage(returnJid, strconv.FormatInt(chatID, 10), text, c.xmpp)
+func (c *Client) returnMessage(returnJid string, chatID int64, text string, code int, isGroupchat bool) {
+ sChatId := strconv.FormatInt(chatID, 10)
+ if isGroupchat {
+ gateway.SendErrorMessage(returnJid, sChatId + "@" + gateway.Jid.Bare(), text, code, isGroupchat, c.xmpp)
+ } else {
+ gateway.SendTextMessage(returnJid, sChatId, text, c.xmpp)
+ }
}
-func (c *Client) returnError(returnJid string, chatID int64, msg string, err error) {
- c.returnMessage(returnJid, chatID, fmt.Sprintf("%s: %s", msg, err.Error()))
+func (c *Client) returnError(returnJid string, chatID int64, msg string, err error, code int, isGroupchat bool) {
+ c.returnMessage(returnJid, chatID, fmt.Sprintf("%s: %s", msg, err.Error()), code, isGroupchat)
}
func (c *Client) prepareOutgoingMessageContent(text string, file *client.InputFileLocal) client.InputMessageContent {