This commit is contained in:
Hamcha 2018-09-17 12:26:24 +02:00
commit 54c6e88ae5
Signed by: hamcha
GPG Key ID: A40413D21021EAEE
2 changed files with 145 additions and 0 deletions

80
main.go Normal file
View File

@ -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("<a href=\"%s\">Scryfall</a> - <a href=\"%s\">EDHREC</a> - <a href=\"%s\">cardmarket</a> (%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,
})
}()
}

65
scryfall.go Normal file
View File

@ -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
}