Use finch's configuration handling.

This commit is contained in:
Syfaro 2018-10-04 19:16:19 -05:00
parent b7b86eb9e4
commit 133ee0866e
8 changed files with 22 additions and 45 deletions

View File

@ -6,8 +6,7 @@ After being configured (optional, will only do SFW images from FurAffinity thoug
## Configuration
* `TELEGRAM_APITOKEN` - Telegram Bot API token from Botfather
* `TOKEN` - Telegram Bot API token from Botfather
* `DEBUG` - If debugging information should be printed
* `FA_A` - FurAffinity cookie 'a'
* `FA_B` - FurAffinity cookie 'b'

25
bot.go
View File

@ -1,9 +1,8 @@
package main
import (
"os"
"github.com/Syfaro/finch"
"github.com/sirupsen/logrus"
_ "huefox.com/syfaro/telegram-furryimgbot/commands/info"
_ "huefox.com/syfaro/telegram-furryimgbot/commands/inline"
@ -13,21 +12,21 @@ import (
)
func main() {
var f *finch.Finch
f := finch.NewFinch("")
token := os.Getenv("TELEGRAM_APITOKEN")
if token == "" {
logger.Log.Debug("Attempting to use config file for Telegram configuration.")
f = finch.NewFinch("")
} else {
logger.Log.Debug("Using environment variables for configuration.")
f = finch.NewFinch(token)
switch v := f.Config.Get("debug").(type) {
case bool:
f.API.Debug = v
case string:
f.API.Debug = v == "true"
default:
f.API.Debug = false
}
if v, ok := f.Config["debug"]; ok {
f.API.Debug = v.(bool)
if f.Config.Get("debug") == "true" {
logger.Log.SetLevel(logrus.DebugLevel)
} else {
f.API.Debug = os.Getenv("DEBUG") == "true"
logger.Log.SetLevel(logrus.InfoLevel)
}
sites.ConfigureSites(f)

2
go.mod
View File

@ -2,7 +2,7 @@ module huefox.com/syfaro/telegram-furryimgbot
require (
github.com/PuerkitoBio/goquery v1.4.1
github.com/Syfaro/finch v0.0.0-20180922020248-0a006980206b
github.com/Syfaro/finch v0.0.0-20181005000040-65a305514294
github.com/andybalholm/cascadia v1.0.0 // indirect
github.com/cenkalti/backoff v2.0.0+incompatible // indirect
github.com/certifi/gocertifi v0.0.0-20180905225744-ee1a9a0726d2 // indirect

3
go.sum
View File

@ -2,6 +2,8 @@ github.com/PuerkitoBio/goquery v1.4.1 h1:smcIRGdYm/w7JSbcdeLHEMzxmsBQvl8lhf0dSw2
github.com/PuerkitoBio/goquery v1.4.1/go.mod h1:T9ezsOHcCrDCgA8aF1Cqr3sSYbO/xgdy8/R/XiIMAhA=
github.com/Syfaro/finch v0.0.0-20180922020248-0a006980206b h1:Hf99+dSLVO8/cTKwyGGoiMcmp1fZHwO+hEupJgUxM7w=
github.com/Syfaro/finch v0.0.0-20180922020248-0a006980206b/go.mod h1:MQMvy4IFRZVDSFhjuAI3FYrvNxcgvKCJNl83wUxCyTI=
github.com/Syfaro/finch v0.0.0-20181005000040-65a305514294 h1:8UnlS1KsRGLiWGHjC1yoRlz8LkJxsPhMLRnyS7a9uQo=
github.com/Syfaro/finch v0.0.0-20181005000040-65a305514294/go.mod h1:MQMvy4IFRZVDSFhjuAI3FYrvNxcgvKCJNl83wUxCyTI=
github.com/andybalholm/cascadia v1.0.0 h1:hOCXnnZ5A+3eVDX8pvgl4kofXv2ELss0bKcqRySc45o=
github.com/andybalholm/cascadia v1.0.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
github.com/cenkalti/backoff v2.0.0+incompatible h1:5IIPUHhlnUZbcHQsQou5k1Tn58nJkeJL9U+ig5CHJbY=
@ -22,6 +24,7 @@ github.com/go-telegram-bot-api/telegram-bot-api v4.6.3-0.20180922012028-898e79fe
github.com/go-telegram-bot-api/telegram-bot-api v4.6.3-0.20180922012028-898e79fe47da+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/konsorten/go-windows-terminal-sequences v0.0.0-20180402223658-b729f2633dfe h1:CHRGQ8V7OlCYtwaKPJi3iA7J+YdNKdo8j7nG5IgDhjs=
github.com/konsorten/go-windows-terminal-sequences v0.0.0-20180402223658-b729f2633dfe/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=

View File

@ -1,18 +1,8 @@
package logger
import (
"os"
"github.com/sirupsen/logrus"
)
// Log is the application logger.
var Log = logrus.New()
func init() {
if os.Getenv("DEBUG") == "true" {
Log.SetLevel(logrus.DebugLevel)
} else {
Log.SetLevel(logrus.InfoLevel)
}
}

View File

@ -15,12 +15,9 @@ type fa struct {
}
func (f *fa) loadConfig(finch *finch.Finch) {
a := fromConfigOrSecret(finch, "fa_a", "FA_A")
b := fromConfigOrSecret(finch, "fa_b", "FA_B")
cookies := []*http.Cookie{
&http.Cookie{Name: "b", Value: b},
&http.Cookie{Name: "a", Value: a},
&http.Cookie{Name: "b", Value: finch.Config.Get("fa_b").(string)},
&http.Cookie{Name: "a", Value: finch.Config.Get("fa_a").(string)},
}
f.cookies = cookies

View File

@ -3,7 +3,6 @@ package sites
import (
"crypto/sha1"
"encoding/hex"
"os"
"github.com/Syfaro/finch"
@ -26,14 +25,6 @@ type PostInfo struct {
Caption string
}
func fromConfigOrSecret(f *finch.Finch, config, env string) string {
if v, ok := f.Config[config]; ok {
return v.(string)
}
return os.Getenv(env)
}
func getKeyboard(sourceURL, imageURL string) *tgbotapi.InlineKeyboardMarkup {
var buttons = []tgbotapi.InlineKeyboardButton{
tgbotapi.InlineKeyboardButton{Text: "Direct Link", URL: &imageURL},

View File

@ -17,14 +17,12 @@ type twit struct {
}
func (t *twit) loadConfig(f *finch.Finch) {
consumerKey := fromConfigOrSecret(f, "twitter_consumer_key", "TWITTER_CONSUMER_KEY")
consumerSecret := fromConfigOrSecret(f, "twitter_consumer_secret", "TWITTER_CONSUMER_SECRET")
config := &clientcredentials.Config{
ClientID: consumerKey,
ClientSecret: consumerSecret,
ClientID: f.Config.Get("twitter_consumer_key").(string),
ClientSecret: f.Config.Get("twitter_secret_key").(string),
TokenURL: "https://api.twitter.com/oauth2/token",
}
httpClient := config.Client(oauth2.NoContext)
t.client = twitter.NewClient(httpClient)
}