1
0
mirror of synced 2024-11-25 21:26:02 +03:00

Merge pull request #6 from gwinn/master

MG webhook route
This commit is contained in:
Alex Lushpai 2018-05-31 16:33:24 +03:00 committed by GitHub
commit e8f58daa37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
29 changed files with 545 additions and 254 deletions

4
.gitignore vendored
View File

@ -1,5 +1,5 @@
gin-bin
config.yml config.yml
config_test.yml
.idea/ .idea/
/bin/* /bin/*
mg-telegram *.xml

View File

@ -1,14 +1,14 @@
FROM golang:1.9.3-stretch FROM golang:1.9.3-stretch
WORKDIR / WORKDIR /
ADD ./bin/mg-telegram / ADD ./bin/transport /
ADD ./templates/ /templates/ ADD ./templates/ /templates/
ADD ./web/ /web/ ADD ./static/ /static/
ADD ./translate/ /translate/ ADD ./translate/ /translate/
ADD ./migrations/ /migrations/ ADD ./migrations/ /migrations/
EXPOSE 3001 EXPOSE 3001
ENTRYPOINT ["/mg-telegram"] ENTRYPOINT ["/transport"]
CMD ["run"] CMD ["run"]

6
Jenkinsfile vendored
View File

@ -16,14 +16,14 @@ pipeline {
stage('Prepare') { stage('Prepare') {
steps { steps {
sh 'cp config_test.yml.dist config_test.yml' sh 'cp config_test.yml.dist config_test.yml'
compose 'up -d --build postgres' compose 'up -d --build postgres_test'
compose 'run --rm mg_telegram make migrate_test' compose 'run --rm mg_telegram_test make migrate_test'
} }
} }
stage('Tests') { stage('Tests') {
steps { steps {
compose 'run --rm --no-deps mg_telegram make jenkins_test' compose 'run --rm mg_telegram_test make jenkins_test'
} }
post { post {

View File

@ -3,7 +3,7 @@ SRC_DIR=$(ROOT_DIR)
MIGRATIONS_DIR=$(ROOT_DIR)/migrations MIGRATIONS_DIR=$(ROOT_DIR)/migrations
CONFIG_FILE=$(ROOT_DIR)/config.yml CONFIG_FILE=$(ROOT_DIR)/config.yml
CONFIG_TEST_FILE=$(ROOT_DIR)/config_test.yml CONFIG_TEST_FILE=$(ROOT_DIR)/config_test.yml
BIN=$(ROOT_DIR)/bin/mg-telegram BIN=$(ROOT_DIR)/bin/transport
REVISION=$(shell git describe --tags 2>/dev/null || git log --format="v0.0-%h" -n 1 || echo "v0.0-unknown") REVISION=$(shell git describe --tags 2>/dev/null || git log --format="v0.0-%h" -n 1 || echo "v0.0-unknown")
ifndef GOPATH ifndef GOPATH
@ -23,7 +23,7 @@ run: migrate
test: deps fmt test: deps fmt
@echo "==> Running tests" @echo "==> Running tests"
@cd $(SRC_DIR) && go test ./... -v -cpu 2 -cover -race @cd $(SRC_DIR) && go test ./... -v -cpu 2
jenkins_test: deps jenkins_test: deps
@echo "==> Running tests (result in test-report.xml)" @echo "==> Running tests (result in test-report.xml)"
@ -42,10 +42,10 @@ deps:
@go get -d -v $(DEPS) @go get -d -v $(DEPS)
migrate: build migrate: build
@${BIN} --config $(CONFIG_FILE) migrate -p ./migrations/ ${BIN} --config $(CONFIG_FILE) migrate -p $(MIGRATIONS_DIR)
migrate_test: build migrate_test: build
@${BIN} --config $(CONFIG_TEST_FILE) migrate ./migrations/ @${BIN} --config $(CONFIG_TEST_FILE) migrate -p $(MIGRATIONS_DIR)
migrate_down: build migrate_down: build
@${BIN} --config $(CONFIG_FILE) migrate -v down @${BIN} --config $(CONFIG_FILE) migrate -v down

View File

@ -1,5 +1,5 @@
database: database:
connection: postgres://mg_telegram_test:mg_telegram_test@postgres_test:5450/mg_telegram_test?sslmode=disable connection: postgres://mg_telegram_test:mg_telegram_test@postgres_test:5432/mg_telegram_test?sslmode=disable
http_server: http_server:
host: ~ host: ~

View File

@ -1,22 +1,24 @@
version: '2.1' version: '2.1'
services: services:
postgres: postgres_test:
image: postgres:9.6 image: postgres:9.6
environment: environment:
POSTGRES_USER: mg_telegram_test POSTGRES_USER: mg_telegram_test
POSTGRES_PASSWORD: mg_telegram_test POSTGRES_PASSWORD: mg_telegram_test
POSTGRES_DATABASE: mg_telegram_test POSTGRES_DATABASE: mg_telegram_test
ports: ports:
- ${POSTGRES_ADDRESS:-127.0.0.1:5450}:${POSTGRES_PORT:-5450} - ${POSTGRES_ADDRESS:-127.0.0.1:5434}:${POSTGRES_PORT:-5432}
mg_telegram: mg_telegram_test:
image: golang:1.9.3-stretch image: golang:1.9.3-stretch
working_dir: /mg_telegram working_dir: /mgtg
user: ${UID:-1000}:${GID:-1000} user: ${UID:-1000}:${GID:-1000}
volumes: volumes:
- ./:/mg_telegram/ - ./:/mgtg/
- ./static:/static/
links: links:
- postgres - postgres_test
ports: ports:
- ${MG_TELEGRAM_ADDRESS:-3002}:3002 - ${MG_TELEGRAM_ADDRESS:-3002}:3002
# command: make migrate_test

View File

@ -8,14 +8,15 @@ services:
POSTGRES_PASSWORD: mg_telegram POSTGRES_PASSWORD: mg_telegram
POSTGRES_DATABASE: mg_telegram POSTGRES_DATABASE: mg_telegram
ports: ports:
- ${POSTGRES_ADDRESS:-127.0.0.1:5434}:5432 - ${POSTGRES_ADDRESS:-127.0.0.1:5434}:${POSTGRES_PORT:-5432}
mg_telegram: mg_telegram:
image: golang:1.9.3-stretch image: golang:1.9.3-stretch
working_dir: /mg_telegram working_dir: /mgtg
user: ${UID:-1000}:${GID:-1000} user: ${UID:-1000}:${GID:-1000}
volumes: volumes:
- ./:/mg_telegram/ - ./:/mgtg
- ./static:/static/
links: links:
- postgres - postgres
ports: ports:

16
main.go
View File

@ -3,8 +3,9 @@ package main
import ( import (
"os" "os"
"github.com/getsentry/raven-go"
"github.com/jessevdk/go-flags" "github.com/jessevdk/go-flags"
"github.com/op/go-logging"
) )
// Options struct // Options struct
@ -14,12 +15,13 @@ type Options struct {
const transport = "mg-telegram" const transport = "mg-telegram"
var options Options var (
var parser = flags.NewParser(&options, flags.Default) config *TransportConfig
orm *Orm
func init() { logger *logging.Logger
raven.SetDSN(config.SentryDSN) options Options
} parser = flags.NewParser(&options, flags.Default)
)
func main() { func main() {
if _, err := parser.Parse(); err != nil { if _, err := parser.Parse(); err != nil {

View File

@ -0,0 +1,32 @@
alter table connection
drop constraint connection_key,
alter column client_id drop not null,
alter column api_key drop not null,
alter column api_url drop not null,
alter column mg_url drop not null,
alter column mg_token drop not null,
alter column api_url type varchar(100),
alter column mg_url type varchar(100);
alter table bot
add column client_id varchar(70);
update bot b
set client_id = c.client_id
from connection c
where b.connection_id = c.id;
alter table bot
drop column connection_id,
alter column channel drop not null,
alter column token drop not null,
drop constraint bot_key;
create table mapping
(
id serial not null
constraint mapping_pkey
primary key,
site_code text,
bot_id text
);

View File

@ -0,0 +1,27 @@
alter table connection
add constraint connection_key unique (client_id, mg_token),
alter column client_id set not null,
alter column api_key set not null,
alter column api_url set not null,
alter column mg_url set not null,
alter column mg_token set not null,
alter column api_url type varchar(255),
alter column mg_url type varchar(255);
alter table bot
add column connection_id integer;
update bot b
set connection_id = c.id
from connection c
where b.client_id = c.client_id;
alter table bot
drop column client_id,
alter column channel set not null,
alter column token set not null,
add constraint bot_key unique (channel, token);
alter table bot add foreign key (connection_id) references connection on delete cascade;
drop table mapping;

View File

@ -5,23 +5,24 @@ import "time"
// Connection model // Connection model
type Connection struct { type Connection struct {
ID int `gorm:"primary_key"` ID int `gorm:"primary_key"`
ClientID string `gorm:"client_id" json:"clientId,omitempty"` ClientID string `gorm:"client_id type:varchar(70);not null;unique" json:"clientId,omitempty"`
APIKEY string `gorm:"api_key" json:"api_key,omitempty"` APIKEY string `gorm:"api_key type:varchar(100);not null" json:"api_key,omitempty"`
APIURL string `gorm:"api_url" json:"api_url,omitempty"` APIURL string `gorm:"api_url type:varchar(255);not null" json:"api_url,omitempty"`
MGURL string `gorm:"mg_url" json:"mg_url,omitempty"` MGURL string `gorm:"mg_url type:varchar(255);not null;" json:"mg_url,omitempty"`
MGToken string `gorm:"mg_token" json:"mg_token,omitempty"` MGToken string `gorm:"mg_token type:varchar(100);not null;unique" json:"mg_token,omitempty"`
CreatedAt time.Time CreatedAt time.Time
UpdatedAt time.Time UpdatedAt time.Time
Active bool `json:"active,omitempty"` Active bool `json:"active,omitempty"`
Bots []Bot `gorm:"foreignkey:ConnectionID"`
} }
// Bot model // Bot model
type Bot struct { type Bot struct {
ID int `gorm:"primary_key"` ID int `gorm:"primary_key"`
ClientID string `gorm:"client_id" json:"clientId,omitempty"` ConnectionID int `gorm:"connection_id" json:"connectionId,omitempty"`
Channel uint64 `json:"channel,omitempty"` Channel uint64 `gorm:"channel;not null;unique" json:"channel,omitempty"`
Token string `json:"token,omitempty"` Token string `gorm:"token type:varchar(100);not null;unique" json:"token,omitempty"`
Name string `json:"name,omitempty"` Name string `gorm:"name type:varchar(40)" json:"name,omitempty"`
CreatedAt time.Time CreatedAt time.Time
UpdatedAt time.Time UpdatedAt time.Time
Active bool `json:"active,omitempty"` Active bool `json:"active,omitempty"`

View File

@ -1,5 +1,7 @@
package main package main
import "github.com/jinzhu/gorm"
func getConnection(uid string) *Connection { func getConnection(uid string) *Connection {
var connection Connection var connection Connection
orm.DB.First(&connection, "client_id = ?", uid) orm.DB.First(&connection, "client_id = ?", uid)
@ -26,15 +28,29 @@ func (c *Connection) saveConnection() error {
return orm.DB.Model(c).Where("client_id = ?", c.ClientID).Update(c).Error return orm.DB.Model(c).Where("client_id = ?", c.ClientID).Update(c).Error
} }
func getBotByToken(token string) *Bot { func (c *Connection) createBot(b Bot) error {
var bot Bot return orm.DB.Model(c).Association("Bots").Append(&b).Error
orm.DB.First(&bot, "token = ?", token)
return &bot
} }
func (b *Bot) createBot() error { func getConnectionByBotToken(token string) (*Connection, error) {
return orm.DB.Create(b).Error var c Connection
err := orm.DB.Where("active = ?", true).
Preload("Bots", "token = ?", token).
First(&c).Error
if gorm.IsRecordNotFoundError(err) {
return &c, nil
} else {
return &c, err
}
return &c, nil
}
func getBotByChannel(ch uint64) *Bot {
var bot Bot
orm.DB.First(&bot, "channel = ?", ch)
return &bot
} }
func (b *Bot) setBotActivity() error { func (b *Bot) setBotActivity() error {
@ -48,6 +64,19 @@ func getBotChannelByToken(token string) uint64 {
return b.Channel return b.Channel
} }
func (b *Bots) getBotsByClientID(uid string) error { func (c Connection) getBotsByClientID() Bots {
return orm.DB.Where("client_id = ?", uid).Find(b).Error var b Bots
err := orm.DB.Model(c).Association("Bots").Find(&b).Error
if err != nil {
logger.Error(err)
}
return b
}
func getConnectionById(id int) *Connection {
var connection Connection
orm.DB.First(&connection, "id = ?", id)
return &connection
} }

View File

@ -8,8 +8,7 @@ import (
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"regexp" "regexp"
"strconv" "strings"
"time"
"github.com/getsentry/raven-go" "github.com/getsentry/raven-go"
"github.com/go-telegram-bot-api/telegram-bot-api" "github.com/go-telegram-bot-api/telegram-bot-api"
@ -21,7 +20,6 @@ import (
) )
var ( var (
templates = template.Must(template.ParseFiles("templates/layout.html", "templates/form.html", "templates/home.html"))
validPath = regexp.MustCompile(`^/(save|settings|telegram)/([a-zA-Z0-9-:_+]+)$`) validPath = regexp.MustCompile(`^/(save|settings|telegram)/([a-zA-Z0-9-:_+]+)$`)
localizer *i18n.Localizer localizer *i18n.Localizer
bundle = &i18n.Bundle{DefaultLanguage: language.English} bundle = &i18n.Bundle{DefaultLanguage: language.English}
@ -62,6 +60,8 @@ func setWrapperRoutes() {
http.HandleFunc("/save/", saveHandler) http.HandleFunc("/save/", saveHandler)
http.HandleFunc("/create/", createHandler) http.HandleFunc("/create/", createHandler)
http.HandleFunc("/actions/activity", activityHandler) http.HandleFunc("/actions/activity", activityHandler)
http.HandleFunc("/add-bot/", addBotHandler)
http.HandleFunc("/activity-bot/", activityBotHandler)
} }
func renderTemplate(w http.ResponseWriter, tmpl string, c interface{}) { func renderTemplate(w http.ResponseWriter, tmpl string, c interface{}) {
@ -100,7 +100,7 @@ func connectHandler(w http.ResponseWriter, r *http.Request) {
}{ }{
&p, &p,
map[string]interface{}{ map[string]interface{}{
"ButConnect": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "but_connect"}), "ButtonSave": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "button_save"}),
"ApiKey": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "api_key"}), "ApiKey": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "api_key"}),
"Title": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "title"}), "Title": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "title"}),
}, },
@ -114,6 +114,7 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
raven.CaptureErrorAndWait(err, nil) raven.CaptureErrorAndWait(err, nil)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_adding_bot"}), http.StatusInternalServerError) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_adding_bot"}), http.StatusInternalServerError)
logger.Error(err.Error())
return return
} }
@ -123,6 +124,7 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
raven.CaptureErrorAndWait(err, nil) raven.CaptureErrorAndWait(err, nil)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_adding_bot"}), http.StatusInternalServerError) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_adding_bot"}), http.StatusInternalServerError)
logger.Error(err.Error())
return return
} }
@ -131,8 +133,15 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
cl := getBotByToken(b.Token) cb, err := getConnectionByBotToken(b.Token)
if cl.ID != 0 { if err != nil {
raven.CaptureErrorAndWait(err, nil)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_adding_bot"}), http.StatusInternalServerError)
logger.Error(err.Error())
return
}
if len(cb.Bots) != 0 {
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "bot_already_created"}), http.StatusBadRequest) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "bot_already_created"}), http.StatusBadRequest)
return return
} }
@ -146,29 +155,21 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) {
bot.Debug = false bot.Debug = false
_, err = bot.SetWebhook(tgbotapi.NewWebhook("https://" + config.HTTPServer.Host + "/telegram/" + bot.Token)) wr, err := bot.SetWebhook(tgbotapi.NewWebhook("https://" + config.HTTPServer.Host + "/telegram/" + bot.Token))
if err != nil { if err != nil {
logger.Error(b.Token, err.Error()) logger.Error(b.Token, err.Error())
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_creating_webhook"}), http.StatusBadRequest) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_creating_webhook"}), http.StatusBadRequest)
return return
} }
_, err = bot.GetWebhookInfo() if !wr.Ok {
if err != nil { logger.Error(b.Token, wr.ErrorCode, wr.Result)
logger.Error(b.Token, err.Error())
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_creating_webhook"}), http.StatusBadRequest) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_creating_webhook"}), http.StatusBadRequest)
return return
} }
b.Name = GetBotName(bot) b.Name = GetBotName(bot)
c := getConnection(b.ClientID)
if c.MGURL == "" || c.MGToken == "" {
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "not_found_account"}), http.StatusBadRequest)
logger.Error(b.ClientID, "MGURL or MGToken is empty")
return
}
ch := v1.Channel{ ch := v1.Channel{
Type: "telegram", Type: "telegram",
Events: []string{ Events: []string{
@ -179,22 +180,22 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) {
}, },
} }
var client = v1.New(c.MGURL, c.MGToken) var client = v1.New(cb.MGURL, cb.MGToken)
data, status, err := client.ActivateTransportChannel(ch) data, status, err := client.ActivateTransportChannel(ch)
if status != http.StatusCreated { if status != http.StatusCreated {
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_activating_channel"}), http.StatusBadRequest) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_activating_channel"}), http.StatusBadRequest)
logger.Error(c.APIURL, status, err.Error(), data) logger.Error(cb.APIURL, status, err.Error(), data)
return return
} }
b.Channel = data.ChannelID b.Channel = data.ChannelID
b.Active = true b.Active = true
err = b.createBot() err = cb.createBot(b)
if err != nil { if err != nil {
raven.CaptureErrorAndWait(err, nil) raven.CaptureErrorAndWait(err, nil)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_adding_bot"}), http.StatusInternalServerError) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_adding_bot"}), http.StatusInternalServerError)
logger.Error(c.APIURL, err.Error()) logger.Error(cb.APIURL, err.Error())
return return
} }
@ -202,7 +203,7 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
raven.CaptureErrorAndWait(err, nil) raven.CaptureErrorAndWait(err, nil)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_adding_bot"}), http.StatusInternalServerError) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_adding_bot"}), http.StatusInternalServerError)
logger.Error(c.APIURL, err.Error()) logger.Error(cb.APIURL, err.Error())
return return
} }
@ -215,6 +216,7 @@ func activityBotHandler(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
raven.CaptureErrorAndWait(err, nil) raven.CaptureErrorAndWait(err, nil)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError)
logger.Error(err.Error())
return return
} }
@ -224,6 +226,14 @@ func activityBotHandler(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
raven.CaptureErrorAndWait(err, nil) raven.CaptureErrorAndWait(err, nil)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError)
logger.Error(err.Error())
return
}
c := getConnectionById(b.ConnectionID)
if c.MGURL == "" || c.MGToken == "" {
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "not_found_account"}), http.StatusBadRequest)
logger.Error(b.ID, "MGURL or MGToken is empty")
return return
} }
@ -238,34 +248,28 @@ func activityBotHandler(w http.ResponseWriter, r *http.Request) {
}, },
} }
c := getConnection(b.ClientID)
if c.MGURL == "" || c.MGToken == "" {
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "not_found_account"}), http.StatusBadRequest)
logger.Error(b.ClientID, "MGURL or MGToken is empty")
return
}
var client = v1.New(c.MGURL, c.MGToken) var client = v1.New(c.MGURL, c.MGToken)
if b.Active { if b.Active {
data, status, err := client.DeactivateTransportChannel(ch.ID) data, status, err := client.DeactivateTransportChannel(ch.ID)
if status > http.StatusOK { if status > http.StatusOK {
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_deactivating_channel"}), http.StatusBadRequest) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_deactivating_channel"}), http.StatusBadRequest)
logger.Error(b.ClientID, status, err.Error(), data) logger.Error(b.ID, status, err.Error(), data)
return return
} }
} else { } else {
data, status, err := client.ActivateTransportChannel(ch) data, status, err := client.ActivateTransportChannel(ch)
if status > http.StatusCreated { if status > http.StatusCreated {
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_activating_channel"}), http.StatusBadRequest) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_activating_channel"}), http.StatusBadRequest)
logger.Error(b.ClientID, status, err.Error(), data) logger.Error(b.ID, status, err.Error(), data)
return return
} }
} }
err = b.setBotActivity() err = b.setBotActivity()
if err != nil { if err != nil {
logger.Error(b.ClientID, err.Error()) raven.CaptureErrorAndWait(err, nil)
logger.Error(b.ID, err.Error())
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError)
return return
} }
@ -279,16 +283,11 @@ func settingsHandler(w http.ResponseWriter, r *http.Request, uid string) {
p := getConnection(uid) p := getConnection(uid)
if p.ID == 0 { if p.ID == 0 {
http.Redirect(w, r, "/", http.StatusFound) http.Redirect(w, r, "/", http.StatusFound)
}
bots := Bots{}
err := bots.getBotsByClientID(uid)
if err != nil {
raven.CaptureErrorAndWait(err, nil)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError)
return return
} }
bots := p.getBotsByClientID()
res := struct { res := struct {
Conn *Connection Conn *Connection
Bots Bots Bots Bots
@ -297,7 +296,7 @@ func settingsHandler(w http.ResponseWriter, r *http.Request, uid string) {
p, p,
bots, bots,
map[string]interface{}{ map[string]interface{}{
"ButConnect": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "but_connect"}), "ButtonSave": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "button_save"}),
"ApiKey": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "api_key"}), "ApiKey": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "api_key"}),
"TabSettings": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "tab_settings"}), "TabSettings": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "tab_settings"}),
"TabBots": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "tab_bots"}), "TabBots": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "tab_bots"}),
@ -331,13 +330,19 @@ func saveHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
err = validate(c) err = validateCrmSettings(c)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
logger.Error(c.APIURL, err.Error()) logger.Error(c.APIURL, err.Error())
return return
} }
_, err, code := getAPIClient(c.APIURL, c.APIKEY)
if err != nil {
http.Error(w, err.Error(), code)
return
}
err = c.saveConnection() err = c.saveConnection()
if err != nil { if err != nil {
raven.CaptureErrorAndWait(err, nil) raven.CaptureErrorAndWait(err, nil)
@ -357,6 +362,7 @@ func createHandler(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
raven.CaptureErrorAndWait(err, nil) raven.CaptureErrorAndWait(err, nil)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError)
logger.Error(err.Error())
return return
} }
@ -366,12 +372,13 @@ func createHandler(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
raven.CaptureErrorAndWait(err, nil) raven.CaptureErrorAndWait(err, nil)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_save"}), http.StatusInternalServerError)
logger.Error(c.APIURL, err.Error())
return return
} }
c.ClientID = GenerateToken() c.ClientID = GenerateToken()
err = validate(c) err = validateCrmSettings(c)
if err != nil { if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) http.Error(w, err.Error(), http.StatusBadRequest)
logger.Error(c.APIURL, err.Error()) logger.Error(c.APIURL, err.Error())
@ -384,19 +391,9 @@ func createHandler(w http.ResponseWriter, r *http.Request) {
return return
} }
client := v5.New(c.APIURL, c.APIKEY) client, err, code := getAPIClient(c.APIURL, c.APIKEY)
if err != nil {
cr, status, errr := client.APICredentials() http.Error(w, err.Error(), code)
if errr.RuntimeErr != nil {
raven.CaptureErrorAndWait(errr.RuntimeErr, nil)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "not_found_account"}), http.StatusInternalServerError)
logger.Error(c.APIURL, status, errr.RuntimeErr, cr)
return
}
if !cr.Success {
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "incorrect_url_key"}), http.StatusBadRequest)
logger.Error(c.APIURL, status, errr.ApiErr, cr)
return return
} }
@ -407,7 +404,7 @@ func createHandler(w http.ResponseWriter, r *http.Request) {
Name: "Telegram", Name: "Telegram",
ClientID: c.ClientID, ClientID: c.ClientID,
Logo: fmt.Sprintf( Logo: fmt.Sprintf(
"https://%s/web/telegram_logo.svg", "https://%s/static/telegram_logo.svg",
config.HTTPServer.Host, config.HTTPServer.Host,
), ),
BaseURL: fmt.Sprintf( BaseURL: fmt.Sprintf(
@ -452,6 +449,7 @@ func createHandler(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
raven.CaptureErrorAndWait(err, nil) raven.CaptureErrorAndWait(err, nil)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_creating_connection"}), http.StatusInternalServerError) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_creating_connection"}), http.StatusInternalServerError)
logger.Error(c.APIURL, err.Error())
return return
} }
@ -466,7 +464,8 @@ func createHandler(w http.ResponseWriter, r *http.Request) {
jss, err := json.Marshal(res) jss, err := json.Marshal(res)
if err != nil { if err != nil {
raven.CaptureErrorAndWait(err, nil) raven.CaptureErrorAndWait(err, nil)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_creating_connection"}), http.StatusBadRequest) http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_creating_connection"}), http.StatusInternalServerError)
logger.Error(c.APIURL, err.Error())
return return
} }
@ -544,7 +543,7 @@ func activityHandler(w http.ResponseWriter, r *http.Request) {
w.Write(jsonString) w.Write(jsonString)
} }
func validate(c Connection) error { func validateCrmSettings(c Connection) error {
if c.APIURL == "" || c.APIKEY == "" { if c.APIURL == "" || c.APIKEY == "" {
return errors.New(localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "missing_url_key"})) return errors.New(localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "missing_url_key"}))
} }
@ -556,97 +555,54 @@ func validate(c Connection) error {
return nil return nil
} }
func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string) { func getAPIClient(url, key string) (*v5.Client, error, int) {
ok := make(chan bool) client := v5.New(url, key)
bytes, err := ioutil.ReadAll(r.Body)
if err != nil { cr, status, errr := client.APICredentials()
raven.CaptureErrorAndWait(err, nil) if errr.RuntimeErr != nil {
logger.Error(token, err) raven.CaptureErrorAndWait(errr.RuntimeErr, nil)
return logger.Error(url, status, errr.RuntimeErr, cr)
return nil, errors.New(localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "not_found_account"})), http.StatusInternalServerError
} }
go func() { if !cr.Success {
b := getBotByToken(token) logger.Error(url, status, errr.ApiErr, cr)
if b.ID == 0 { return nil, errors.New(localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "incorrect_url_key"})), http.StatusBadRequest
logger.Error(token, "missing")
return
} }
if !b.Active { if res := checkCredentials(cr.Credentials); len(res) != 0 {
logger.Error(token, "deactivated") logger.Error(url, status, res)
return return nil,
} errors.New(localizer.MustLocalize(&i18n.LocalizeConfig{
MessageID: "missing_credentials",
var update tgbotapi.Update TemplateData: map[string]interface{}{
"Credentials": strings.Join(res, ", "),
err = json.Unmarshal(bytes, &update)
if err != nil {
raven.CaptureErrorAndWait(err, nil)
logger.Error(token, err)
return
}
c := getConnection(b.ClientID)
if c.MGURL == "" || c.MGToken == "" {
logger.Error(token, "MGURL or MGToken is empty")
return
}
var client = v1.New(c.MGURL, c.MGToken)
if update.Message != nil {
snd := v1.SendData{
Message: v1.SendMessage{
Message: v1.Message{
ExternalID: strconv.Itoa(update.Message.MessageID),
Type: "text",
Text: update.Message.Text,
}, },
SentAt: time.Now(), })),
}, http.StatusBadRequest
User: v1.User{
ExternalID: strconv.Itoa(update.Message.From.ID),
Nickname: update.Message.From.UserName,
Firstname: update.Message.From.FirstName,
},
Channel: b.Channel,
} }
data, status, err := client.Messages(snd) return client, nil, 0
if err != nil { }
logger.Error(token, err.Error(), status, data)
ok <- false func checkCredentials(credential []string) []string {
return rc := []string{
"/api/integration-modules/{code}",
"/api/integration-modules/{code}/edit",
}
for kn, vn := range rc {
for _, vc := range credential {
if vn == vc {
if len(rc) == 1 {
rc = rc[:0]
break
}
rc = append(rc[:kn], rc[kn+1:]...)
}
} }
} }
if update.EditedMessage != nil { return rc
snd := v1.UpdateData{
Message: v1.UpdateMessage{
Message: v1.Message{
ExternalID: strconv.Itoa(update.EditedMessage.MessageID),
Type: "text",
Text: update.EditedMessage.Text,
},
},
Channel: b.Channel,
}
data, status, err := client.UpdateMessages(snd)
if err != nil {
logger.Error(token, err.Error(), status, data)
ok <- false
return
}
}
ok <- true
}()
if <-ok {
w.WriteHeader(http.StatusOK)
w.Write([]byte("SendMessage"))
} else {
w.WriteHeader(http.StatusBadRequest)
}
} }

View File

@ -1,16 +1,29 @@
package main package main
import ( import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"net/url"
"sort"
"strings" "strings"
"testing" "testing"
"github.com/h2non/gock" "github.com/h2non/gock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
) )
func init() { func init() {
config = LoadConfig("config_test.yml")
orm = NewDb(config)
logger = newLogger()
c := Connection{ c := Connection{
ID: 1,
ClientID: "123123", ClientID: "123123",
APIKEY: "test", APIKEY: "test",
APIURL: "https://test.retailcrm.ru", APIURL: "https://test.retailcrm.ru",
@ -20,7 +33,9 @@ func init() {
} }
c.createConnection() c.createConnection()
orm.DB.Where("token = 123123:Qwerty").Delete(Bot{})
} }
func TestRouting_connectHandler(t *testing.T) { func TestRouting_connectHandler(t *testing.T) {
req, err := http.NewRequest("GET", "/", nil) req, err := http.NewRequest("GET", "/", nil)
if err != nil { if err != nil {
@ -31,16 +46,15 @@ func TestRouting_connectHandler(t *testing.T) {
handler := http.HandlerFunc(connectHandler) handler := http.HandlerFunc(connectHandler)
handler.ServeHTTP(rr, req) handler.ServeHTTP(rr, req)
assert.Equal(t, http.StatusOK, rr.Code,
if rr.Code != http.StatusOK { fmt.Sprintf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusOK))
t.Errorf("handler returned wrong status code: got %v want %v",
rr.Code, http.StatusOK)
}
} }
func TestRouting_addBotHandler(t *testing.T) { func TestRouting_addBotHandler(t *testing.T) {
defer gock.Off() defer gock.Off()
p := url.Values{"url": {"https://test.com/telegram/123123:Qwerty"}}
gock.New("https://api.telegram.org"). gock.New("https://api.telegram.org").
Post("/bot123123:Qwerty/getMe"). Post("/bot123123:Qwerty/getMe").
Reply(200). Reply(200).
@ -49,7 +63,7 @@ func TestRouting_addBotHandler(t *testing.T) {
gock.New("https://api.telegram.org"). gock.New("https://api.telegram.org").
Post("/bot123123:Qwerty/setWebhook"). Post("/bot123123:Qwerty/setWebhook").
MatchType("url"). MatchType("url").
BodyString("url=https%3A%2F%2Ftest.com%2Ftelegram%2F123123%3AQwerty"). BodyString(p.Encode()).
Reply(201). Reply(201).
BodyString(`{"ok":true}`) BodyString(`{"ok":true}`)
@ -66,19 +80,29 @@ func TestRouting_addBotHandler(t *testing.T) {
Reply(201). Reply(201).
BodyString(`{"id": 1}`) BodyString(`{"id": 1}`)
req, err := http.NewRequest("POST", "/add-bot/", strings.NewReader(`{"token": "123123:Qwerty", "clientId": "123123"}`)) req, err := http.NewRequest("POST", "/add-bot/", strings.NewReader(`{"token": "123123:Qwerty", "connectionId": 1}`))
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(addBotHandler)
handler.ServeHTTP(rr, req)
require.Equal(t, http.StatusCreated, rr.Code,
fmt.Sprintf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusCreated))
bytes, err := ioutil.ReadAll(rr.Body)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
rr := httptest.NewRecorder() var res map[string]interface{}
handler := http.HandlerFunc(addBotHandler)
handler.ServeHTTP(rr, req)
if rr.Code != http.StatusCreated { err = json.Unmarshal(bytes, &res)
t.Errorf("handler returned wrong status code: got %v want %v", if err != nil {
rr.Code, http.StatusCreated) t.Fatal(err)
} }
assert.Equal(t, "123123:Qwerty", res["token"])
} }
func TestRouting_activityBotHandler(t *testing.T) { func TestRouting_activityBotHandler(t *testing.T) {
@ -92,7 +116,7 @@ func TestRouting_activityBotHandler(t *testing.T) {
Reply(200). Reply(200).
BodyString(`{"id": 1}`) BodyString(`{"id": 1}`)
req, err := http.NewRequest("POST", "/activity-bot/", strings.NewReader(`{"token": "123123:Qwerty", "active": false, "clientId": "123123"}`)) req, err := http.NewRequest("POST", "/activity-bot/", strings.NewReader(`{"token": "123123:Qwerty", "active": false, "connectionId": 1}`))
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -101,10 +125,8 @@ func TestRouting_activityBotHandler(t *testing.T) {
handler := http.HandlerFunc(activityBotHandler) handler := http.HandlerFunc(activityBotHandler)
handler.ServeHTTP(rr, req) handler.ServeHTTP(rr, req)
if rr.Code != http.StatusOK { assert.Equal(t, http.StatusOK, rr.Code,
t.Errorf("handler returned wrong status code: got %v want %v", fmt.Sprintf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusOK))
rr.Code, http.StatusOK)
}
} }
func TestRouting_settingsHandler(t *testing.T) { func TestRouting_settingsHandler(t *testing.T) {
@ -117,13 +139,18 @@ func TestRouting_settingsHandler(t *testing.T) {
handler := http.HandlerFunc(makeHandler(settingsHandler)) handler := http.HandlerFunc(makeHandler(settingsHandler))
handler.ServeHTTP(rr, req) handler.ServeHTTP(rr, req)
if rr.Code != http.StatusOK { assert.Equal(t, http.StatusOK, rr.Code,
t.Errorf("handler returned wrong status code: got %v want %v", fmt.Sprintf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusOK))
rr.Code, http.StatusOK)
}
} }
func TestRouting_saveHandler(t *testing.T) { func TestRouting_saveHandler(t *testing.T) {
defer gock.Off()
gock.New("https://test.retailcrm.ru").
Get("/api/credentials").
Reply(200).
BodyString(`{"success": true, "credentials": ["/api/integration-modules/{code}", "/api/integration-modules/{code}/edit"]}`)
req, err := http.NewRequest("POST", "/save/", req, err := http.NewRequest("POST", "/save/",
strings.NewReader( strings.NewReader(
`{"clientId": "123123", `{"clientId": "123123",
@ -138,10 +165,8 @@ func TestRouting_saveHandler(t *testing.T) {
handler := http.HandlerFunc(saveHandler) handler := http.HandlerFunc(saveHandler)
handler.ServeHTTP(rr, req) handler.ServeHTTP(rr, req)
if rr.Code != http.StatusOK { assert.Equal(t, http.StatusOK, rr.Code,
t.Errorf("handler returned wrong status code: got %v want %v", fmt.Sprintf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusOK))
rr.Code, http.StatusOK)
}
} }
func TestRouting_activityHandler(t *testing.T) { func TestRouting_activityHandler(t *testing.T) {
@ -157,8 +182,41 @@ func TestRouting_activityHandler(t *testing.T) {
handler := http.HandlerFunc(activityHandler) handler := http.HandlerFunc(activityHandler)
handler.ServeHTTP(rr, req) handler.ServeHTTP(rr, req)
if rr.Code != http.StatusOK { assert.Equal(t, http.StatusOK, rr.Code,
t.Errorf("handler returned wrong status code: got %v want %v", fmt.Sprintf("handler returned wrong status code: got %v want %v", rr.Code, http.StatusOK))
rr.Code, http.StatusOK) }
func TestRouting_TranslateLoader(t *testing.T) {
type m map[string]string
te := [][]string{}
dt := "translate"
files, err := ioutil.ReadDir(dt)
if err != nil {
t.Fatal(err)
}
for _, f := range files {
ms := m{}
if !f.IsDir() {
res, err := ioutil.ReadFile(fmt.Sprintf("%s/%s", dt, f.Name()))
if err != nil {
t.Fatal(err)
}
err = yaml.Unmarshal(res, &ms)
if err != nil {
t.Fatal(err)
}
keys := []string{}
for kms := range ms {
keys = append(keys, kms)
}
sort.Strings(keys)
te = append(te, keys)
} }
} }
}

14
run.go
View File

@ -7,16 +7,11 @@ import (
"os/signal" "os/signal"
"syscall" "syscall"
"github.com/getsentry/raven-go"
_ "github.com/golang-migrate/migrate/database/postgres" _ "github.com/golang-migrate/migrate/database/postgres"
_ "github.com/golang-migrate/migrate/source/file" _ "github.com/golang-migrate/migrate/source/file"
) )
var (
config = LoadConfig("config.yml")
orm = NewDb(config)
logger = newLogger()
)
func init() { func init() {
parser.AddCommand("run", parser.AddCommand("run",
"Run mg-telegram", "Run mg-telegram",
@ -30,6 +25,11 @@ type RunCommand struct{}
// Execute command // Execute command
func (x *RunCommand) Execute(args []string) error { func (x *RunCommand) Execute(args []string) error {
config = LoadConfig(options.Config)
orm = NewDb(config)
logger = newLogger()
raven.SetDSN(config.SentryDSN)
go start() go start()
c := make(chan os.Signal, 1) c := make(chan os.Signal, 1)
@ -49,6 +49,6 @@ func (x *RunCommand) Execute(args []string) error {
func start() { func start() {
setWrapperRoutes() setWrapperRoutes()
setTransportRoutes() setTransportRoutes()
http.Handle("/web/", http.StripPrefix("/web/", http.FileServer(http.Dir("web")))) http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.ListenAndServe(config.HTTPServer.Listen, nil) http.ListenAndServe(config.HTTPServer.Listen, nil)
} }

View File

@ -24,7 +24,10 @@ $("#add-bot").on("submit", function(e) {
e.preventDefault(); e.preventDefault();
send( send(
$(this).attr('action'), $(this).attr('action'),
formDataToObj($(this).serializeArray()), {
connectionId: parseInt($(this).find('input[name=connectionId]').val()),
token: $(this).find('input[name=token]').val(),
},
function (data) { function (data) {
let bots = $("#bots"); let bots = $("#bots");
if (bots.hasClass("hide")) { if (bots.hasClass("hide")) {
@ -42,7 +45,7 @@ $(document).on("click", ".activity-bot", function(e) {
{ {
token: but.attr("data-token"), token: but.attr("data-token"),
active: (but.attr("data-activity") === 'true'), active: (but.attr("data-activity") === 'true'),
clientId: $('input[name=clientId]').val(), connectionId: parseInt($('input[name=connectionId]').val()),
}, },
function () { function () {
if (but.attr("data-activity") === 'true') { if (but.attr("data-activity") === 'true') {

View File

Before

Width:  |  Height:  |  Size: 999 B

After

Width:  |  Height:  |  Size: 999 B

View File

@ -1,15 +1,20 @@
package main package main
import ( import (
"encoding/json"
"io/ioutil"
"net/http" "net/http"
"strconv"
"time"
"github.com/getsentry/raven-go"
"github.com/go-telegram-bot-api/telegram-bot-api" "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/retailcrm/mg-transport-api-client-go/v1"
) )
func setTransportRoutes() { func setTransportRoutes() {
http.HandleFunc("/add-bot/", addBotHandler)
http.HandleFunc("/activity-bot/", activityBotHandler)
http.HandleFunc("/telegram/", makeHandler(telegramWebhookHandler)) http.HandleFunc("/telegram/", makeHandler(telegramWebhookHandler))
http.HandleFunc("/webhook/", mgWebhookHandler)
} }
// GetBotInfo function // GetBotInfo function
@ -25,3 +30,176 @@ func GetBotInfo(token string) (*tgbotapi.BotAPI, error) {
func GetBotName(bot *tgbotapi.BotAPI) string { func GetBotName(bot *tgbotapi.BotAPI) string {
return bot.Self.FirstName return bot.Self.FirstName
} }
func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string) {
c, err := getConnectionByBotToken(token)
if err != nil {
raven.CaptureErrorAndWait(err, nil)
logger.Error(token, err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
if len(c.Bots) == 0 {
logger.Error(token, "missing")
w.WriteHeader(http.StatusBadRequest)
return
}
if !c.Bots[0].Active {
logger.Error(token, "deactivated")
w.WriteHeader(http.StatusBadRequest)
return
}
var update tgbotapi.Update
bytes, err := ioutil.ReadAll(r.Body)
if err != nil {
raven.CaptureErrorAndWait(err, nil)
w.WriteHeader(http.StatusInternalServerError)
logger.Error(token, err)
return
}
err = json.Unmarshal(bytes, &update)
if err != nil {
raven.CaptureErrorAndWait(err, nil)
logger.Error(token, err)
w.WriteHeader(http.StatusInternalServerError)
return
}
var client = v1.New(c.MGURL, c.MGToken)
if update.Message != nil {
snd := v1.SendData{
Message: v1.SendMessage{
Message: v1.Message{
ExternalID: strconv.Itoa(update.Message.MessageID),
Type: "text",
Text: update.Message.Text,
},
SentAt: time.Now(),
},
User: v1.User{
ExternalID: strconv.Itoa(update.Message.From.ID),
Nickname: update.Message.From.UserName,
Firstname: update.Message.From.FirstName,
Lastname: update.Message.From.LastName,
Language: update.Message.From.LanguageCode,
},
Channel: c.Bots[0].Channel,
}
data, st, err := client.Messages(snd)
if err != nil {
raven.CaptureErrorAndWait(err, nil)
logger.Error(token, err.Error(), st, data)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
if update.EditedMessage != nil {
snd := v1.UpdateData{
Message: v1.UpdateMessage{
Message: v1.Message{
ExternalID: strconv.Itoa(update.EditedMessage.MessageID),
Type: "text",
Text: update.EditedMessage.Text,
},
},
Channel: c.Bots[0].Channel,
}
data, st, err := client.UpdateMessages(snd)
if err != nil {
raven.CaptureErrorAndWait(err, nil)
logger.Error(token, err.Error(), st, data)
w.WriteHeader(http.StatusInternalServerError)
return
}
}
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 {
raven.CaptureErrorAndWait(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 {
raven.CaptureErrorAndWait(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 {
raven.CaptureErrorAndWait(err, nil)
logger.Error(err)
w.WriteHeader(http.StatusBadRequest)
return
}
logger.Debugf("%v", msg)
w.WriteHeader(http.StatusOK)
w.Write([]byte("Message deleted"))
}
}

View File

@ -9,7 +9,7 @@
<div id="tab1" class="col s12"> <div id="tab1" class="col s12">
<div class="row indent-top"> <div class="row indent-top">
<form id="save" class="tab-el-center" action="/save/" method="POST"> <form id="save" class="tab-el-center" action="/save/" method="POST">
<input name="clientId" type="hidden" value="{{.Conn.ClientID}}"> <input name="connectionId" type="hidden" value="{{.Conn.ID}}">
<div class="row"> <div class="row">
<div class="input-field col s12"> <div class="input-field col s12">
<input placeholder="API Url" id="api_url" name="api_url" type="text" class="validate" value="{{.Conn.APIURL}}"> <input placeholder="API Url" id="api_url" name="api_url" type="text" class="validate" value="{{.Conn.APIURL}}">
@ -23,7 +23,7 @@
<div class="row"> <div class="row">
<div class="input-field col s12 center-align"> <div class="input-field col s12 center-align">
<button class="btn waves-effect waves-light light-blue darken-1" type="submit" name="action"> <button class="btn waves-effect waves-light light-blue darken-1" type="submit" name="action">
{{.Locale.ButConnect}} {{.Locale.ButtonSave}}
<i class="material-icons right">sync</i> <i class="material-icons right">sync</i>
</button> </button>
</div> </div>
@ -34,7 +34,7 @@
<div id="tab2" class="col s12"> <div id="tab2" class="col s12">
<div class="row indent-top"> <div class="row indent-top">
<form id="add-bot" class="tab-el-center" action="/add-bot/" method="POST"> <form id="add-bot" class="tab-el-center" action="/add-bot/" method="POST">
<input name="clientId" type="hidden" value="{{.Conn.ClientID}}"> <input name="connectionId" type="hidden" value="{{.Conn.ID}}">
<div class="row"> <div class="row">
<div class="input-field col s12"> <div class="input-field col s12">
<input placeholder="{{.Locale.TableToken}}" id="token" name="token" type="text" class="validate"> <input placeholder="{{.Locale.TableToken}}" id="token" name="token" type="text" class="validate">

View File

@ -15,7 +15,7 @@
<div class="row"> <div class="row">
<div class="input-field col s12 center-align"> <div class="input-field col s12 center-align">
<button class="btn waves-effect waves-light light-blue darken-1" type="submit" name="action"> <button class="btn waves-effect waves-light light-blue darken-1" type="submit" name="action">
{{.Locale.ButConnect}} {{.Locale.ButtonSave}}
<i class="material-icons right">sync</i> <i class="material-icons right">sync</i>
</button> </button>
</div> </div>

View File

@ -3,9 +3,9 @@
<meta title="Telegram transport"> <meta title="Telegram transport">
<meta name="viewport" content="width=device-width, initial-scale=1.0"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>{{.Locale.Title}}</title> <title>{{.Locale.Title}}</title>
<link rel="stylesheet" href="../web/materialize.min.css"> <link rel="stylesheet" href="/static/materialize.min.css">
<link rel="stylesheet" href="../web/font.css" > <link rel="stylesheet" href="/static/font.css" >
<link rel="stylesheet" href="../web/style.css" > <link rel="stylesheet" href="/static/style.css" >
</head> </head>
<body> <body>
<div class="container"> <div class="container">
@ -14,8 +14,8 @@
</div> </div>
{{template "body" .}} {{template "body" .}}
</div> </div>
<script src="../web/materialize.min.js"></script> <script src="/static/materialize.min.js"></script>
<script src="../web/jquery-3.3.1.min.js"></script> <script src="/static/jquery-3.3.1.min.js"></script>
<script src="../web/script.js"></script> <script src="/static/script.js"></script>
</body> </body>
</html> </html>

View File

@ -1,4 +1,4 @@
but_connect: Save button_save: Save
tab_settings: Connection settings tab_settings: Connection settings
tab_bots: Bots tab_bots: Bots
table_name: Bot name table_name: Bot name
@ -26,3 +26,4 @@ incorrect_token: Set correct bot token
error_creating_webhook: Error while creating webhook error_creating_webhook: Error while creating webhook
error_adding_bot: Error while adding bot error_adding_bot: Error while adding bot
error_save: An error occurred while saving, contact technical support error_save: An error occurred while saving, contact technical support
missing_credentials: "Necessary credentials: {{.Credentials}}"

View File

@ -1,4 +1,4 @@
but_connect: Сохранить button_save: Сохранить
tab_settings: Настройки CRM tab_settings: Настройки CRM
tab_bots: Боты tab_bots: Боты
table_name: Имя table_name: Имя
@ -26,3 +26,4 @@ incorrect_token: Установите корректный токен
error_creating_webhook: Ошибка при создании webhook error_creating_webhook: Ошибка при создании webhook
error_adding_bot: Ошибка при добавлении бота error_adding_bot: Ошибка при добавлении бота
error_save: Ошибка при сохранении, обратитесь в службу технической поддержки error_save: Ошибка при сохранении, обратитесь в службу технической поддержки
missing_credentials: "Необходимые методы: {{.Credentials}}"