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, "chat_id": update.Message.ChatId,
}).Warn("New message from chat") }).Warn("New message from chat")
text := c.messageToText(update.Message) text := c.messageToText(update.Message, false)
file, filename := c.contentToFilename(update.Message.Content) file, filename := c.contentToFilename(update.Message.Content)
// download file(s) // download file(s)

View file

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