stappabot/main.go

101 lines
2.1 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
2018-09-17 12:42:07 +00:00
return
2018-09-17 10:26:24 +00:00
}
photos := make([]tg.APIInlineQueryResultPhoto, len(results.Data))
for i, card := range results.Data {
2018-09-17 12:49:43 +00:00
caption := fmt.Sprintf("EDHREC rank: #%d - cardmarket: € %s", card.EdhrecRank, 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,
2018-09-17 12:49:43 +00:00
Caption: caption,
2018-09-17 12:47:09 +00:00
ReplyMarkup: &tg.APIInlineKeyboardMarkup{
InlineKeyboard: [][]tg.APIInlineKeyboardButton{{
{
Text: "Scryfall",
URL: card.ScryfallURI,
}, {
2018-09-17 12:49:43 +00:00
Text: "EDHREC",
2018-09-17 12:47:09 +00:00
URL: card.RelatedUris.Edhrec,
}, {
2018-09-17 12:50:32 +00:00
Text: "MCM",
2018-09-17 12:47:09 +00:00
URL: card.PurchaseUris.Magiccardmarket,
},
}},
2018-09-17 12:42:07 +00:00
},
2018-09-17 10:26:24 +00:00
}
}
2018-09-17 12:42:07 +00:00
err = api.AnswerInlineQuery(tg.InlineQueryResponse{
2018-09-17 10:26:24 +00:00
QueryID: update.Inline.QueryID,
Results: photos,
})
2018-09-17 12:42:07 +00:00
if err != nil {
fmt.Println(err)
// DO SOMETHING
return
}
2018-09-17 10:26:24 +00:00
}()
}