1
0
mirror of synced 2024-11-25 13:16:02 +03:00

send messages from mg to telegram, minor fixes

This commit is contained in:
Alex Lushpai 2018-05-29 17:20:56 +03:00
parent 7ebadf753e
commit 889e246320
7 changed files with 89 additions and 95 deletions

View File

@ -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/

8
Jenkinsfile vendored
View File

@ -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 {

View File

@ -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)"

View File

@ -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

View File

@ -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"`
}

View File

@ -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)

View File

@ -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"))
}
}