Browse Source

Extracted config reading from single file out into a config module

pull/1/head
rodley82 4 years ago
parent
commit
5a695d912c
  1. 17
      cmd/bot.go
  2. 24
      config/app.go
  3. 2
      go.mod

17
main.go → cmd/bot.go

@ -6,29 +6,26 @@ import (
"fmt" "fmt"
"log" "log"
"os" "os"
"strings"
"os/exec" "os/exec"
"github.com/joho/godotenv" "rodleyserverbot/slack-bot/config"
"strings"
"github.com/slack-go/slack" "github.com/slack-go/slack"
"github.com/slack-go/slack/slackevents" "github.com/slack-go/slack/slackevents"
"github.com/slack-go/slack/socketmode" "github.com/slack-go/slack/socketmode"
) )
func main() { func main() {
// Load Env variables from .dot file
godotenv.Load(".env")
token := os.Getenv("SLACK_AUTH_TOKEN")
appToken := os.Getenv("SLACK_APP_TOKEN")
// Create a new client to slack by giving token // Create a new client to slack by giving token
// Set debug to true while developing // Set debug to true while developing
// Also add a ApplicationToken option to the client // Also add a ApplicationToken option to the client
client := slack.New(token, slack.OptionDebug(false), slack.OptionAppLevelToken(appToken)) client := slack.New(
config.Config.SlackAppToken,
slack.OptionDebug(false),
slack.OptionAppLevelToken(config.Config.SlackAppToken))
// go-slack comes with a SocketMode package that we need to use that accepts a Slack client and outputs a Socket mode client instead // go-slack comes with a SocketMode package that we need to use that accepts a Slack client and outputs a Socket mode client instead
socketClient := socketmode.New( socketClient := socketmode.New(
client, client,
socketmode.OptionDebug(true), socketmode.OptionDebug(false),
// Option to set a custom logger // Option to set a custom logger
socketmode.OptionLog(log.New(os.Stdout, "socketmode: ", log.Lshortfile|log.LstdFlags)), socketmode.OptionLog(log.New(os.Stdout, "socketmode: ", log.Lshortfile|log.LstdFlags)),
) )

24
config/app.go

@ -0,0 +1,24 @@
package config
import (
"os"
"github.com/joho/godotenv"
)
type ConfigEntries struct {
SlackAuthToken string
SlackAppToken string
ActionScript1 string
ActionScript2 string
}
var Config ConfigEntries
func init(){
// fmt.Println("init de config!")
// Load Env variables from .dot file
godotenv.Load(".env")
Config.SlackAuthToken = os.Getenv("SLACK_AUTH_TOKEN")
Config.SlackAppToken = os.Getenv("SLACK_APP_TOKEN")
}

2
go.mod

@ -4,6 +4,6 @@ go 1.13
require ( require (
github.com/gorilla/websocket v1.5.0 // indirect github.com/gorilla/websocket v1.5.0 // indirect
github.com/joho/godotenv v1.4.0 // indirect github.com/joho/godotenv v1.4.0
github.com/slack-go/slack v0.11.0 // indirect github.com/slack-go/slack v0.11.0 // indirect
) )

Loading…
Cancel
Save