From 69e49429453ffa5748dd4d1effffbf59141b605c Mon Sep 17 00:00:00 2001 From: Ayomide Onigbinde Date: Sun, 30 Dec 2018 18:59:42 +0100 Subject: [PATCH] allow the use of a config file instead of manually pasting configs --- examples/basic/bot.config.yml | 5 ++++ examples/basic/main.go | 10 ++++---- parser.go | 45 +++++++++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 examples/basic/bot.config.yml create mode 100644 parser.go diff --git a/examples/basic/bot.config.yml b/examples/basic/bot.config.yml new file mode 100644 index 0000000..f867ca7 --- /dev/null +++ b/examples/basic/bot.config.yml @@ -0,0 +1,5 @@ +verify_token: +access_token: +app_secret: + +# other configs can be added as necessary. For example, the webhook URL and host. diff --git a/examples/basic/main.go b/examples/basic/main.go index f86d6e0..c8cf836 100644 --- a/examples/basic/main.go +++ b/examples/basic/main.go @@ -8,16 +8,18 @@ import ( "os" "time" + msgs "github.com/messenger" "github.com/paked/messenger" ) +var vt, acct, appsc = msgs.GetTokens() var ( - verifyToken = flag.String("verify-token", "mad-skrilla", "The token used to verify facebook (required)") + verifyToken = flag.String("verify-token", vt, "The token used to verify facebook (required)") verify = flag.Bool("should-verify", false, "Whether or not the app should verify itself") - pageToken = flag.String("page-token", "not skrilla", "The token that is used to verify the page on facebook") - appSecret = flag.String("app-secret", "", "The app secret from the facebook developer portal (required)") + pageToken = flag.String("page-token", acct, "The token that is used to verify the page on facebook") + appSecret = flag.String("app-secret", appsc, "The app secret from the facebook developer portal (required)") host = flag.String("host", "localhost", "The host used to serve the messenger bot") - port = flag.Int("port", 8080, "The port used to serve the messenger bot") + port = flag.Int("port", 5000, "The port used to serve the messenger bot") ) func main() { diff --git a/parser.go b/parser.go new file mode 100644 index 0000000..9d8c52d --- /dev/null +++ b/parser.go @@ -0,0 +1,45 @@ +package messenger + +import ( + "io/ioutil" + "log" + "os" + "path/filepath" + + "gopkg.in/yaml.v2" +) + +//Config is the struct for the configuration file +type Config struct { + VerifyToken string `yaml:"verify_token"` + AccessToken string `yaml:"access_token"` + AppSecret string `yaml:"app_secret"` +} + +//ReadYml parses the config yml file and format into the Config struct +func (x *Config) ReadYml() *Config { + configFile, err := filepath.Abs("./bot.config.yml") + if err != nil { + log.Printf("ERROR READING THE CONFIG FILE: %s", err) + } + + yamlFile, err := ioutil.ReadFile(configFile) + + if err != nil { + log.Println("Could not find the config file. Please make sure it is created", err) + os.Exit(-1) + } + + yaml.Unmarshal(yamlFile, &x) + + return x +} + +//GetTokens returns the verifytoken, accesstoken and the appsecret from the config file +func GetTokens() (string, string, string) { + var c Config + + configObj := c.ReadYml() + verifyToken, accessToken, appSecret := configObj.VerifyToken, configObj.AccessToken, configObj.AppSecret + return verifyToken, accessToken, appSecret +}