1
0
mirror of synced 2024-11-22 04:46:05 +03:00

allow the use of a config file instead of manually pasting configs

This commit is contained in:
Ayomide Onigbinde 2018-12-30 18:59:42 +01:00
parent 8d546920cd
commit 69e4942945
3 changed files with 56 additions and 4 deletions

View File

@ -0,0 +1,5 @@
verify_token: <verify_token_here>
access_token: <access_token_here>
app_secret: <app_secret_here>
# other configs can be added as necessary. For example, the webhook URL and host.

View File

@ -8,16 +8,18 @@ import (
"os" "os"
"time" "time"
msgs "github.com/messenger"
"github.com/paked/messenger" "github.com/paked/messenger"
) )
var vt, acct, appsc = msgs.GetTokens()
var ( 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") 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") pageToken = flag.String("page-token", acct, "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)") 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") 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() { func main() {

45
parser.go Normal file
View File

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