package main
import (
"encoding/json"
"flag"
"fmt"
"os"
"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
//TODO OFFSET
results, err := scryfallSearch(query)
if err != nil {
fmt.Println(err)
// DO SOMETHING
}
photos := make([]tg.APIInlineQueryResultPhoto, len(results.Data))
for i, card := range results.Data {
caption := fmt.Sprintf("Scryfall - EDHREC (#%d) - cardmarket (%s €)", card.ScryfallURI, card.RelatedUris.Edhrec, card.EdhrecRank, card.PurchaseUris.Magiccardmarket, card.Eur)
photos[i] = tg.APIInlineQueryResultPhoto{
Type: "photo",
ResultID: card.ID,
PhotoURL: card.ImageUris.Large,
ThumbURL: card.ImageUris.Normal,
Title: card.Name,
Caption: caption,
}
}
api.AnswerInlineQuery(tg.InlineQueryResponse{
QueryID: update.Inline.QueryID,
Results: photos,
})
}()
}