stappabot/main.go

81 lines
1.8 KiB
Go
Raw Normal View History

2018-09-17 10:26:24 +00:00
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 {
2018-09-17 12:19:11 +00:00
caption := fmt.Sprintf("<a href=\"%s\">Scryfall</a> - <a href=\"%s\">EDHREC</a> (#%d) - <a href=\"%s\">cardmarket</a> (%s €)", card.ScryfallURI, card.RelatedUris.Edhrec, card.EdhrecRank, card.PurchaseUris.Magiccardmarket, card.Eur)
2018-09-17 10:26:24 +00:00
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,
})
}()
}