Refactor ProcessOutgoingMessage
This commit is contained in:
parent
9a84e9a8b6
commit
a5c90340ad
|
@ -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{
|
||||
|
|
|
@ -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,47 +1067,55 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
|
|||
}
|
||||
}
|
||||
|
||||
content := c.prepareOutgoingMessageContent(text, file)
|
||||
|
||||
tgMessage, err := c.client.SendMessage(&client.SendMessageRequest{
|
||||
ChatId: chatID,
|
||||
ReplyToMessageId: reply,
|
||||
InputMessageContent: content,
|
||||
})
|
||||
if err != nil {
|
||||
gateway.SendTextMessage(
|
||||
returnJid,
|
||||
strconv.FormatInt(chatID, 10),
|
||||
fmt.Sprintf("Not sent: %s", err.Error()),
|
||||
c.xmpp,
|
||||
)
|
||||
} else {
|
||||
err = gateway.IdsDB.Set(c.Session.Login, c.jid, tgMessage.ChatId, tgMessage.Id, id)
|
||||
if err != nil {
|
||||
log.Errorf("Failed to save ids %v/%v %v", tgMessage.ChatId, tgMessage.Id, id)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 message client.InputMessageContent
|
||||
var content client.InputMessageContent
|
||||
if file != nil {
|
||||
// we can try to send a document
|
||||
message = &client.InputMessageDocument{
|
||||
content = &client.InputMessageDocument{
|
||||
Document: file,
|
||||
Caption: formattedText,
|
||||
}
|
||||
} else {
|
||||
// compile our message
|
||||
message = &client.InputMessageText{
|
||||
content = &client.InputMessageText{
|
||||
Text: formattedText,
|
||||
}
|
||||
}
|
||||
|
||||
if chatID != 0 {
|
||||
tgMessage, err := c.client.SendMessage(&client.SendMessageRequest{
|
||||
ChatId: chatID,
|
||||
ReplyToMessageId: reply,
|
||||
InputMessageContent: message,
|
||||
})
|
||||
if err != nil {
|
||||
gateway.SendTextMessage(
|
||||
returnJid,
|
||||
strconv.FormatInt(chatID, 10),
|
||||
fmt.Sprintf("Not sent: %s", err.Error()),
|
||||
c.xmpp,
|
||||
)
|
||||
} else {
|
||||
err = gateway.IdsDB.Set(c.Session.Login, c.jid, tgMessage.ChatId, tgMessage.Id, id)
|
||||
if err != nil {
|
||||
log.Errorf("Failed to save ids %v/%v %v", tgMessage.ChatId, tgMessage.Id, id)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
} else {
|
||||
return message
|
||||
}
|
||||
return content
|
||||
}
|
||||
|
||||
// StatusesRange proxies the following function from unexported cache
|
||||
|
|
Loading…
Reference in a new issue