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
|
return "Last message is empty", true
|
||||||
}
|
}
|
||||||
|
|
||||||
content := c.ProcessOutgoingMessage(0, rawCmdArguments(cmdline, 0), "", "", 0)
|
content := c.PrepareOutgoingMessageContent(rawCmdArguments(cmdline, 0))
|
||||||
|
|
||||||
if content != nil {
|
if content != nil {
|
||||||
c.client.EditMessageText(&client.EditMessageTextRequest{
|
c.client.EditMessageText(&client.EditMessageTextRequest{
|
||||||
|
@ -515,7 +515,7 @@ func (c *Client) ProcessChatCommand(chatID int64, cmdline string) (string, bool)
|
||||||
return "Not enough arguments", true
|
return "Not enough arguments", true
|
||||||
}
|
}
|
||||||
|
|
||||||
content := c.ProcessOutgoingMessage(0, rawCmdArguments(cmdline, 0), "", "", 0)
|
content := c.PrepareOutgoingMessageContent(rawCmdArguments(cmdline, 0))
|
||||||
|
|
||||||
if content != nil {
|
if content != nil {
|
||||||
_, err := c.client.SendMessage(&client.SendMessageRequest{
|
_, 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 {
|
if content != nil {
|
||||||
_, err := c.client.SendMessage(&client.SendMessageRequest{
|
_, 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
|
// 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() {
|
if !c.Online() {
|
||||||
// we're offline
|
// 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
|
// try to execute commands
|
||||||
response, isCommand := c.ProcessChatCommand(chatID, text)
|
response, isCommand := c.ProcessChatCommand(chatID, text)
|
||||||
if response != "" {
|
if response != "" {
|
||||||
gateway.SendTextMessage(returnJid, strconv.FormatInt(chatID, 10), response, c.xmpp)
|
c.returnMessage(returnJid, chatID, response)
|
||||||
}
|
}
|
||||||
// do not send on success
|
// do not send on success
|
||||||
if isCommand {
|
if isCommand {
|
||||||
return nil
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1020,60 +1025,30 @@ func (c *Client) ProcessOutgoingMessage(chatID int64, text string, returnJid str
|
||||||
|
|
||||||
// attach a file
|
// attach a file
|
||||||
var file *client.InputFileLocal
|
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)
|
response, err := http.Get(text)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
gateway.SendTextMessage(
|
c.returnError(returnJid, chatID, "Failed to fetch the uploaded file", err)
|
||||||
returnJid,
|
|
||||||
strconv.FormatInt(chatID, 10),
|
|
||||||
fmt.Sprintf("Failed to fetch the uploaded file: %s", err.Error()),
|
|
||||||
c.xmpp,
|
|
||||||
)
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
if response != nil && response.Body != nil {
|
if response != nil && response.Body != nil {
|
||||||
defer response.Body.Close()
|
defer response.Body.Close()
|
||||||
|
|
||||||
if response.StatusCode != 200 {
|
if response.StatusCode != 200 {
|
||||||
gateway.SendTextMessage(
|
c.returnMessage(returnJid, chatID, fmt.Sprintf("Received status code %v", response.StatusCode))
|
||||||
returnJid,
|
|
||||||
strconv.FormatInt(chatID, 10),
|
|
||||||
fmt.Sprintf("Received status code %v", response.StatusCode),
|
|
||||||
c.xmpp,
|
|
||||||
)
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tempDir, err := ioutil.TempDir("", "telegabber-*")
|
tempDir, err := ioutil.TempDir("", "telegabber-*")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
gateway.SendTextMessage(
|
c.returnError(returnJid, chatID, "Failed to create a temporary directory", err)
|
||||||
returnJid,
|
|
||||||
strconv.FormatInt(chatID, 10),
|
|
||||||
fmt.Sprintf("Failed to create a temporary directory: %s", err.Error()),
|
|
||||||
c.xmpp,
|
|
||||||
)
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
tempFile, err := os.Create(filepath.Join(tempDir, filepath.Base(text)))
|
tempFile, err := os.Create(filepath.Join(tempDir, filepath.Base(text)))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
gateway.SendTextMessage(
|
c.returnError(returnJid, chatID, "Failed to create a temporary file", err)
|
||||||
returnJid,
|
|
||||||
strconv.FormatInt(chatID, 10),
|
|
||||||
fmt.Sprintf("Failed to create a temporary file: %s", err.Error()),
|
|
||||||
c.xmpp,
|
|
||||||
)
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err = io.Copy(tempFile, response.Body)
|
_, err = io.Copy(tempFile, response.Body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
gateway.SendTextMessage(
|
c.returnError(returnJid, chatID, "Failed to write a temporary file", err)
|
||||||
returnJid,
|
|
||||||
strconv.FormatInt(chatID, 10),
|
|
||||||
fmt.Sprintf("Failed to write a temporary file: %s", err.Error()),
|
|
||||||
c.xmpp,
|
|
||||||
)
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
file = &client.InputFileLocal{
|
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{
|
formattedText := &client.FormattedText{
|
||||||
Text: text,
|
Text: text,
|
||||||
}
|
}
|
||||||
|
|
||||||
var message client.InputMessageContent
|
var content client.InputMessageContent
|
||||||
if file != nil {
|
if file != nil {
|
||||||
// we can try to send a document
|
// we can try to send a document
|
||||||
message = &client.InputMessageDocument{
|
content = &client.InputMessageDocument{
|
||||||
Document: file,
|
Document: file,
|
||||||
Caption: formattedText,
|
Caption: formattedText,
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// compile our message
|
// compile our message
|
||||||
message = &client.InputMessageText{
|
content = &client.InputMessageText{
|
||||||
Text: formattedText,
|
Text: formattedText,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return content
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// StatusesRange proxies the following function from unexported cache
|
// StatusesRange proxies the following function from unexported cache
|
||||||
|
|
Loading…
Reference in a new issue