Add support for poll creation messages

This commit is contained in:
Tulir Asokan 2022-11-17 23:02:01 +02:00
parent cd584d9c05
commit 1da1b9935b
3 changed files with 41 additions and 4 deletions

2
go.mod
View file

@ -11,7 +11,7 @@ require (
github.com/prometheus/client_golang v1.14.0
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
github.com/tidwall/gjson v1.14.3
go.mau.fi/whatsmeow v0.0.0-20221113135643-05e0c80bc669
go.mau.fi/whatsmeow v0.0.0-20221117205735-357841c4ff30
golang.org/x/image v0.1.0
golang.org/x/net v0.2.0
google.golang.org/protobuf v1.28.1

4
go.sum
View file

@ -66,8 +66,8 @@ github.com/yuin/goldmark v1.5.3 h1:3HUJmBFbQW9fhQOzMgseU134xfi6hU+mjWywx5Ty+/M=
github.com/yuin/goldmark v1.5.3/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.mau.fi/libsignal v0.0.0-20221015105917-d970e7c3c9cf h1:mzPxXBgDPHKDHMVV1tIWh7lwCiRpzCsXC0gNRX+K07c=
go.mau.fi/libsignal v0.0.0-20221015105917-d970e7c3c9cf/go.mod h1:XCjaU93vl71YNRPn059jMrK0xRDwVO5gKbxoPxow9mQ=
go.mau.fi/whatsmeow v0.0.0-20221113135643-05e0c80bc669 h1:pnt9YTRgOKN5eeq67F2vjfsDPu2BnmXx7f2RGZVFjIc=
go.mau.fi/whatsmeow v0.0.0-20221113135643-05e0c80bc669/go.mod h1:elpwNbr2EwG1kadbswg+fKjlWffYUmOxqvx7Q8uX9YE=
go.mau.fi/whatsmeow v0.0.0-20221117205735-357841c4ff30 h1:AVHEkGJSI6NsdbxVs9YEGPFR68uru1Ke/hH72EqzGpk=
go.mau.fi/whatsmeow v0.0.0-20221117205735-357841c4ff30/go.mod h1:2yweL8nczvtlIxkrvCb0y8xiO13rveX9lJPambwYV/E=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.2.0 h1:BRXPfhNivWL5Yq0BGQ39a2sW6t44aODpfxkWjYdzewE=

View file

@ -383,7 +383,7 @@ func containsSupportedMessage(waMsg *waProto.Message) bool {
waMsg.DocumentMessage != nil || waMsg.ContactMessage != nil || waMsg.LocationMessage != nil ||
waMsg.LiveLocationMessage != nil || waMsg.GroupInviteMessage != nil || waMsg.ContactsArrayMessage != nil ||
waMsg.HighlyStructuredMessage != nil || waMsg.TemplateMessage != nil || waMsg.TemplateButtonReplyMessage != nil ||
waMsg.ListMessage != nil || waMsg.ListResponseMessage != nil
waMsg.ListMessage != nil || waMsg.ListResponseMessage != nil || waMsg.PollCreationMessage != nil
}
func getMessageType(waMsg *waProto.Message) string {
@ -414,6 +414,10 @@ func getMessageType(waMsg *waProto.Message) string {
return "group invite"
case waMsg.ReactionMessage != nil:
return "reaction"
case waMsg.PollCreationMessage != nil:
return "poll create"
case waMsg.PollUpdateMessage != nil:
return "poll update"
case waMsg.ProtocolMessage != nil:
switch waMsg.GetProtocolMessage().GetType() {
case waProto.ProtocolMessage_REVOKE:
@ -527,6 +531,8 @@ func (portal *Portal) convertMessage(intent *appservice.IntentAPI, source *User,
return portal.convertListMessage(intent, source, waMsg.GetListMessage())
case waMsg.ListResponseMessage != nil:
return portal.convertListResponseMessage(intent, waMsg.GetListResponseMessage())
case waMsg.PollCreationMessage != nil:
return portal.convertPollCreationMessage(intent, waMsg.GetPollCreationMessage())
case waMsg.ImageMessage != nil:
return portal.convertMediaMessage(intent, source, info, waMsg.GetImageMessage(), "photo", isBackfill)
case waMsg.StickerMessage != nil:
@ -2089,6 +2095,37 @@ func (portal *Portal) convertListResponseMessage(intent *appservice.IntentAPI, m
}
}
func (portal *Portal) convertPollCreationMessage(intent *appservice.IntentAPI, msg *waProto.PollCreationMessage) *ConvertedMessage {
optionsListText := make([]string, len(msg.GetOptions()))
optionsListHTML := make([]string, len(msg.GetOptions()))
optionNames := make([]string, len(msg.GetOptions()))
for i, opt := range msg.GetOptions() {
optionNames[i] = opt.GetOptionName()
optionsListText[i] = fmt.Sprintf("%d. %s\n", i+1, optionNames[i])
optionsListHTML[i] = fmt.Sprintf("<li>%s</li>", event.TextToHTML(optionNames[i]))
}
body := fmt.Sprintf("%s\n\n%s", msg.GetName(), strings.Join(optionsListText, "\n"))
formattedBody := fmt.Sprintf("<p>%s</p><ol>%s</ol>", event.TextToHTML(msg.GetName()), strings.Join(optionsListHTML, ""))
return &ConvertedMessage{
Intent: intent,
Type: event.EventMessage,
Content: &event.MessageEventContent{
Body: body,
MsgType: event.MsgText,
Format: event.FormatHTML,
FormattedBody: formattedBody,
},
Extra: map[string]any{
"fi.mau.whatsapp.poll": map[string]any{
"options": optionNames,
"max_choices": msg.GetSelectableOptionsCount(),
},
},
ReplyTo: GetReply(msg.GetContextInfo()),
ExpiresIn: msg.GetContextInfo().GetExpiration(),
}
}
func (portal *Portal) convertLiveLocationMessage(intent *appservice.IntentAPI, msg *waProto.LiveLocationMessage) *ConvertedMessage {
content := &event.MessageEventContent{
Body: "Started sharing live location",