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 return } photos := make([]tg.APIInlineQueryResultPhoto, len(results.Data)) for i, card := range results.Data { photos[i] = tg.APIInlineQueryResultPhoto{ Type: "photo", ResultID: card.ID, PhotoURL: card.ImageUris.Large, ThumbURL: card.ImageUris.Normal, Title: card.Name, ReplyMarkup: []tg.APIInlineKeyboardButton{ { Text: "Scryfall", URL: card.ScryfallURI, }, { Text: fmt.Sprintf("EDHREC (#%d)", card.EdhrecRank), URL: card.RelatedUris.Edhrec, }, { Text: fmt.Sprintf("Cardmarket (%s €)", card.ScryfallURI), URL: card.PurchaseUris.Magiccardmarket, }, }, } } err = api.AnswerInlineQuery(tg.InlineQueryResponse{ QueryID: update.Inline.QueryID, Results: photos, }) if err != nil { fmt.Println(err) // DO SOMETHING return } }() }