add config

This commit is contained in:
Andrey Belvedersky
2021-06-06 03:46:11 +03:00
parent 0aa4922105
commit f3175b8ce9
10 changed files with 97 additions and 433 deletions

View File

@ -17,10 +17,8 @@ import (
// Чатбот
func JiraBot() {
func JiraBot(cfg *config.Config) {
cfg, _ := config.GetConfig()
redis := utils.Redis(cfg.Redis)
ctx := context.Background()
pnsq := utils.GetProducer()
@ -47,10 +45,9 @@ func JiraBot() {
b.Handle("/exit", scenes.Exit(b, redis, ctx))
b.Handle("/hello", func(m *telegram.Message) {
b.Send(m.Sender, "Hello World!")
b.Send(m.Sender, "Hello World from chatbot")
messageBody := []byte("hello")
topicName := "error"
topicName := "jira"
// Synchronously publish a single message to the specified topic.
// Messages can also be sent asynchronously and/or in batches.
err = pnsq.Publish(topicName, messageBody)
@ -58,7 +55,6 @@ func JiraBot() {
log.Fatal(err)
}
})
fmt.Println(templates.Title(b.Me, cfg.BotVersion, jiraClient.GetBaseURL().Host))
b.Start()
}

View File

@ -36,17 +36,13 @@ func ParseConfig(v *viper.Viper) (*Config, error) {
}
// Get config
func GetConfig() (*Config, error) {
cfgFile, err := LoadConfig("settings")
func GetConfig(filename string) (*Config, error) {
cfgFile, err := LoadConfig(filename)
if err != nil {
return nil, err
}
cfg, err := ParseConfig(cfgFile)
if err != nil {
return nil, err
}
return cfg, nil
return cfg, err
}
func GetReply() (*Reply, error) {

View File

@ -1,8 +1,10 @@
package message
package events
import (
"git/ecom/jira-bot/pkg/config"
"log"
"github.com/davecgh/go-spew/spew"
"github.com/nsqio/go-nsq"
)
@ -22,23 +24,24 @@ func (h *myMessageHandler) HandleMessage(m *nsq.Message) error {
return err
}
func Message() {
func Listen(cfg *config.Config) {
cf := nsq.NewConfig()
cf.ClientID = "jirabot"
cf.Hostname = "ekb-web02.myway.local"
cf.LookupdPollInterval = cf.LookupdPollInterval / 2
spew.Dump(cf)
consumer, err := nsq.NewConsumer("error", "error", cf)
consumer, err := nsq.NewConsumer("telegram", "jira", cf)
if err != nil {
log.Fatal(err)
}
// Set the Handler for messages received by this Consumer. Can be called multiple times.
// See also AddConcurrentHandlers.
consumer.SetLoggerLevel(nsq.LogLevelError)
consumer.AddHandler(&myMessageHandler{})
// Use nsqlookupd to discover nsqd instances.
// See also ConnectToNSQD, ConnectToNSQDs, ConnectToNSQLookupds.
err = consumer.ConnectToNSQLookupd("ekb-app01.myway.local:4161")
err = consumer.ConnectToNSQLookupd("ekb-web02.myway.local:4161")
if err != nil {
log.Fatal(err)
}
}

13
pkg/utils/exit.go Normal file
View File

@ -0,0 +1,13 @@
package utils
import (
"log"
"os"
"time"
)
func Exit(err error) {
log.Println(err)
time.Sleep(time.Second * 2)
os.Exit(1)
}

View File

@ -11,14 +11,14 @@ import (
)
func Hack() {
cfg, _ := settings.GetConfig()
cfg, _ := settings.GetConfig("settings")
tp := jira.BasicAuthTransport{
Username: cfg.Jira.Auth.Username,
Password: cfg.Jira.Auth.Password,
}
jiraClient, _ := jira.NewClient(tp.Client(), cfg.Jira.Url)
me, _, _ := jiraClient.User.GetSelf()
timeNeed := 165 // Количество часов

View File

@ -15,3 +15,5 @@ func GetProducer() *nsq.Producer {
}
return producer
}
func GetConsumer()

16
pkg/utils/wait.go Normal file
View File

@ -0,0 +1,16 @@
package utils
import (
"os"
"os/signal"
"syscall"
)
func Wait() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
select {
case <-c:
return
}
}