106 lines
2.2 KiB
Go
106 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"flag"
|
|
"fmt"
|
|
"os"
|
|
"strconv"
|
|
|
|
"github.com/hamcha/tg"
|
|
)
|
|
|
|
type Config struct {
|
|
Token string
|
|
Bind string
|
|
WebhookURL string
|
|
WebhookPath string
|
|
}
|
|
|
|
func checkErr(err error, msg string, args ...interface{}) {
|
|
if err != nil {
|
|
fmt.Printf("FATAL ERROR\n"+msg+":\n ", args...)
|
|
fmt.Println(err.Error())
|
|
os.Exit(1)
|
|
}
|
|
}
|
|
|
|
var api *tg.Telegram
|
|
|
|
func main() {
|
|
cfgpath := flag.String("config", "stappa.conf", "Path to config file")
|
|
flag.Parse()
|
|
|
|
cfgfile, err := os.Open(*cfgpath)
|
|
checkErr(err, "Could not open config file")
|
|
|
|
var cfg Config
|
|
err = json.NewDecoder(cfgfile).Decode(&cfg)
|
|
checkErr(err, "Could not decode JSON from config file contents")
|
|
|
|
cfgfile.Close()
|
|
|
|
api = tg.MakeAPIClient(cfg.Token)
|
|
api.SetWebhook(cfg.WebhookURL)
|
|
api.HandleWebhook(cfg.Bind, cfg.WebhookPath, webhook)
|
|
}
|
|
|
|
func webhook(update tg.APIUpdate) {
|
|
// Ignore everything that isn't an inline query (for now)
|
|
if update.Inline == nil {
|
|
return
|
|
}
|
|
|
|
go func() {
|
|
query := update.Inline.Query
|
|
offset, _ := strconv.Atoi(update.Inline.Offset)
|
|
results, err := scryfallSearch(query, offset)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
// DO SOMETHING
|
|
return
|
|
}
|
|
|
|
nextcard := ""
|
|
if results.HasMore {
|
|
nextcard = strconv.Itoa(offset + len(results.Data))
|
|
}
|
|
photos := make([]tg.APIInlineQueryResultPhoto, len(results.Data))
|
|
for i, card := range results.Data {
|
|
caption := fmt.Sprintf("EDHREC rank: #%d - cardmarket: € %s", card.EdhrecRank, card.Eur)
|
|
photos[i] = tg.APIInlineQueryResultPhoto{
|
|
Type: "photo",
|
|
ResultID: card.ID,
|
|
PhotoURL: card.ImageUris.Large,
|
|
ThumbURL: card.ImageUris.Normal,
|
|
Title: card.Name,
|
|
Caption: caption,
|
|
ReplyMarkup: &tg.APIInlineKeyboardMarkup{
|
|
InlineKeyboard: [][]tg.APIInlineKeyboardButton{{
|
|
{
|
|
Text: "Scryfall",
|
|
URL: card.ScryfallURI,
|
|
}, {
|
|
Text: "EDHREC",
|
|
URL: card.RelatedUris.Edhrec,
|
|
}, {
|
|
Text: "MCM",
|
|
URL: card.PurchaseUris.Magiccardmarket,
|
|
},
|
|
}},
|
|
},
|
|
}
|
|
}
|
|
|
|
err = api.AnswerInlineQuery(tg.InlineQueryResponse{
|
|
QueryID: update.Inline.QueryID,
|
|
Results: photos,
|
|
NextOffset: nextcard,
|
|
})
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
// DO SOMETHING
|
|
return
|
|
}
|
|
}()
|
|
}
|