commit 54c6e88ae5ceec10420f16d88c761558247935b6 Author: Hamcha Date: Mon Sep 17 12:26:24 2018 +0200 Check in diff --git a/main.go b/main.go new file mode 100644 index 0000000..1dfe900 --- /dev/null +++ b/main.go @@ -0,0 +1,80 @@ +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 - cardmarket (%s €)", card.ScryfallURI, card.RelatedUris.Edhrec, card.PurchaseUris.Magiccardmarket) + 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, + }) + }() +} diff --git a/scryfall.go b/scryfall.go new file mode 100644 index 0000000..080186b --- /dev/null +++ b/scryfall.go @@ -0,0 +1,65 @@ +package main + +import ( + "encoding/json" + "net/http" + "net/url" + "time" +) + +type CardSearchResults struct { + TotalCards int `json:"total_cards"` + HasMore bool `json:"has_more"` + Data []CardData `json:"data"` + NextPage string `json:"next_page"` +} + +type CardData struct { + ID string `json:"id"` + Name string `json:"name"` + URI string `json:"uri"` + ScryfallURI string `json:"scryfall_uri"` + Layout string `json:"layout"` + HighresImage bool `json:"highres_image"` + ImageUris struct { + Small string `json:"small"` + Normal string `json:"normal"` + Large string `json:"large"` + Png string `json:"png"` + ArtCrop string `json:"art_crop"` + BorderCrop string `json:"border_crop"` + } `json:"image_uris"` + Eur string `json:"eur"` + RelatedUris struct { + Gatherer string `json:"gatherer"` + TcgplayerDecks string `json:"tcgplayer_decks"` + Edhrec string `json:"edhrec"` + Mtgtop8 string `json:"mtgtop8"` + } `json:"related_uris"` + PurchaseUris struct { + Amazon string `json:"amazon"` + Ebay string `json:"ebay"` + Tcgplayer string `json:"tcgplayer"` + Magiccardmarket string `json:"magiccardmarket"` + Cardhoarder string `json:"cardhoarder"` + CardKingdom string `json:"card_kingdom"` + MtgoTraders string `json:"mtgo_traders"` + Coolstuffinc string `json:"coolstuffinc"` + } `json:"purchase_uris"` +} + +var netClient = &http.Client{ + Timeout: time.Second * 10, +} + +func scryfallSearch(query string) (results CardSearchResults, err error) { + query = url.QueryEscape(query) + response, err := netClient.Get("https://api.scryfall.com/cards/search?order=name&q=" + query) + if err != nil { + return + } + defer response.Body.Close() + + json.NewDecoder(response.Body).Decode(&results) + return +}