Refactor ProcessOutgoingMessage

This commit is contained in:
Bohdan Horbeshko 2023-06-03 20:25:00 -04:00
parent 9a84e9a8b6
commit a5c90340ad
2 changed files with 54 additions and 71 deletions

View file

@ -498,7 +498,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
return "Last message is empty", true
}
content := c.ProcessOutgoingMessage(0, rawCmdArguments(cmdline, 0), "", "", 0)
content := c.PrepareOutgoingMessageContent(rawCmdArguments(cmdline, 0))
if content != nil {
c.client.EditMessageText(&client.EditMessageTextRequest{
@ -515,7 +515,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
return "Not enough arguments", true
}
content := c.ProcessOutgoingMessage(0, rawCmdArguments(cmdline, 0), "", "", 0)
content := c.PrepareOutgoingMessageContent(rawCmdArguments(cmdline, 0))
if content != nil {
_, err := c.client.SendMessage(&client.SendMessageRequest{
@ -594,7 +594,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
}
}
content := c.ProcessOutgoingMessage(0, rawCmdArguments(cmdline, 1), "", "", 0)
content := c.PrepareOutgoingMessageContent(rawCmdArguments(cmdline, 1))
if content != nil {
_, err := c.client.SendMessage(&client.SendMessageRequest{

View file

@ -986,22 +986,27 @@ func (c *Client) ProcessIncomingMessage(chatId int64, message *client.Message) {
}
}
// PrepareMessageContent creates a simple text message
func (c *Client) PrepareOutgoingMessageContent(text string) client.InputMessageContent {
return c.prepareOutgoingMessageContent(text, nil)
}
// ProcessOutgoingMessage executes commands or sends messages to mapped chats
func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid string, id string, replyId int64) client.InputMessageContent {
func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid string, id string, replyId int64) {
if !c.Online() {
// we're offline
return nil
return
}
if returnJid != "" && (strings.HasPrefix(text, "/") || strings.HasPrefix(text, "!")) {
if strings.HasPrefix(text, "/") || strings.HasPrefix(text, "!") {
// try to execute commands
response, isCommand := c.ProcessChatCommand(chatID, text)
if response != "" {
gateway.SendTextMessage(returnJid, strconv.FormatInt(chatID, 10), response, c.xmpp)
c.returnMessage(returnJid, chatID, response)
}
// do not send on success
if isCommand {
return nil
return
}
}
@ -1020,60 +1025,30 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
// attach a file
var file *client.InputFileLocal
if chatID != 0 && c.content.Upload != "" && strings.HasPrefix(text, c.content.Upload) {
if c.content.Upload != "" && strings.HasPrefix(text, c.content.Upload) {
response, err := http.Get(text)
if err != nil {
gateway.SendTextMessage(
returnJid,
strconv.FormatInt(chatID, 10),
fmt.Sprintf("Failed to fetch the uploaded file: %s", err.Error()),
c.xmpp,
)
return nil
c.returnError(returnJid, chatID, "Failed to fetch the uploaded file", err)
}
if response != nil && response.Body != nil {
defer response.Body.Close()
if response.StatusCode != 200 {
gateway.SendTextMessage(
returnJid,
strconv.FormatInt(chatID, 10),
fmt.Sprintf("Received status code %v", response.StatusCode),
c.xmpp,
)
return nil
c.returnMessage(returnJid, chatID, fmt.Sprintf("Received status code %v", response.StatusCode))
}
tempDir, err := ioutil.TempDir("", "telegabber-*")
if err != nil {
gateway.SendTextMessage(
returnJid,
strconv.FormatInt(chatID, 10),
fmt.Sprintf("Failed to create a temporary directory: %s", err.Error()),
c.xmpp,
)
return nil
c.returnError(returnJid, chatID, "Failed to create a temporary directory", err)
}
tempFile, err := os.Create(filepath.Join(tempDir, filepath.Base(text)))
if err != nil {
gateway.SendTextMessage(
returnJid,
strconv.FormatInt(chatID, 10),
fmt.Sprintf("Failed to create a temporary file: %s", err.Error()),
c.xmpp,
)
return nil
c.returnError(returnJid, chatID, "Failed to create a temporary file", err)
}
_, err = io.Copy(tempFile, response.Body)
if err != nil {
gateway.SendTextMessage(
returnJid,
strconv.FormatInt(chatID, 10),
fmt.Sprintf("Failed to write a temporary file: %s", err.Error()),
c.xmpp,
)
return nil
c.returnError(returnJid, chatID, "Failed to write a temporary file", err)
}
file = &client.InputFileLocal{
@ -1092,29 +1067,12 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
}
}
formattedText := &client.FormattedText{
Text: text,
}
content := c.prepareOutgoingMessageContent(text, file)
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 chatID != 0 {
tgMessage, err := c.client.SendMessage(&client.SendMessageRequest{
ChatId: chatID,
ReplyToMessageId: reply,
InputMessageContent: message,
InputMessageContent: content,
})
if err != nil {
gateway.SendTextMessage(
@ -1129,10 +1087,35 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
log.Errorf("Failed to save ids %v/%v %v", tgMessage.ChatId, tgMessage.Id, id)
}
}
return nil
} else {
return message
}
func (c *Client) returnMessage(returnJid string, chatID int64, text string) {
gateway.SendTextMessage(returnJid, strconv.FormatInt(chatID, 10), 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) prepareOutgoingMessageContent(text string, file *client.InputFileLocal) client.InputMessageContent {
formattedText := &client.FormattedText{
Text: text,
}
var content client.InputMessageContent
if file != nil {
// we can try to send a document
content = &client.InputMessageDocument{
Document: file,
Caption: formattedText,
}
} else {
// compile our message
content = &client.InputMessageText{
Text: formattedText,
}
}
return content
}
// StatusesRange proxies the following function from unexported cache