1
0
mirror of synced 2024-11-22 04:26:01 +03:00

Merge pull request #2 from gwinn/master

* update build files
* add static files
* add translations
* minor fixes
* improve templates & settings data
This commit is contained in:
Alex Lushpai 2018-05-23 17:44:23 +03:00 committed by GitHub
commit d1a9e53560
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
29 changed files with 677 additions and 378 deletions

2
.gitignore vendored
View File

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

View File

@ -1,12 +1,13 @@
FROM golang:1.9.3-stretch as ca-certs
FROM scratch
COPY --from=ca-certs /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
FROM golang:1.9.3-stretch
WORKDIR /
ADD ./bin/mg-telegram /
ADD ./templates/ /templates/
ADD ./web/ /web/
ADD ./translate/ /translate/
EXPOSE 3001
ENTRYPOINT ["/mg-telegram", "--config", "/config.yml"]
ENTRYPOINT ["/mg-telegram"]
CMD ["run"]

View File

@ -1,8 +1,9 @@
ROOT_DIR=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
SRC_DIR=$(ROOT_DIR)
MIGRATIONS_DIR=$(ROOT_DIR)/migrations
CONFIG_FILE=$(ROOT_DIR)/config.yml
CONFIG_TEST_FILE=$(ROOT_DIR)/config_test.yml
BIN=$(ROOT_DIR)/mg-telegram
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
@ -11,25 +12,31 @@ endif
export GOPATH := $(GOPATH):$(ROOT_DIR)
build: deps fmt
@echo "==> Building"
@go build -o $(BIN) -ldflags "-X common.build=${REVISION}" .
@echo $(BIN)
run: migrate
@echo "==> Running"
@${BIN} --config $(CONFIG_FILE) run
fmt:
@echo "==> Running gofmt"
@gofmt -l -s -w $(SRC_DIR)
install: fmt
@echo "==> Running go get"
deps:
@echo "==> Installing dependencies"
$(eval DEPS:=$(shell cd $(SRC_DIR) \
&& go list -f '{{join .Imports "\n"}}{{ "\n" }}{{join .TestImports "\n"}}' ./... \
| sort | uniq | tr '\r' '\n' | paste -sd ' ' -))
@go get -d -v $(DEPS)
build: install
@echo "==> Building"
@go build -o $(BIN) -ldflags "-X common.build=${REVISION}" .
@echo $(BIN)
migrate: build
@${BIN} --config $(CONFIG_FILE) migrate -p ./migrations/
run: migrate
@echo "==> Running"
@${BIN} --config $(CONFIG_FILE) run
migrate_test: build
@${BIN} --config $(CONFIG_TEST_FILE) migrate
migrate_down: build
@${BIN} --config $(CONFIG_FILE) migrate -v down

View File

@ -12,4 +12,3 @@ create table connection
updated_at timestamp with time zone,
active boolean
);

View File

@ -11,4 +11,3 @@ create table bot
updated_at timestamp with time zone,
active boolean
);

View File

@ -6,4 +6,3 @@ create table mapping
site_code text,
bot_id text
);

View File

@ -0,0 +1,11 @@
alter table connection
alter column client_id type text,
alter column api_key type text,
alter column api_url type text,
alter column mg_url type text,
alter column mg_token type text;
alter table bot
alter column client_id type text,
alter column name type text,
alter column token type text;

View File

@ -0,0 +1,11 @@
alter table connection
alter column client_id type varchar(70),
alter column api_key type varchar(100),
alter column api_url type varchar(100),
alter column mg_url type varchar(100),
alter column mg_token type varchar(100);
alter table bot
alter column client_id type varchar(70),
alter column name type varchar(40),
alter column token type varchar(100);

View File

@ -27,12 +27,5 @@ type Bot struct {
Active bool `json:"active,omitempty"`
}
// Mapping model
type Mapping struct {
ID int `gorm:"primary_key"`
SiteCode string `gorm:"site_code" json:"site_code,omitempty"`
BotID string `gorm:"bot_id" json:"bot_id,omitempty"`
}
//Bots list
type Bots []Bot

View File

@ -1,28 +1,5 @@
package main
func createMapping(s []Mapping) error {
tx := orm.DB.Begin()
if tx.Error != nil {
return tx.Error
}
defer func() {
if r := recover(); r != nil {
logger.Warning(r)
tx.Rollback()
}
}()
for _, val := range s {
if err := tx.Create(&val).Error; err != nil {
tx.Rollback()
return err
}
}
return tx.Commit().Error
}
func getConnection(uid string) (*Connection, error) {
var connection Connection
orm.DB.First(&connection, "client_id = ?", uid)

View File

@ -10,15 +10,43 @@ import (
"regexp"
"github.com/getsentry/raven-go"
"github.com/nicksnyder/go-i18n/v2/i18n"
"github.com/retailcrm/api-client-go/v5"
"github.com/retailcrm/mg-transport-api-client-go/v1"
"golang.org/x/text/language"
"gopkg.in/yaml.v2"
)
var (
templates = template.Must(template.ParseFiles("templates/form.html", "templates/home.html"))
templates = template.Must(template.ParseFiles("templates/layout.html", "templates/form.html", "templates/home.html"))
validPath = regexp.MustCompile("^/(save|settings)/([a-zA-Z0-9]+)$")
localizer *i18n.Localizer
bundle = &i18n.Bundle{DefaultLanguage: language.English}
matcher = language.NewMatcher([]language.Tag{
language.English,
language.Russian,
language.Spanish,
})
)
func init() {
bundle.RegisterUnmarshalFunc("yml", yaml.Unmarshal)
files, err := ioutil.ReadDir("translate")
if err != nil {
logger.Error(err)
}
for _, f := range files {
if !f.IsDir() {
bundle.MustLoadMessageFile("translate/" + f.Name())
}
}
}
func setLocale(al string) {
tag, _ := language.MatchStrings(matcher, al)
localizer = i18n.NewLocalizer(bundle, tag.String())
}
// Response struct
type Response struct {
Success bool `json:"success"`
@ -53,8 +81,20 @@ func makeHandler(fn func(http.ResponseWriter, *http.Request, string)) http.Handl
}
func connectHandler(w http.ResponseWriter, r *http.Request) {
setLocale(r.Header.Get("Accept-Language"))
p := Connection{}
renderTemplate(w, "home", &p)
res := struct {
Conn *Connection
Locale map[string]interface{}
}{
&p,
map[string]interface{}{
"ButConnect": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "but_connect"}),
"ApiKey": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "api_key"}),
},
}
renderTemplate(w, "home", &res)
}
func addBotHandler(w http.ResponseWriter, r *http.Request) {
@ -73,20 +113,20 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) {
}
if b.Token == "" {
http.Error(w, "set bot token", http.StatusInternalServerError)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "no_bot_token"}), http.StatusInternalServerError)
return
}
cl, _ := getBotByToken(b.Token)
if cl.ID != 0 {
http.Error(w, "bot already created", http.StatusInternalServerError)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "bot_already_created"}), http.StatusInternalServerError)
return
}
bot, err := GetBotInfo(b.Token)
if err != nil {
logger.Error(b.Token, err.Error())
http.Error(w, "set correct bot token", http.StatusInternalServerError)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "incorrect_token"}), http.StatusInternalServerError)
return
}
@ -94,13 +134,13 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) {
c, err := getConnection(b.ClientID)
if err != nil {
http.Error(w, "could not find account, please contact technical support", http.StatusInternalServerError)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "not_find_account"}), http.StatusInternalServerError)
logger.Error(b.ClientID, err.Error())
return
}
if c.MGURL == "" || c.MGToken == "" {
http.Error(w, "could not find account, please contact technical support", http.StatusInternalServerError)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "not_find_account"}), http.StatusInternalServerError)
logger.Error(b.ClientID)
return
}
@ -118,7 +158,7 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) {
var client = v1.New(c.MGURL, c.MGToken)
data, status, err := client.ActivateTransportChannel(ch)
if status != http.StatusCreated {
http.Error(w, "error while activating the channel", http.StatusInternalServerError)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_activating_channel"}), http.StatusInternalServerError)
logger.Error(c.APIURL, status, err.Error(), data)
return
}
@ -133,7 +173,9 @@ func addBotHandler(w http.ResponseWriter, r *http.Request) {
return
}
jsonString, _ := json.Marshal(b)
w.WriteHeader(http.StatusCreated)
w.Write(jsonString)
}
func activityBotHandler(w http.ResponseWriter, r *http.Request) {
@ -164,14 +206,14 @@ func activityBotHandler(w http.ResponseWriter, r *http.Request) {
c, err := getConnection(b.ClientID)
if err != nil {
http.Error(w, "could not find account, please contact technical support", http.StatusInternalServerError)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "not_find_account"}), http.StatusInternalServerError)
logger.Error(b.ClientID, err.Error())
return
}
if c.MGURL == "" || c.MGToken == "" {
http.Error(w, "could not find account, please contact technical support", http.StatusInternalServerError)
logger.Error(b.ClientID, "could not find account, please contact technical support")
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "not_find_account"}), http.StatusInternalServerError)
logger.Error(b.ClientID, "not find account")
return
}
@ -180,14 +222,14 @@ func activityBotHandler(w http.ResponseWriter, r *http.Request) {
if b.Active {
data, status, err := client.DeactivateTransportChannel(ch.ID)
if status > http.StatusOK {
http.Error(w, "error while deactivating the channel", http.StatusInternalServerError)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_deactivating_channel"}), http.StatusInternalServerError)
logger.Error(b.ClientID, status, err.Error(), data)
return
}
} else {
data, status, err := client.ActivateTransportChannel(ch)
if status > http.StatusCreated {
http.Error(w, "error while activating the channel", http.StatusInternalServerError)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_activating_channel"}), http.StatusInternalServerError)
logger.Error(b.ClientID, status, err.Error(), data)
return
}
@ -203,31 +245,9 @@ func activityBotHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}
func mappingHandler(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
raven.CaptureErrorAndWait(err, nil)
return
}
var rec []Mapping
err = json.Unmarshal(body, &rec)
if err != nil {
raven.CaptureErrorAndWait(err, nil)
return
}
err = createMapping(rec)
if err != nil {
raven.CaptureErrorAndWait(err, nil)
return
}
w.WriteHeader(http.StatusOK)
}
func settingsHandler(w http.ResponseWriter, r *http.Request, uid string) {
setLocale(r.Header.Get("Accept-Language"))
p, err := getConnection(uid)
if err != nil {
raven.CaptureErrorAndWait(err, nil)
@ -249,10 +269,21 @@ func settingsHandler(w http.ResponseWriter, r *http.Request, uid string) {
Conn *Connection
Bots Bots
Sites map[string]v5.Site
Locale map[string]interface{}
}{
p,
bots,
sites.Sites,
map[string]interface{}{
"ButConnect": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "but_connect"}),
"ApiKey": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "api_key"}),
"TabSettings": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "tab_settings"}),
"TabBots": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "tab_bots"}),
"TableName": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "table_name"}),
"TableToken": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "table_token"}),
"AddBot": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "add_bot"}),
"TableActivity": localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "table_activity"}),
},
}
renderTemplate(w, "form", res)
@ -292,13 +323,25 @@ func saveHandler(w http.ResponseWriter, r *http.Request) {
}
func createHandler(w http.ResponseWriter, r *http.Request) {
c := Connection{
ClientID: GenerateToken(),
APIURL: string([]byte(r.FormValue("api_url"))),
APIKEY: string([]byte(r.FormValue("api_key"))),
setLocale(r.Header.Get("Accept-Language"))
body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
err := validate(c)
var c Connection
err = json.Unmarshal(body, &c)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
c.ClientID = GenerateToken()
err = validate(c)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
logger.Error(c.APIURL, err.Error())
@ -307,7 +350,7 @@ func createHandler(w http.ResponseWriter, r *http.Request) {
cl, _ := getConnectionByURL(c.APIURL)
if cl.ID != 0 {
http.Error(w, "connection already created", http.StatusBadRequest)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "connection_already_created"}), http.StatusBadRequest)
return
}
@ -315,14 +358,14 @@ func createHandler(w http.ResponseWriter, r *http.Request) {
cr, status, errr := client.APICredentials()
if errr.RuntimeErr != nil {
http.Error(w, "set correct crm url or key", http.StatusBadRequest)
logger.Error(c.APIURL, status, err.Error(), cr)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "incorrect_url_key"}), http.StatusBadRequest)
logger.Error(c.APIURL, status, errr.RuntimeErr, cr)
return
}
if !cr.Success {
http.Error(w, "set correct crm url or key", http.StatusBadRequest)
logger.Error(c.APIURL, status, err.Error(), cr)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "incorrect_url_key"}), http.StatusBadRequest)
logger.Error(c.APIURL, status, errr.ApiErr, cr)
return
}
@ -330,11 +373,18 @@ func createHandler(w http.ResponseWriter, r *http.Request) {
Code: transport,
IntegrationCode: transport,
Active: true,
Name: "MG Telegram",
Name: "Telegram",
ClientID: c.ClientID,
BaseURL: config.HTTPServer.Host,
Logo: fmt.Sprintf(
"https://%s/web/telegram_logo.svg",
config.HTTPServer.Host,
),
BaseURL: fmt.Sprintf(
"https://%s",
config.HTTPServer.Host,
),
AccountURL: fmt.Sprintf(
"%s/settings/%s",
"https://%s/settings/%s",
config.HTTPServer.Host,
c.ClientID,
),
@ -342,7 +392,7 @@ func createHandler(w http.ResponseWriter, r *http.Request) {
Integrations: &v5.Integrations{
MgTransport: &v5.MgTransport{
WebhookUrl: fmt.Sprintf(
"%s/webhook",
"https://%s/webhook",
config.HTTPServer.Host,
),
},
@ -351,7 +401,7 @@ func createHandler(w http.ResponseWriter, r *http.Request) {
data, status, errr := client.IntegrationModuleEdit(integration)
if errr.RuntimeErr != nil {
http.Error(w, "error while creating integration", http.StatusBadRequest)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_creating_integration"}), http.StatusBadRequest)
logger.Error(c.APIURL, status, errr.RuntimeErr, data)
return
}
@ -368,7 +418,7 @@ func createHandler(w http.ResponseWriter, r *http.Request) {
err = c.createConnection()
if err != nil {
raven.CaptureErrorAndWait(err, nil)
http.Error(w, "error while creating connection", http.StatusInternalServerError)
http.Error(w, localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "error_creating_connection"}), http.StatusInternalServerError)
return
}
@ -381,7 +431,7 @@ func activityHandler(w http.ResponseWriter, r *http.Request) {
res := Response{Success: false}
if r.Method != http.MethodPost {
res.Error = "set POST"
res.Error = localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "set_method"})
jsonString, _ := json.Marshal(res)
w.Write(jsonString)
return
@ -390,7 +440,7 @@ func activityHandler(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
raven.CaptureErrorAndWait(err, nil)
res.Error = "incorrect data"
res.Error = localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "incorrect_data"})
jsonString, _ := json.Marshal(res)
w.Write(jsonString)
return
@ -401,7 +451,7 @@ func activityHandler(w http.ResponseWriter, r *http.Request) {
err = json.Unmarshal(body, &rec)
if err != nil {
raven.CaptureErrorAndWait(err, nil)
res.Error = "incorrect data"
res.Error = localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "incorrect_data"})
jsonString, _ := json.Marshal(res)
w.Write(jsonString)
return
@ -409,7 +459,7 @@ func activityHandler(w http.ResponseWriter, r *http.Request) {
if err := rec.setConnectionActivity(); err != nil {
raven.CaptureErrorAndWait(err, nil)
res.Error = "incorrect data"
res.Error = localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "incorrect_data"})
jsonString, _ := json.Marshal(res)
w.Write(jsonString)
return
@ -422,11 +472,11 @@ func activityHandler(w http.ResponseWriter, r *http.Request) {
func validate(c Connection) error {
if c.APIURL == "" || c.APIKEY == "" {
return errors.New("missing crm url or key")
return errors.New(localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "missing_url_key"}))
}
if res, _ := regexp.MatchString(`https://?[\da-z\.-]+\.(retailcrm\.(ru|pro)|ecomlogic\.com)`, c.APIURL); !res {
return errors.New("set correct crm url")
return errors.New(localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: "incorrect_url"}))
}
return nil

73
routing_test.go Normal file
View File

@ -0,0 +1,73 @@
package main
import (
"net/http"
"net/http/httptest"
"testing"
"strings"
"encoding/json"
"github.com/h2non/gock"
"github.com/retailcrm/mg-transport-api-client-go/v1"
)
func TestRouting_connectHandler(t *testing.T) {
req, err := http.NewRequest("GET", "/", nil)
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(connectHandler)
handler.ServeHTTP(rr, req)
if 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) {
defer gock.Off()
gock.New("https://api.telegram.org").
Post("/botbot123:test/getMe").
Reply(200).
BodyString(`{"ok":true,"result":{"id":123,"is_bot":true,"first_name":"Test","username":"TestBot"}}`)
ch := v1.Channel{
Type: "telegram",
Events: []string{
"message_sent",
"message_updated",
"message_deleted",
"message_read",
},
}
str, _ := json.Marshal(ch)
gock.New("https://mg-test.com").
Post("/api/v1/transport/channels").
JSON(str).
MatchHeader("Content-Type", "application/json").
MatchHeader("X-Transport-Token", "test-token").
Reply(200).
BodyString(`{"id": 1}`)
req, err := http.NewRequest("POST", "/add-bot/", strings.NewReader(`{"token": "bot123:test", "clientId": "test"}`))
if err != nil {
t.Fatal(err)
}
rr := httptest.NewRecorder()
handler := http.HandlerFunc(addBotHandler)
handler.ServeHTTP(rr, req)
if rr.Code != http.StatusCreated {
t.Errorf("handler returned wrong status code: got %v want %v",
rr.Code, http.StatusCreated)
}
}

1
run.go
View File

@ -49,5 +49,6 @@ func (x *RunCommand) Execute(args []string) error {
func start() {
setWrapperRoutes()
setTransportRoutes()
http.Handle("/web/", http.StripPrefix("/web/", http.FileServer(http.Dir("web"))))
http.ListenAndServe(config.HTTPServer.Listen, nil)
}

View File

@ -9,7 +9,6 @@ import (
func setTransportRoutes() {
http.HandleFunc("/add-bot/", addBotHandler)
http.HandleFunc("/activity-bot/", activityBotHandler)
http.HandleFunc("/map-bot/", mappingHandler)
}
// GetBotInfo function

View File

@ -1,28 +1,17 @@
<html>
<head>
<meta title="Telegram transport">
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-alpha.4/css/materialize.min.css">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-alpha.4/js/materialize.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
<div class="container">
<div class="row" style="margin-top: 5%">
<div class="col s8 offset-s3">
{{template "header"}}
<div class="row indent-top">
<div class="col s6 offset-s3">
<ul class="tabs" id="tab">
<li class="tab col s3"><a class="active" href="#tab1">Settings CRM</a></li>
<li class="tab col s3"><a href="#tab2">Bots</a></li>
<li class="tab col s3"><a href="#tab3">Mapping</a></li>
<li class="tab col s6"><a class="active" href="#tab1">{{.Locale.TabSettings}}</a></li>
<li class="tab col s6"><a class="" href="#tab2">{{.Locale.TabBots}}</a></li>
</ul>
</div>
<div class="col s8 offset-s3">
<p id="msg"></p>
<div class="col s6 offset-s3">
<div id="msg" class="indent-top"></div>
</div>
<div id="tab1" class="col s12">
<div class="row" style="margin-top: 5%">
<form class="col s8 offset-s2" action="/save/" method="POST">
<div class="row indent-top">
<form id="save" class="col s8 offset-s2" action="/save/" method="POST">
<input name="clientId" type="hidden" value="{{.Conn.ClientID}}">
<div class="row">
<div class="input-field col s12">
@ -31,13 +20,13 @@
</div>
<div class="row">
<div class="input-field col s12">
<input placeholder="API Key" id="api_key" name="api_key" type="text" class="validate" value="{{.Conn.APIKEY}}">
<input placeholder="{{.Locale.ApiKey}}" id="api_key" name="api_key" type="text" class="validate" value="{{.Conn.APIKEY}}">
</div>
</div>
<div class="row">
<div class="input-field col s6 offset-s5">
<button class="btn waves-effect waves-light red lighten-1" type="submit" name="action">
Connect
<div class="input-field col s12 center-align">
<button class="btn waves-effect waves-light light-blue darken-1" type="submit" name="action">
{{.Locale.ButConnect}}
<i class="material-icons right">sync</i>
</button>
</div>
@ -46,25 +35,28 @@
</div>
</div>
<div id="tab2" class="col s12">
<div class="row" style="margin-top: 5%">
<form class="col s8 offset-s2" action="/add-bot/" method="POST">
<div class="row indent-top">
<form id="add-bot" class="col s8 offset-s2" action="/add-bot/" method="POST">
<input name="clientId" type="hidden" value="{{.Conn.ClientID}}">
<div class="row">
<div class="input-field col s11">
<input placeholder="Bot Token" id="token" name="token" type="text" class="validate">
<div class="input-field col s12">
<input placeholder="{{.Locale.TableToken}}" id="token" name="token" type="text" class="validate">
</div>
<div class="input-field col s1">
<button class="btn btn-meddium waves-effect waves-light red" type="submit" name="action">
<i class="material-icons">add</i>
</div>
<div class="row">
<div class="input-field col s12 center-align">
<button class="btn waves-effect waves-light light-blue darken-1" type="submit" name="action">
{{.Locale.AddBot}} <i class="material-icons right">add</i>
</button>
</div>
</div>
</form>
{{if .Bots}}
<table class="col s8 offset-s2">
<table id="bots" class="col s8 offset-s2">
<thead>
<tr>
<th>Name</th><th>Token</th><th style="text-align: right">Activity</th>
<th>{{.Locale.TableName}}</th>
<th>{{.Locale.TableToken}}</th>
<th class="text-left">{{.Locale.TableActivity}}</th>
</tr>
</thead>
<tbody>
@ -73,127 +65,16 @@
<td>{{.Name}}</td>
<td>{{.Token}}</td>
<td>
<button class="activity-bot btn btn-meddium waves-effect waves-light red" type="submit" name="action" style="float: right"
data-activity="{{.Active}}" data-id="{{.Token}}">
<i class="material-icons">{{if .Active}}stop{{else}}play_arrow{{end}}</i>
<button class="activity-bot btn btn-small waves-effect waves-light light-blue darken-1" type="submit" name="action"
data-activity="{{.Active}}" data-token="{{.Token}}">
<i class="material-icons">{{if .Active}}pause{{else}}play_arrow{{end}}</i>
</button>
</td>
</tr>
{{end}}
</tbody>
</table>
{{end}}
</div>
</div>
<div id="tab3" class="col s12">
<div class="row" style="margin-top: 5%">
{{ $sites := .Sites }}
{{ $bots := .Bots }}
{{if and $sites $bots}}
<table class="col s8 offset-s2">
<thead>
<tr>
<th>Sites</th>
<th>Bots</th>
</tr>
</thead>
<tbody>
{{range $site := $sites}}
<tr>
<td>{{$site.Name}}</td>
<td>
<select class="browser-default">
<option value="" disabled selected>Choose your option</option>
{{range $bot := $bots}}
<option data-code="{{$site.Code}}" data-bot="{{$bot.ID}}">{{$bot.Name}}</option>
{{end}}
</select>
</td>
</tr>
{{end}}
</tbody>
</table>
</div>
<div class="row">
<div class="input-field col s6 offset-s5">
<button class="btn btn-meddium waves-effect waves-light red" type="submit" id="map-bot">
save
<i class="material-icons">save</i>
</button>
</div>
</div>
{{end}}
</div>
</div>
</div>
<script>
$("form").each(function() {
$(this).on("submit", function(e) {
e.preventDefault();
send($(this).attr('action'), formDataToObj($(this).serializeArray()))
});
});
$('.activity-bot').on("click", function(e) {
send("/activity-bot/", {
token: $(this).attr("data-id"),
active: ($(this).attr("data-activity") == 'true'),
clientId: $('input[name=clientId]').val(),
})
});
$('#map-bot').on("click", function(e) {
let mapping = [];
$('select option:selected').each(function() {
let site_code = $(this).attr("data-code");
let bot_id = $(this).attr("data-bot");
if (site_code && bot_id) {
mapping.push({site_code: site_code, bot_id: bot_id});
}
});
if (!$.isEmptyObject(mapping)) {
send("/map-bot/", mapping);
}
});
function send(url, data) {
$.ajax({
url: url,
data: JSON.stringify(data),
type: "POST",
success: function(res){
location.reload();
},
error: function (res){
if (res.status < 400) {
if (res.responseText) {
document.location.replace(
location.protocol.concat("//").concat(window.location.host) + res.responseText
);
}
} else {
$('#msg').append(res.responseText);
}
}
});
}
function formDataToObj(formArray) {
let obj = {};
for (let i = 0; i < formArray.length; i++){
obj[formArray[i]['name']] = formArray[i]['value'];
}
return obj;
}
document.addEventListener('DOMContentLoaded', function() {
M.Tabs.init(document.getElementById("tab"));
});
</script>
</div>
</body>
</html>
{{template "footer"}}

View File

@ -1,17 +1,10 @@
<html>
<head>
<meta title="Telegram transport">
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-alpha.4/css/materialize.min.css">
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/1.0.0-alpha.4/js/materialize.min.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
<div class="container">
<div class="row" style="margin-top: 5%">
{{template "header"}}
<div class="row indent-top">
<div class="col s6 offset-s3">
<div id="msg" class="indent-top"></div>
</div>
<form class="col s8 offset-s2" method="POST" id="save-crm" action="/create/">
<p id="msg"></p>
<div id="msg"></div>
<div class="row">
<div class="input-field col s12">
<input placeholder="API Url" id="api_url" name="api_url" type="text" class="validate">
@ -19,47 +12,17 @@
</div>
<div class="row">
<div class="input-field col s12">
<input placeholder="API Key" id="api_key" name="api_key" type="text" class="validate">
<input placeholder="{{.Locale.ApiKey}}" id="api_key" name="api_key" type="text" class="validate">
</div>
</div>
<div class="row">
<div class="input-field col s6 offset-s5">
<button class="btn waves-effect waves-light red lighten-1" type="submit" name="action">Connect
<div class="input-field col s12 center-align">
<button class="btn waves-effect waves-light light-blue darken-1" type="submit" name="action">
{{.Locale.ButConnect}}
<i class="material-icons right">sync</i>
</button>
</div>
</div>
</form>
</div>
</div>
</body>
<script>
$('#save-crm').on("submit", function(e) {
e.preventDefault();
send($(this).attr('action'), $(this).serialize())
});
function send(url, data) {
$.ajax({
url: url,
data: data,
success: function(res){
console.log(res);
},
error: function (res){
if (res.status < 400) {
if (res.responseText) {
document.location.replace(
location.protocol.concat("//").concat(window.location.host) + res.responseText
);
}
} else {
$('#msg').append(res.responseText);
}
}
});
}
</script>
</html>
{{template "footer"}}

25
templates/layout.html Normal file
View File

@ -0,0 +1,25 @@
{{ define "header" }}
<html>
<head>
<meta title="Telegram transport">
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link rel="stylesheet" href="../web/materialize.min.css">
<link rel="stylesheet" href="../web/font.css" >
<link rel="stylesheet" href="../web/style.css" >
</head>
<body>
<div class="container">
<div class="indent-top center-align">
<img id="logo" src="data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIj8+CjxzdmcgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIiB4bWxuczp4bGluaz0iaHR0cDovL3d3dy53My5vcmcvMTk5OS94bGluayIgdmlld0JveD0iMCAwIDI0MCAyNDAiPgo8ZGVmcz4KCTxsaW5lYXJHcmFkaWVudCBpZD0iYiIgeDE9IjAuNjY2NyIgeTE9IjAuMTY2NyIgeDI9IjAuNDE2NyIgeTI9IjAuNzUiPgoJCTxzdG9wIHN0b3AtY29sb3I9IiMzN2FlZTIiIG9mZnNldD0iMCIvPgoJCTxzdG9wIHN0b3AtY29sb3I9IiMxZTk2YzgiIG9mZnNldD0iMSIvPgoJPC9saW5lYXJHcmFkaWVudD4KCTxsaW5lYXJHcmFkaWVudCBpZD0idyIgeDE9IjAuNjU5NyIgeTE9IjAuNDM2OSIgeDI9IjAuODUxMiIgeTI9IjAuODAyNCI+CgkJPHN0b3Agc3RvcC1jb2xvcj0iI2VmZjdmYyIgb2Zmc2V0PSIwIi8+CgkJPHN0b3Agc3RvcC1jb2xvcj0iI2ZmZiIgb2Zmc2V0PSIxIi8+Cgk8L2xpbmVhckdyYWRpZW50Pgo8L2RlZnM+CjxjaXJjbGUgY3g9IjEyMCIgY3k9IjEyMCIgcj0iMTIwIiBmaWxsPSJ1cmwoI2IpIi8+CjxwYXRoIGZpbGw9IiNjOGRhZWEiIGQ9Im05OCAxNzVjLTMuODg3NiAwLTMuMjI3LTEuNDY3OS00LjU2NzgtNS4xNjk1TDgyIDEzMi4yMDU5IDE3MCA4MCIvPgo8cGF0aCBmaWxsPSIjYTljOWRkIiBkPSJtOTggMTc1YzMgMCA0LjMyNTUtMS4zNzIgNi0zbDE2LTE1LjU1OC0xOS45NTgtMTIuMDM1Ii8+CjxwYXRoIGZpbGw9InVybCgjdykiIGQ9Im0xMDAuMDQgMTQ0LjQxIDQ4LjM2IDM1LjcyOWM1LjUxODUgMy4wNDQ5IDkuNTAxNCAxLjQ2ODQgMTAuODc2LTUuMTIzNWwxOS42ODUtOTIuNzYzYzIuMDE1NC04LjA4MDItMy4wODAxLTExLjc0NS04LjM1OTQtOS4zNDgybC0xMTUuNTkgNDQuNTcxYy03Ljg5MDEgMy4xNjQ3LTcuODQ0MSA3LjU2NjYtMS40MzgyIDkuNTI4bDI5LjY2MyA5LjI1ODMgNjguNjczLTQzLjMyNWMzLjI0MTktMS45NjU5IDYuMjE3My0wLjkwODk5IDMuNzc1MiAxLjI1ODQiLz4KPC9zdmc+" alt="telegram" >
</div>
{{ end }}
{{ define "footer" }}
</div>
<script src="../web/materialize.min.js"></script>
<script src="../web/jquery-3.3.1.min.js"></script>
<script src="../web/script.js"></script>
</body>
</html>
{{ end }}

View File

@ -0,0 +1,23 @@
but_connect: Save
tab_settings: Connection settings
tab_bots: Bots
table_name: Bot name
table_token: Bot token
table_activity: Activity
api_key: API Key
no_bot_token: Enter the bot token
wrong_data: Incorrect data
set_method: Set POST method
bot_already_created: Bot already created
not_find_account: Could not find account, please contact technical support
error_activating_channel: Error while activating the channel
error_deactivating_channel: Error while deactivating the channel
incorrect_url_key: Enter the correct CRM url or apiKey
error_creating_integration: Error while creating integration
error_creating_connection: Error while creating connection
connection_already_created: Connection already created
missing_url_key: Missing crm url or apiKey
incorrect_url: Enter the correct CRM url
incorrect_token: Set correct bot token
add_bot: Add bot

View File

@ -0,0 +1,23 @@
but_connect: Сохранить
tab_settings: Настройки CRM
tab_bots: Боты
table_name: Имя
table_token: Токен
table_activity: Активность
api_key: API Ключ
no_bot_token: Введите токен
wrong_data: Неверные данные
set_method: Установить метод POST
bot_already_created: Бот уже создан
not_find_account: Не удалось найти учетную запись, обратитесь в службу технической поддержки
error_activating_channel: Ошибка при активации канала
error_deactivating_channel: Ошибка при отключении канала
incorrect_url_key: Введите корректный URL или apiKey
error_creating_integration: Ошибка при создании интеграции
error_creating_connection: Ошибка при создании соединения
connection_already_created: Соединение уже создано
missing_url_key: Отсутствует URL или apiKey
incorrect_url: Введите корректный URL CRM
incorrect_token: Установите корректный токен
add_bot: Добавить бота

23
web/font.css Normal file
View File

@ -0,0 +1,23 @@
/* fallback */
@font-face {
font-family: 'Material Icons';
font-style: normal;
font-weight: 400;
src: url(font.woff2) format('woff2');
}
.material-icons {
font-family: 'Material Icons';
font-weight: normal;
font-style: normal;
font-size: 24px;
line-height: 1;
letter-spacing: normal;
text-transform: none;
display: inline-block;
white-space: nowrap;
word-wrap: normal;
direction: ltr;
-webkit-font-feature-settings: 'liga';
-webkit-font-smoothing: antialiased;
}

BIN
web/font.woff2 Normal file

Binary file not shown.

2
web/jquery-3.3.1.min.js vendored Normal file

File diff suppressed because one or more lines are too long

13
web/materialize.min.css vendored Normal file

File diff suppressed because one or more lines are too long

6
web/materialize.min.js vendored Normal file

File diff suppressed because one or more lines are too long

109
web/script.js Normal file
View File

@ -0,0 +1,109 @@
$('#save-crm').on("submit", function(e) {
e.preventDefault();
send(
$(this).attr('action'),
formDataToObj($(this).serializeArray()),
function () {
return 0;
}
)
});
$("#save").on("submit", function(e) {
e.preventDefault();
send(
$(this).attr('action'),
formDataToObj($(this).serializeArray()),
function () {
return 0;
}
)
});
$("#add-bot").on("submit", function(e) {
e.preventDefault();
send(
$(this).attr('action'),
formDataToObj($(this).serializeArray()),
function (data) {
let bots = $("#bots");
if (bots.hasClass("hide")) {
bots.removeClass("hide")
}
$("#bots tbody").append(getBotTemplate(data));
}
)
});
$(document).on("click", ".activity-bot", function(e) {
let but = $(this);
send("/activity-bot/",
{
token: but.attr("data-token"),
active: (but.attr("data-activity") === 'true'),
clientId: $('input[name=clientId]').val(),
},
function () {
if (but.attr("data-activity") === 'true') {
but.find('i').replaceWith('<i class="material-icons">play_arrow</i>');
but.attr("data-activity", "false")
} else {
but.find('i').replaceWith('<i class="material-icons">pause</i>');
but.attr("data-activity", "true")
}
}
)
});
function send(url, data, callback) {
$('#msg').empty();
$.ajax({
url: url,
data: JSON.stringify(data),
type: "POST",
success: callback,
error: function (res){
if (res.status < 400) {
if (res.responseText) {
document.location.replace(
location.protocol.concat("//").concat(window.location.host) + res.responseText
);
}
} else {
//$('#msg').html(`<p class="err-msg truncate">${res.responseText}</p>`);
M.toast({html: res.responseText})
}
}
});
}
function getBotTemplate(data) {
let bot = JSON.parse(data);
tmpl =
`<tr>
<td>${bot.name}</td>
<td>${bot.token}</td>
<td>
<button class="activity-bot btn btn-small waves-effect waves-light light-blue darken-1" type="submit" name="action"
data-activity="true" data-token="${bot.token}">
<i class="material-icons">pause</i>
</button>
</td>
</tr>`;
return tmpl;
}
function formDataToObj(formArray) {
let obj = {};
for (let i = 0; i < formArray.length; i++){
obj[formArray[i]['name']] = formArray[i]['value'];
}
return obj;
}
$( document ).ready(function() {
M.Tabs.init(document.getElementById("tab"));
if ($("table tbody").children().length === 0) {
$("#bots").addClass("hide");
}
});

94
web/style.css Normal file
View File

@ -0,0 +1,94 @@
.indent-top {
margin-top: 2%;
}
.text-left{
text-align: right;
}
#bots .activity-bot{
float: right;
}
#bots {
font-size: 12px;
}
#msg{
height: 23px;
}
.err-msg{
color: red;
margin: 0;
}
#logo{
height: 100px;
margin-bottom: 20px;
}
/* label color */
.input-field label {
color: #039be5;
}
/* label focus color */
.input-field input[type=text]:focus + label {
color: #039be5;
}
/* label underline focus color */
.input-field input[type=text]:focus {
border-bottom: 1px solid #039be5;
box-shadow: 0 1px 0 0 #039be5;
}
/* valid color */
.input-field input[type=text].valid {
border-bottom: 1px solid #039be5;
box-shadow: 0 1px 0 0 #039be5;
}
/* invalid color */
.input-field input[type=text].invalid {
border-bottom: 1px solid #c62828;
box-shadow: 0 1px 0 0 #c62828;
}
/* icon prefix focus color */
.input-field .prefix.active {
color: #039be5;
}
.tabs .tab a {
color: #039be5;
display: block;
width: 100%;
height: 100%;
padding: 0 24px;
font-size: 14px;
text-overflow: ellipsis;
overflow: hidden;
-webkit-transition: color .28s ease, background-color .28s ease;
transition: color .28s ease, background-color .28s ease;
}
.tabs .tab a:focus, .tabs .tab a:focus.active {
background-color: #e1f5fe;
outline: none;
}
.tabs .tab a:hover, .tabs .tab a.active {
background-color: transparent;
color: #039be5;
}
.tabs .tab.disabled a,
.tabs .tab.disabled a:hover {
color: #039be5;
cursor: default;
}
.tabs .indicator {
position: absolute;
bottom: 0;
height: 2px;
background-color: #039be5;
will-change: left, right;
}

17
web/telegram_logo.svg Normal file
View File

@ -0,0 +1,17 @@
<?xml version="1.0"?>
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 240 240">
<defs>
<linearGradient id="b" x1="0.6667" y1="0.1667" x2="0.4167" y2="0.75">
<stop stop-color="#37aee2" offset="0"/>
<stop stop-color="#1e96c8" offset="1"/>
</linearGradient>
<linearGradient id="w" x1="0.6597" y1="0.4369" x2="0.8512" y2="0.8024">
<stop stop-color="#eff7fc" offset="0"/>
<stop stop-color="#fff" offset="1"/>
</linearGradient>
</defs>
<circle cx="120" cy="120" r="120" fill="url(#b)"/>
<path fill="#c8daea" d="m98 175c-3.8876 0-3.227-1.4679-4.5678-5.1695L82 132.2059 170 80"/>
<path fill="#a9c9dd" d="m98 175c3 0 4.3255-1.372 6-3l16-15.558-19.958-12.035"/>
<path fill="url(#w)" d="m100.04 144.41 48.36 35.729c5.5185 3.0449 9.5014 1.4684 10.876-5.1235l19.685-92.763c2.0154-8.0802-3.0801-11.745-8.3594-9.3482l-115.59 44.571c-7.8901 3.1647-7.8441 7.5666-1.4382 9.528l29.663 9.2583 68.673-43.325c3.2419-1.9659 6.2173-0.90899 3.7752 1.2584"/>
</svg>

After

Width:  |  Height:  |  Size: 999 B