diff --git a/Dockerfile b/Dockerfile index 7e4007c..0eafdc1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,7 +3,7 @@ FROM golang:1.9.3-stretch WORKDIR / ADD ./bin/mg-telegram / ADD ./templates/ /templates/ -ADD ./web/ /web/ +ADD ./static/ /static/ ADD ./translate/ /translate/ ADD ./migrations/ /migrations/ diff --git a/Jenkinsfile b/Jenkinsfile index ab87e76..6540574 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -15,15 +15,15 @@ pipeline { stages { stage('Prepare') { steps { - sh 'cp config_test.yml.dist config_test.yml' - compose 'up -d --build postgres' - compose 'run --rm mg_telegram make migrate_test' + sh 'cp config_test.yml.dist config.yml' + compose 'up -d --build postgres_test' + compose 'run --rm mg_telegram_test make migrate_test' } } stage('Tests') { steps { - compose 'run --rm --no-deps mg_telegram make jenkins_test' + compose 'run --rm --no-deps mg_telegram_test make jenkins_test' } post { diff --git a/Makefile b/Makefile index d79f368..e166e68 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ BIN=$(ROOT_DIR)/bin/mg-telegram REVISION=$(shell git describe --tags 2>/dev/null || git log --format="v0.0-%h" -n 1 || echo "v0.0-unknown") ifndef GOPATH - $(error GOPATH must be defined) + $(error GOPATH must be defined) endif export GOPATH := $(GOPATH):$(ROOT_DIR) @@ -22,8 +22,9 @@ run: migrate @${BIN} --config $(CONFIG_FILE) run test: deps fmt - @echo "==> Running tests" - @cd $(SRC_DIR) && go test ./... -v -cpu 2 -cover -race + @echo "==> Running tests (result in test-report.xml)" + @go get -v -u github.com/jstemmer/go-junit-report + @cd $(SRC_DIR) && go test ./... -v -cpu 2 -cover -race | go-junit-report -set-exit-code > $(SRC_DIR)/test-report.xml jenkins_test: deps @echo "==> Running tests (result in test-report.xml)" diff --git a/docker-compose-test.yml b/docker-compose-test.yml index 6d4babf..cba0c50 100644 --- a/docker-compose-test.yml +++ b/docker-compose-test.yml @@ -1,7 +1,7 @@ version: '2.1' services: - postgres: + postgres_test: image: postgres:9.6 environment: POSTGRES_USER: mg_telegram_test @@ -10,13 +10,14 @@ services: ports: - ${POSTGRES_ADDRESS:-127.0.0.1:5450}:${POSTGRES_PORT:-5450} - mg_telegram: + mg_telegram_test: image: golang:1.9.3-stretch working_dir: /mg_telegram user: ${UID:-1000}:${GID:-1000} volumes: - ./:/mg_telegram/ links: - - postgres + - postgres_test ports: - ${MG_TELEGRAM_ADDRESS:-3002}:3002 + command: make migrate_test diff --git a/event.go b/event.go deleted file mode 100644 index e4c4d24..0000000 --- a/event.go +++ /dev/null @@ -1,9 +0,0 @@ -package main - -type EventMessage struct { - ChannelID uint64 `json:"channel_id"` - ExternalID int `json:"external_id,omitempty"` - ChatID int64 `json:"chat_id"` - Message string `json:"message,omitempty"` - Type string `json:"type"` -} diff --git a/routing.go b/routing.go index cff5230..06b3900 100644 --- a/routing.go +++ b/routing.go @@ -557,81 +557,6 @@ func validateCrmSettings(c Connection) error { return nil } -func mgWebhookHandler(w http.ResponseWriter, r *http.Request) { - bytes, err := ioutil.ReadAll(r.Body) - if err != nil { - raven.CaptureErrorAndWait(err, nil) - logger.Error(err) - return - } - - var msg EventMessage - err = json.Unmarshal(bytes, &msg) - if err != nil { - raven.CaptureErrorAndWait(err, nil) - logger.Error(err) - return - } - - b := getBotByChannel(msg.ChannelID) - if b.ID == 0 { - logger.Error(msg.ChannelID, "missing") - return - } - - if !b.Active { - logger.Error(msg.ChannelID, "deactivated") - return - } - - bot, err := tgbotapi.NewBotAPI(b.Token) - if err != nil { - raven.CaptureErrorAndWait(err, nil) - logger.Error(err) - } - - bot.Debug = true - - if msg.Type == "message_sent" { - msg, err := bot.Send(tgbotapi.NewMessage(msg.ChatID, msg.Message)) - if err != nil { - logger.Error(err) - w.WriteHeader(http.StatusBadRequest) - return - } - - logger.Debugf("%v", msg) - w.WriteHeader(http.StatusOK) - w.Write([]byte("Message sent")) - } - - if msg.Type == "message_updated" { - msg, err := bot.Send(tgbotapi.NewEditMessageText(msg.ChatID, msg.ExternalID, msg.Message)) - if err != nil { - logger.Error(err) - w.WriteHeader(http.StatusBadRequest) - return - } - - logger.Debugf("%v", msg) - w.WriteHeader(http.StatusOK) - w.Write([]byte("Message updated")) - } - - if msg.Type == "message_deleted" { - msg, err := bot.Send(tgbotapi.NewDeleteMessage(msg.ChatID, msg.ExternalID)) - if err != nil { - logger.Error(err) - w.WriteHeader(http.StatusBadRequest) - return - } - - logger.Debugf("%v", msg) - w.WriteHeader(http.StatusOK) - w.Write([]byte("Message deleted")) - } -} - func getAPIClient(url, key string) (*v5.Client, error, int) { client := v5.New(url, key) diff --git a/telegram.go b/telegram.go index aa62cc7..0a9939d 100644 --- a/telegram.go +++ b/telegram.go @@ -123,3 +123,79 @@ func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string w.WriteHeader(http.StatusOK) } + +func mgWebhookHandler(w http.ResponseWriter, r *http.Request) { + bytes, err := ioutil.ReadAll(r.Body) + if err != nil { + raven.CaptureErrorAndWait(err, nil) + logger.Error(err) + return + } + + var msg v1.WebhookRequest + err = json.Unmarshal(bytes, &msg) + if err != nil { + raven.CaptureErrorAndWait(err, nil) + logger.Error(err) + return + } + + uid, _ := strconv.Atoi(msg.Data.ExternalMessageID) + + b := getBotByChannel(msg.Data.ChannelID) + if b.ID == 0 { + logger.Error(msg.Data.ChannelID, "missing") + return + } + + if !b.Active { + logger.Error(msg.Data.ChannelID, "deactivated") + return + } + + bot, err := tgbotapi.NewBotAPI(b.Token) + if err != nil { + raven.CaptureErrorAndWait(err, nil) + logger.Error(err) + return + } + + if msg.Type == "message_sent" { + msg, err := bot.Send(tgbotapi.NewMessage(msg.Data.ExternalChatID, msg.Data.Content)) + if err != nil { + logger.Error(err) + w.WriteHeader(http.StatusBadRequest) + return + } + + logger.Debugf("%v", msg) + w.WriteHeader(http.StatusOK) + w.Write([]byte("Message sent")) + } + + if msg.Type == "message_updated" { + msg, err := bot.Send(tgbotapi.NewEditMessageText(msg.Data.ExternalChatID, uid, msg.Data.Content)) + if err != nil { + logger.Error(err) + w.WriteHeader(http.StatusBadRequest) + return + } + + logger.Debugf("%v", msg) + w.WriteHeader(http.StatusOK) + w.Write([]byte("Message updated")) + } + + if msg.Type == "message_deleted" { + msg, err := bot.Send(tgbotapi.NewDeleteMessage(msg.Data.ExternalChatID, uid)) + if err != nil { + logger.Error(err) + w.WriteHeader(http.StatusBadRequest) + return + } + + logger.Debugf("%v", msg) + w.WriteHeader(http.StatusOK) + w.Write([]byte("Message deleted")) + } +}