allow the use of a config file instead of manually pasting configs
This commit is contained in:
parent
8d546920cd
commit
69e4942945
5
examples/basic/bot.config.yml
Normal file
5
examples/basic/bot.config.yml
Normal 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.
|
@ -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
45
parser.go
Normal 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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user