1
0
mirror of synced 2024-11-21 20:16:02 +03:00

add bots localization

This commit is contained in:
DmitryZagorulko 2018-09-21 12:48:42 +03:00
parent 9d6ebb540f
commit 16922ef0fc
11 changed files with 83 additions and 9 deletions

View File

@ -59,6 +59,7 @@ func getLocale() map[string]interface{} {
"AddBot": getLocalizedMessage("add_bot"),
"TableDelete": getLocalizedMessage("table_delete"),
"Title": getLocalizedMessage("title"),
"Language": getLocalizedMessage("language"),
"InfoBot": template.HTML(getLocalizedMessage("info_bot")),
"CRMLink": template.HTML(getLocalizedMessage("crm_link")),
"DocLink": template.HTML(getLocalizedMessage("doc_link")),

View File

@ -24,6 +24,7 @@ type Bot struct {
ChannelSettingsHash string `gorm:"channel_settings_hash type:varchar(70)"`
Token string `gorm:"token type:varchar(100);not null;unique" json:"token,omitempty"`
Name string `gorm:"name type:varchar(40)" json:"name,omitempty"`
Lang string `gorm:"lang type:varchar(2)" json:"lang,omitempty"`
CreatedAt time.Time
UpdatedAt time.Time
}

View File

@ -117,15 +117,17 @@ func settingsHandler(c *gin.Context) {
bots := p.getBotsByClientID()
res := struct {
Conn *Connection
Bots Bots
Locale map[string]interface{}
Year int
Conn *Connection
Bots Bots
Locale map[string]interface{}
Year int
LangCode []string
}{
p,
bots,
getLocale(),
time.Now().Year(),
[]string{"en", "ru", "es"},
}
c.HTML(http.StatusOK, "form", &res)
@ -246,6 +248,25 @@ func activityHandler(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"success": true})
}
func setLangBotHandler(c *gin.Context) {
b := c.MustGet("bot").(Bot)
cl, err := getBotByToken(b.Token)
if err != nil {
c.Error(err)
return
}
cl.Lang = b.Lang
err = cl.save()
if err != nil {
c.Error(err)
return
}
c.JSON(http.StatusOK, gin.H{})
}
func getIntegrationModule(clientId string) v5.IntegrationModule {
return v5.IntegrationModule{
Code: config.TransportInfo.Code,
@ -559,6 +580,8 @@ func mgWebhookHandler(c *gin.Context) {
return
}
setLocale(b.Lang)
switch msg.Type {
case "message_sent":
var mb string
@ -568,13 +591,13 @@ func mgWebhookHandler(c *gin.Context) {
if msg.Data.Product.Cost != nil && msg.Data.Product.Cost.Value != 0 {
mb += fmt.Sprintf(
"\n%s: %s",
"\n%s: %s\n",
getLocalizedMessage("item_cost"),
getLocalizedTemplateMessage(
"cost_currency",
map[string]interface{}{
"CostValue": msg.Data.Product.Cost.Value,
"CostCurrency": currency[strings.ToLower(msg.Data.Product.Cost.Currency)],
"Amount": msg.Data.Product.Cost.Value,
"Currency": currency[strings.ToLower(msg.Data.Product.Cost.Currency)],
},
),
)

View File

@ -90,6 +90,7 @@ func setup() *gin.Engine {
r.POST("/create/", checkConnectionForRequest(), createHandler)
r.POST("/add-bot/", checkBotForRequest(), addBotHandler)
r.POST("/delete-bot/", checkBotForRequest(), deleteBotHandler)
r.POST("/set-lang/", checkBotForRequest(), setLangBotHandler)
r.POST("/actions/activity", activityHandler)
r.POST("/telegram/:token", telegramWebhookHandler)
r.POST("/webhook/", mgWebhookHandler)

View File

@ -1,3 +1,16 @@
$(document).on("change", "select", function(e) {
send(
"/set-lang/",
{
token: $(this).attr("data-token"),
lang: $(this).find(":selected").text()
},
function () {
return 0;
}
)
});
$('#save-crm').on("submit", function(e) {
e.preventDefault();
send(
@ -39,6 +52,7 @@ $("#add-bot").on("submit", function(e) {
}
$("#bots tbody").append(getBotTemplate(data));
$("#token").val("");
$('select').formSelect();
}
)
});
@ -101,6 +115,15 @@ function getBotTemplate(data) {
`<tr>
<td>${data.name}</td>
<td>${data.token}</td>
<td>
<div class="col s3 sel-lang">
<select data-token="${data.token}">
<option value="en" selected>en</option>
<option value="ru">ru</option>
<option value="es">es</option>
</select>
</div>
</td>
<td>
<button class="delete-bot btn btn-small waves-effect waves-light light-blue darken-1" type="submit" name="action"
data-token="${data.token}">
@ -120,6 +143,7 @@ function formDataToObj(formArray) {
}
$( document ).ready(function() {
$('select').formSelect();
M.Tabs.init(document.getElementById("tab"));
if ($("table tbody").children().length === 0) {
$("#bots").addClass("hide");

View File

@ -32,10 +32,19 @@ main {
float: right;
}
#bots .select-wrapper input.select-dropdown,
#bots {
font-size: 12px;
}
#bots .sel-lang{
padding: 0;
}
#bots .select-wrapper ul li span{
color: #039be5;
}
#msg{
height: 23px;
}

View File

@ -51,19 +51,31 @@
</div>
</div>
</form>
{{$LangCode := .LangCode}}
<table id="bots" class="tab-el-center">
<thead>
<tr>
<th>{{.Locale.TableName}}</th>
<th>{{.Locale.TableToken}}</th>
<th>{{.Locale.Language}}</th>
<th class="text-left">{{.Locale.TableDelete}}</th>
</tr>
</thead>
<tbody>
{{range .Bots}}
{{$lang := .Lang}}
<tr>
<td>{{.Name}}</td>
<td>{{.Token}}</td>
<td>
<div class="col s3 sel-lang">
<select data-token="{{.Token}}">
{{range $key, $value := $LangCode}}
<option value="{{$value}}" {{if eq $value $lang}}selected{{end}}>{{$value}}</option>
{{end}}
</select>
</div>
</td>
<td>
<button class="delete-bot btn btn-small waves-effect waves-light light-blue darken-1" type="submit" name="action"
data-token="{{.Token}}">

View File

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

View File

@ -8,6 +8,7 @@ api_key: API key
add_bot: Add a bot
title: Module of connecting Telegram to retailCRM
successful: Data was updated successfully
language: Language
no_bot_token: Enter a token
wrong_data: Wrong data

View File

@ -8,6 +8,7 @@ api_key: API key
add_bot: Añadir un bot
title: Múdulo de conexión de Telegram a retailCRM
successful: Datos actualizados con éxito
language: Idioma
no_bot_token: Introduzca un token
wrong_data: Datos erróneos

View File

@ -8,6 +8,7 @@ api_key: API Ключ
add_bot: Добавить бота
title: Модуль подключения Telegram к retailCRM
successful: Данные успешно обновлены
language: Язык
no_bot_token: Введите токен
wrong_data: Неверные данные