add NSQ
This commit is contained in:
parent
18b2d4e554
commit
0aa4922105
@ -1,7 +1,11 @@
|
||||
package main
|
||||
|
||||
import "git/ecom/jira-bot/pkg/bot"
|
||||
import (
|
||||
"git/ecom/jira-bot/pkg/bot"
|
||||
"git/ecom/jira-bot/pkg/message"
|
||||
)
|
||||
|
||||
func main() {
|
||||
message.Message()
|
||||
bot.JiraBot()
|
||||
}
|
||||
|
1
go.mod
1
go.mod
@ -10,6 +10,7 @@ require (
|
||||
github.com/go-redis/redis/v8 v8.9.0 // indirect
|
||||
github.com/google/go-cmp v0.5.5 // indirect
|
||||
github.com/klauspost/cpuid v1.2.0 // indirect
|
||||
github.com/nsqio/go-nsq v1.0.8 // indirect
|
||||
github.com/spf13/viper v1.7.1
|
||||
github.com/stretchr/testify v1.7.0 // indirect
|
||||
github.com/valyala/quicktemplate v1.6.3
|
||||
|
4
go.sum
4
go.sum
@ -113,6 +113,8 @@ github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:W
|
||||
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
|
||||
github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0=
|
||||
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
|
||||
github.com/golang/snappy v0.0.1 h1:Qgr9rKW7uDUkrbSmQeiDsGa8SjGyCOGtuasMWwvp2P4=
|
||||
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
|
||||
github.com/google/btree v1.0.0 h1:0udJVsspx3VBr5FwtLhQQtuAsVc79tTq0ocGIPAU6qo=
|
||||
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
|
||||
@ -246,6 +248,8 @@ github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9
|
||||
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
|
||||
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223 h1:F9x/1yl3T2AeKLr2AMdilSD8+f9bvMnNN8VS5iDtovc=
|
||||
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
|
||||
github.com/nsqio/go-nsq v1.0.8 h1:3L2F8tNLlwXXlp2slDUrUWSBn2O3nMh8R1/KEDFTHPk=
|
||||
github.com/nsqio/go-nsq v1.0.8/go.mod h1:vKq36oyeVXgsS5Q8YEO7WghqidAVXQlcFxzQbQTuDEY=
|
||||
github.com/nxadm/tail v1.4.4 h1:DQuhQpB1tVlglWS2hLQ5OV6B5r8aGxSrPc5Qo6uTN78=
|
||||
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
|
||||
github.com/oklog/ulid v1.3.1 h1:EGfNDEx6MqHz8B3uNV6QAib1UR2Lm97sHi3ocA6ESJ4=
|
||||
|
54
main.go
Normal file
54
main.go
Normal file
@ -0,0 +1,54 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/nsqio/go-nsq"
|
||||
)
|
||||
|
||||
type myMessageHandler struct{}
|
||||
|
||||
func processMessage(b []byte) error {
|
||||
log.Println(string(b))
|
||||
return nil
|
||||
}
|
||||
|
||||
// HandleMessage implements the Handler interface.
|
||||
func (h *myMessageHandler) HandleMessage(m *nsq.Message) error {
|
||||
log.Println("test")
|
||||
if len(m.Body) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
err := processMessage(m.Body)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func main() {
|
||||
config := nsq.NewConfig()
|
||||
|
||||
// // ....
|
||||
// producer, err := nsq.NewProducer("ekb-app01.myway.local:4150", config)
|
||||
// if err != nil {
|
||||
// log.Fatal(err)
|
||||
// }
|
||||
// if producer.Publish("grpc", []byte("Случилась какая-то ботва 1")); err != nil {
|
||||
// log.Fatal(err)
|
||||
// }
|
||||
// producer.Stop()
|
||||
|
||||
// ...
|
||||
consumer, err := nsq.NewConsumer("grpc", "grpc", config)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
consumer.AddHandler(&myMessageHandler{})
|
||||
|
||||
if consumer.ConnectToNSQLookupd("ekb-app01.myway.local:4161"); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
//consumer.Stop()
|
||||
}
|
@ -14,12 +14,16 @@ import (
|
||||
telegram "gopkg.in/tucnak/telebot.v2"
|
||||
)
|
||||
|
||||
|
||||
|
||||
// Чатбот
|
||||
func JiraBot() {
|
||||
|
||||
|
||||
cfg, _ := config.GetConfig()
|
||||
redis := utils.Redis(cfg.Redis)
|
||||
ctx := context.Background()
|
||||
pnsq := utils.GetProducer()
|
||||
jiraClient, _ := utils.GetClient(&cfg.Jira)
|
||||
b, err := telegram.NewBot(telegram.Settings{
|
||||
URL: cfg.Telegram.Url,
|
||||
@ -41,10 +45,20 @@ func JiraBot() {
|
||||
b.Handle("/start", scenes.Start(b, cfg))
|
||||
|
||||
b.Handle("/exit", scenes.Exit(b, redis, ctx))
|
||||
|
||||
b.Handle("/hello", func(m *telegram.Message) {
|
||||
b.Send(m.Sender, "Hello World!")
|
||||
messageBody := []byte("hello")
|
||||
topicName := "error"
|
||||
|
||||
// Synchronously publish a single message to the specified topic.
|
||||
// Messages can also be sent asynchronously and/or in batches.
|
||||
err = pnsq.Publish(topicName, messageBody)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
})
|
||||
|
||||
fmt.Println(templates.Title(b.Me, cfg.BotVersion,jiraClient.GetBaseURL().Host))
|
||||
fmt.Println(templates.Title(b.Me, cfg.BotVersion, jiraClient.GetBaseURL().Host))
|
||||
b.Start()
|
||||
}
|
||||
|
44
pkg/message/message.go
Normal file
44
pkg/message/message.go
Normal file
@ -0,0 +1,44 @@
|
||||
package message
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/nsqio/go-nsq"
|
||||
)
|
||||
|
||||
type myMessageHandler struct{}
|
||||
|
||||
func processMessage(b []byte) error {
|
||||
log.Println(string(b))
|
||||
return nil
|
||||
}
|
||||
|
||||
// HandleMessage implements the Handler interface.
|
||||
func (h *myMessageHandler) HandleMessage(m *nsq.Message) error {
|
||||
if len(m.Body) == 0 {
|
||||
return nil
|
||||
}
|
||||
err := processMessage(m.Body)
|
||||
return err
|
||||
}
|
||||
|
||||
func Message() {
|
||||
cf := nsq.NewConfig()
|
||||
|
||||
consumer, err := nsq.NewConsumer("error", "error", 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.AddHandler(&myMessageHandler{})
|
||||
|
||||
// Use nsqlookupd to discover nsqd instances.
|
||||
// See also ConnectToNSQD, ConnectToNSQDs, ConnectToNSQLookupds.
|
||||
err = consumer.ConnectToNSQLookupd("ekb-app01.myway.local:4161")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
}
|
@ -2,10 +2,14 @@ package utils
|
||||
|
||||
import "log"
|
||||
|
||||
func SendError(devs []int) {
|
||||
func CheckErr(err error) {
|
||||
if err != nil {
|
||||
sendError([]int{132, 31213, 213})
|
||||
}
|
||||
}
|
||||
|
||||
func sendError(devs []int) {
|
||||
for _, dev := range devs {
|
||||
log.Println(dev)
|
||||
}
|
||||
}
|
||||
|
||||
// 169837862
|
||||
|
17
pkg/utils/nsqd.go
Normal file
17
pkg/utils/nsqd.go
Normal file
@ -0,0 +1,17 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/nsqio/go-nsq"
|
||||
)
|
||||
|
||||
func GetProducer() *nsq.Producer {
|
||||
config := nsq.NewConfig()
|
||||
|
||||
producer, err := nsq.NewProducer("ekb-app01.myway.local:4150", config)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return producer
|
||||
}
|
@ -2,7 +2,6 @@ package utils
|
||||
|
||||
import (
|
||||
"git/ecom/jira-bot/pkg/config"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user