Add callbacks (might not work)

This commit is contained in:
Hamcha 2018-12-13 13:40:46 +01:00
parent 707389387a
commit 16e0dbd129
Signed by: hamcha
GPG key ID: A40413D21021EAEE

62
main.go
View file

@ -52,6 +52,14 @@ func main() {
api.HandleWebhook(cfg.Bind, cfg.WebhookPath, webhook)
}
type CardFaceFlipPics struct {
Normal string
Flipped string
Scryfall string
Edhrec string
MCM string
}
func webhook(update tg.APIUpdate) {
// Handle inline queries (99% of the usage I hope)
if update.Inline != nil {
@ -99,6 +107,18 @@ func webhook(update tg.APIUpdate) {
face := card.ImageUris
if card.ImageUris.Large == "" && card.CardFaces != nil {
face = card.CardFaces[0].ImageUris
facedata, _ := json.Marshal(CardFaceFlipPics{
Normal: face.Large,
Flipped: card.CardFaces[1].ImageUris.Large,
Scryfall: card.ScryfallURI,
Edhrec: card.RelatedUris.Edhrec,
MCM: card.PurchaseUris.Cardmarket,
})
buttons = append(buttons, tg.APIInlineKeyboardButton{
Text: "🔄",
CallbackData: string(facedata),
})
}
photos[i] = tg.APIInlineQueryResultPhoto{
Type: "photo",
@ -176,6 +196,48 @@ func webhook(update tg.APIUpdate) {
})
}
}
// Handle inline callback requests (flipped cards)
if update.Callback != nil {
var data CardFaceFlipPics
err := json.Unmarshal([]byte(*update.Callback.Data), &data)
if err == nil {
buttons := []tg.APIInlineKeyboardButton{
{
Text: "Scryfall",
URL: data.Scryfall,
},
}
if data.Edhrec != "" {
buttons = append(buttons, tg.APIInlineKeyboardButton{
Text: "EDHREC",
URL: data.Edhrec,
})
}
if data.MCM != "" {
buttons = append(buttons, tg.APIInlineKeyboardButton{
Text: "MCM",
URL: data.MCM,
})
}
api.EditMedia(tg.ClientEditMediaData{
ChatID: update.Message.Chat.ChatID,
MessageID: update.Message.MessageID,
Media: tg.APIInputMediaPhoto{
Type: "photo",
Media: data.Flipped,
},
ReplyMarkup: &tg.APIInlineKeyboardMarkup{
InlineKeyboard: [][]tg.APIInlineKeyboardButton{buttons},
},
})
} else {
fmt.Println("Unknown callback: ", *update.Callback.Data)
}
api.AnswerCallback(tg.ClientCallbackQueryData{
QueryID: update.Callback.ID,
})
}
}
func getCardRequests(str string) (out []string) {