Fix replies to non-text messages

This commit is contained in:
Bohdan Horbeshko 2022-02-05 13:54:35 -05:00
parent 7b90b8e4ae
commit 9d84965e8b
2 changed files with 117 additions and 79 deletions

View file

@ -193,7 +193,7 @@ func (c *Client) updateNewMessage(update *client.UpdateNewMessage) {
"chat_id": update.Message.ChatId,
}).Warn("New message from chat")
text := c.messageToText(update.Message)
text := c.messageToText(update.Message, false)
file, filename := c.contentToFilename(update.Message.Content)
// download file(s)

View file

@ -264,6 +264,7 @@ func (c *Client) formatMessage(chatID int64, messageID int64, preview bool, mess
}
var str strings.Builder
// add messageid and sender
var senderId int64
if message.SenderId != nil {
switch message.SenderId.MessageSenderType() {
@ -276,6 +277,7 @@ func (c *Client) formatMessage(chatID int64, messageID int64, preview bool, mess
}
}
str.WriteString(fmt.Sprintf("%v | %s | ", message.Id, c.formatContact(senderId)))
// add date
if !preview {
str.WriteString(
time.Unix(int64(message.Date), 0).
@ -284,21 +286,17 @@ func (c *Client) formatMessage(chatID int64, messageID int64, preview bool, mess
)
}
// text message
var text string
if message.Content != nil {
switch message.Content.MessageContentType() {
case client.TypeMessageText:
messageText, _ := message.Content.(*client.MessageText)
text = messageText.Text.Text
// TODO: handle other message types with labels (not supported in Zhabogram!)
}
text = c.messageToText(message, preview)
}
if text != "" {
if !preview {
str.WriteString(text)
} else {
newlinePos := strings.Index(text, newlineChar)
if !preview || newlinePos == -1 {
if newlinePos == -1 {
str.WriteString(text)
} else {
str.WriteString(text[0:newlinePos])
@ -374,7 +372,7 @@ func (c *Client) formatLocation(location *client.Location) string {
)
}
func (c *Client) messageToText(message *client.Message) string {
func (c *Client) messageToText(message *client.Message, preview bool) string {
if message.Content == nil {
log.Warnf("Unknown message: %#v", message)
return "<empty message>"
@ -406,7 +404,7 @@ func (c *Client) messageToText(message *client.Message) string {
return "kicked " + c.formatContact(deleteMember.UserId)
case client.TypeMessagePinMessage:
pinMessage, _ := message.Content.(*client.MessagePinMessage)
return "pinned message: " + c.formatMessage(message.ChatId, pinMessage.MessageId, false, nil)
return "pinned message: " + c.formatMessage(message.ChatId, pinMessage.MessageId, preview, nil)
case client.TypeMessageChatChangeTitle:
changeTitle, _ := message.Content.(*client.MessageChatChangeTitle)
return "chat title set to: " + changeTitle.Title
@ -415,65 +413,100 @@ func (c *Client) messageToText(message *client.Message) string {
return c.formatLocation(location.Location)
case client.TypeMessageVenue:
venue, _ := message.Content.(*client.MessageVenue)
if preview {
return venue.Venue.Title
} else {
return fmt.Sprintf(
"*%s*\n%s\n%s",
venue.Venue.Title,
venue.Venue.Address,
c.formatLocation(venue.Venue.Location),
)
}
case client.TypeMessagePhoto:
photo, _ := message.Content.(*client.MessagePhoto)
if preview {
return photo.Caption.Text
} else {
return formatter.Format(
photo.Caption.Text,
formatter.SortEntities(photo.Caption.Entities),
markupFunction,
)
}
case client.TypeMessageAudio:
audio, _ := message.Content.(*client.MessageAudio)
if preview {
return audio.Caption.Text
} else {
return formatter.Format(
audio.Caption.Text,
formatter.SortEntities(audio.Caption.Entities),
markupFunction,
)
}
case client.TypeMessageVideo:
video, _ := message.Content.(*client.MessageVideo)
if preview {
return video.Caption.Text
} else {
return formatter.Format(
video.Caption.Text,
formatter.SortEntities(video.Caption.Entities),
markupFunction,
)
}
case client.TypeMessageDocument:
document, _ := message.Content.(*client.MessageDocument)
if preview {
return document.Caption.Text
} else {
return formatter.Format(
document.Caption.Text,
formatter.SortEntities(document.Caption.Entities),
markupFunction,
)
}
case client.TypeMessageText:
text, _ := message.Content.(*client.MessageText)
if preview {
return text.Text.Text
} else {
return formatter.Format(
text.Text.Text,
formatter.SortEntities(text.Text.Entities),
markupFunction,
)
}
case client.TypeMessageVoiceNote:
voice, _ := message.Content.(*client.MessageVoiceNote)
if preview {
return voice.Caption.Text
} else {
return formatter.Format(
voice.Caption.Text,
formatter.SortEntities(voice.Caption.Entities),
markupFunction,
)
}
case client.TypeMessageVideoNote:
return ""
case client.TypeMessageAnimation:
animation, _ := message.Content.(*client.MessageAnimation)
if preview {
return animation.Caption.Text
} else {
return formatter.Format(
animation.Caption.Text,
formatter.SortEntities(animation.Caption.Entities),
markupFunction,
)
}
case client.TypeMessageContact:
contact, _ := message.Content.(*client.MessageContact)
if preview {
return contact.Contact.FirstName + " " + contact.Contact.LastName
} else {
var jid string
if contact.Contact.UserId != 0 {
jid = fmt.Sprintf("%v@%s", contact.Contact.UserId, gateway.Jid.Bare())
@ -486,12 +519,16 @@ func (c *Client) messageToText(message *client.Message) string {
contact.Contact.Vcard,
jid,
)
}
case client.TypeMessageDice:
dice, _ := message.Content.(*client.MessageDice)
return fmt.Sprintf("%s 1d6: [%v]", dice.Emoji, dice.Value)
case client.TypeMessagePoll:
poll, _ := message.Content.(*client.MessagePoll)
if preview {
return poll.Poll.Question
} else {
rows := []string{}
rows = append(rows, fmt.Sprintf("*%s*", poll.Poll.Question))
for _, option := range poll.Poll.Options {
@ -512,6 +549,7 @@ func (c *Client) messageToText(message *client.Message) string {
return strings.Join(rows, "\n")
}
}
return fmt.Sprintf("unknown message (%s)", message.Content.MessageContentType())
}