Fix replies to non-text messages
This commit is contained in:
parent
7b90b8e4ae
commit
9d84965e8b
|
@ -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)
|
||||||
|
|
|
@ -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,102 +413,142 @@ 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)
|
||||||
return fmt.Sprintf(
|
if preview {
|
||||||
"*%s*\n%s\n%s",
|
return venue.Venue.Title
|
||||||
venue.Venue.Title,
|
} else {
|
||||||
venue.Venue.Address,
|
return fmt.Sprintf(
|
||||||
c.formatLocation(venue.Venue.Location),
|
"*%s*\n%s\n%s",
|
||||||
)
|
venue.Venue.Title,
|
||||||
|
venue.Venue.Address,
|
||||||
|
c.formatLocation(venue.Venue.Location),
|
||||||
|
)
|
||||||
|
}
|
||||||
case client.TypeMessagePhoto:
|
case client.TypeMessagePhoto:
|
||||||
photo, _ := message.Content.(*client.MessagePhoto)
|
photo, _ := message.Content.(*client.MessagePhoto)
|
||||||
return formatter.Format(
|
if preview {
|
||||||
photo.Caption.Text,
|
return photo.Caption.Text
|
||||||
formatter.SortEntities(photo.Caption.Entities),
|
} else {
|
||||||
markupFunction,
|
return formatter.Format(
|
||||||
)
|
photo.Caption.Text,
|
||||||
|
formatter.SortEntities(photo.Caption.Entities),
|
||||||
|
markupFunction,
|
||||||
|
)
|
||||||
|
}
|
||||||
case client.TypeMessageAudio:
|
case client.TypeMessageAudio:
|
||||||
audio, _ := message.Content.(*client.MessageAudio)
|
audio, _ := message.Content.(*client.MessageAudio)
|
||||||
return formatter.Format(
|
if preview {
|
||||||
audio.Caption.Text,
|
return audio.Caption.Text
|
||||||
formatter.SortEntities(audio.Caption.Entities),
|
} else {
|
||||||
markupFunction,
|
return formatter.Format(
|
||||||
)
|
audio.Caption.Text,
|
||||||
|
formatter.SortEntities(audio.Caption.Entities),
|
||||||
|
markupFunction,
|
||||||
|
)
|
||||||
|
}
|
||||||
case client.TypeMessageVideo:
|
case client.TypeMessageVideo:
|
||||||
video, _ := message.Content.(*client.MessageVideo)
|
video, _ := message.Content.(*client.MessageVideo)
|
||||||
return formatter.Format(
|
if preview {
|
||||||
video.Caption.Text,
|
return video.Caption.Text
|
||||||
formatter.SortEntities(video.Caption.Entities),
|
} else {
|
||||||
markupFunction,
|
return formatter.Format(
|
||||||
)
|
video.Caption.Text,
|
||||||
|
formatter.SortEntities(video.Caption.Entities),
|
||||||
|
markupFunction,
|
||||||
|
)
|
||||||
|
}
|
||||||
case client.TypeMessageDocument:
|
case client.TypeMessageDocument:
|
||||||
document, _ := message.Content.(*client.MessageDocument)
|
document, _ := message.Content.(*client.MessageDocument)
|
||||||
return formatter.Format(
|
if preview {
|
||||||
document.Caption.Text,
|
return document.Caption.Text
|
||||||
formatter.SortEntities(document.Caption.Entities),
|
} else {
|
||||||
markupFunction,
|
return formatter.Format(
|
||||||
)
|
document.Caption.Text,
|
||||||
|
formatter.SortEntities(document.Caption.Entities),
|
||||||
|
markupFunction,
|
||||||
|
)
|
||||||
|
}
|
||||||
case client.TypeMessageText:
|
case client.TypeMessageText:
|
||||||
text, _ := message.Content.(*client.MessageText)
|
text, _ := message.Content.(*client.MessageText)
|
||||||
return formatter.Format(
|
if preview {
|
||||||
text.Text.Text,
|
return text.Text.Text
|
||||||
formatter.SortEntities(text.Text.Entities),
|
} else {
|
||||||
markupFunction,
|
return formatter.Format(
|
||||||
)
|
text.Text.Text,
|
||||||
|
formatter.SortEntities(text.Text.Entities),
|
||||||
|
markupFunction,
|
||||||
|
)
|
||||||
|
}
|
||||||
case client.TypeMessageVoiceNote:
|
case client.TypeMessageVoiceNote:
|
||||||
voice, _ := message.Content.(*client.MessageVoiceNote)
|
voice, _ := message.Content.(*client.MessageVoiceNote)
|
||||||
return formatter.Format(
|
if preview {
|
||||||
voice.Caption.Text,
|
return voice.Caption.Text
|
||||||
formatter.SortEntities(voice.Caption.Entities),
|
} else {
|
||||||
markupFunction,
|
return formatter.Format(
|
||||||
)
|
voice.Caption.Text,
|
||||||
|
formatter.SortEntities(voice.Caption.Entities),
|
||||||
|
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)
|
||||||
return formatter.Format(
|
if preview {
|
||||||
animation.Caption.Text,
|
return animation.Caption.Text
|
||||||
formatter.SortEntities(animation.Caption.Entities),
|
} else {
|
||||||
markupFunction,
|
return formatter.Format(
|
||||||
)
|
animation.Caption.Text,
|
||||||
|
formatter.SortEntities(animation.Caption.Entities),
|
||||||
|
markupFunction,
|
||||||
|
)
|
||||||
|
}
|
||||||
case client.TypeMessageContact:
|
case client.TypeMessageContact:
|
||||||
contact, _ := message.Content.(*client.MessageContact)
|
contact, _ := message.Content.(*client.MessageContact)
|
||||||
var jid string
|
if preview {
|
||||||
if contact.Contact.UserId != 0 {
|
return contact.Contact.FirstName + " " + contact.Contact.LastName
|
||||||
jid = fmt.Sprintf("%v@%s", contact.Contact.UserId, gateway.Jid.Bare())
|
} else {
|
||||||
|
var jid string
|
||||||
|
if contact.Contact.UserId != 0 {
|
||||||
|
jid = fmt.Sprintf("%v@%s", contact.Contact.UserId, gateway.Jid.Bare())
|
||||||
|
}
|
||||||
|
return fmt.Sprintf(
|
||||||
|
"*%s %s*\n%s\n%s\n%s",
|
||||||
|
contact.Contact.FirstName,
|
||||||
|
contact.Contact.LastName,
|
||||||
|
contact.Contact.PhoneNumber,
|
||||||
|
contact.Contact.Vcard,
|
||||||
|
jid,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
return fmt.Sprintf(
|
|
||||||
"*%s %s*\n%s\n%s\n%s",
|
|
||||||
contact.Contact.FirstName,
|
|
||||||
contact.Contact.LastName,
|
|
||||||
contact.Contact.PhoneNumber,
|
|
||||||
contact.Contact.Vcard,
|
|
||||||
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)
|
||||||
|
|
||||||
rows := []string{}
|
if preview {
|
||||||
rows = append(rows, fmt.Sprintf("*%s*", poll.Poll.Question))
|
return poll.Poll.Question
|
||||||
for _, option := range poll.Poll.Options {
|
} else {
|
||||||
var tick string
|
rows := []string{}
|
||||||
if option.IsChosen {
|
rows = append(rows, fmt.Sprintf("*%s*", poll.Poll.Question))
|
||||||
tick = "x"
|
for _, option := range poll.Poll.Options {
|
||||||
} else {
|
var tick string
|
||||||
tick = " "
|
if option.IsChosen {
|
||||||
|
tick = "x"
|
||||||
|
} else {
|
||||||
|
tick = " "
|
||||||
|
}
|
||||||
|
rows = append(rows, fmt.Sprintf(
|
||||||
|
"[%s] %s | %v%% | %v vote",
|
||||||
|
tick,
|
||||||
|
option.Text,
|
||||||
|
option.VotePercentage,
|
||||||
|
option.VoterCount,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
rows = append(rows, fmt.Sprintf(
|
|
||||||
"[%s] %s | %v%% | %v vote",
|
|
||||||
tick,
|
|
||||||
option.Text,
|
|
||||||
option.VotePercentage,
|
|
||||||
option.VoterCount,
|
|
||||||
))
|
|
||||||
}
|
|
||||||
|
|
||||||
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())
|
||||||
|
|
Loading…
Reference in a new issue