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

add order message

This commit is contained in:
DmitryZagorulko 2018-09-21 12:25:31 +03:00
parent b4bc035f81
commit 9d6ebb540f
6 changed files with 147 additions and 31 deletions

View File

@ -41,6 +41,13 @@ func getLocalizedMessage(messageID string) string {
return localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: messageID}) return localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: messageID})
} }
func getLocalizedTemplateMessage(messageID string, templateData map[string]interface{}) string {
return localizer.MustLocalize(&i18n.LocalizeConfig{
MessageID: messageID,
TemplateData: templateData,
})
}
func getLocale() map[string]interface{} { func getLocale() map[string]interface{} {
return map[string]interface{}{ return map[string]interface{}{
"ButtonSave": getLocalizedMessage("button_save"), "ButtonSave": getLocalizedMessage("button_save"),

View File

@ -562,14 +562,21 @@ func mgWebhookHandler(c *gin.Context) {
switch msg.Type { switch msg.Type {
case "message_sent": case "message_sent":
var mb string var mb string
if msg.Data.Type == v1.MsgTypeProduct { switch msg.Data.Type {
case v1.MsgTypeProduct:
mb = fmt.Sprintf("%s\n", msg.Data.Product.Name) mb = fmt.Sprintf("%s\n", msg.Data.Product.Name)
if msg.Data.Product.Cost != nil && msg.Data.Product.Cost.Value != 0 { if msg.Data.Product.Cost != nil && msg.Data.Product.Cost.Value != 0 {
mb += fmt.Sprintf( mb += fmt.Sprintf(
"\n%v %s\n", "\n%s: %s",
msg.Data.Product.Cost.Value, getLocalizedMessage("item_cost"),
currency[strings.ToLower(msg.Data.Product.Cost.Currency)], getLocalizedTemplateMessage(
"cost_currency",
map[string]interface{}{
"CostValue": msg.Data.Product.Cost.Value,
"CostCurrency": currency[strings.ToLower(msg.Data.Product.Cost.Currency)],
},
),
) )
} }
@ -578,26 +585,9 @@ func mgWebhookHandler(c *gin.Context) {
} else { } else {
mb += msg.Data.Product.Img mb += msg.Data.Product.Img
} }
} else if msg.Data.Type == v1.MsgTypeOrder { case v1.MsgTypeOrder:
mb = "Заказ" mb = getOrderMessage(msg.Data.Order)
case v1.MsgTypeText:
if msg.Data.Order.Number != "" {
mb += " " + msg.Data.Order.Number
}
if msg.Data.Order.Date != "" {
mb += fmt.Sprintf(" (%s)", msg.Data.Order.Date)
}
mb += "\n"
if len(msg.Data.Order.Items) > 0 {
for _, v := range msg.Data.Order.Items {
mb += fmt.Sprintf("%s %v x %v %s\n", v.Name, v.Quantity.Value, v.Price.Value, currency[strings.ToLower(v.Price.Currency)])
}
}
mb += fmt.Sprintf("Сумма: %v %s", msg.Data.Order.Cost.Value, currency[strings.ToLower(msg.Data.Order.Cost.Currency)])
} else {
mb = msg.Data.Content mb = msg.Data.Content
} }
@ -654,3 +644,100 @@ func mgWebhookHandler(c *gin.Context) {
} }
} }
func getOrderMessage(dataOrder *v1.MessageDataOrder) string {
mb := getLocalizedMessage("order")
if dataOrder.Number != "" {
mb += " " + dataOrder.Number
}
if dataOrder.Date != "" {
mb += fmt.Sprintf(" (%s)", dataOrder.Date)
}
mb += "\n"
if len(dataOrder.Items) > 0 {
mb += "\n"
for k, v := range dataOrder.Items {
mb += fmt.Sprintf(
"%d. %s %v x %s\n",
k+1,
v.Name,
v.Quantity.Value,
getLocalizedTemplateMessage(
"cost_currency",
map[string]interface{}{
"Amount": v.Price.Value,
"Currency": currency[strings.ToLower(v.Price.Currency)],
},
),
)
}
}
if dataOrder.Delivery != nil {
mb += fmt.Sprintf(
"\n%s:\n%s; %s",
getLocalizedMessage("delivery"),
dataOrder.Delivery.Name,
getLocalizedTemplateMessage(
"cost_currency",
map[string]interface{}{
"Amount": dataOrder.Delivery.Amount.Value,
"Currency": currency[strings.ToLower(dataOrder.Delivery.Amount.Currency)],
},
),
)
if dataOrder.Delivery.Address != "" {
mb += ";\n" + dataOrder.Delivery.Address
}
mb += "\n"
}
if len(dataOrder.Payments) > 0 {
mb += fmt.Sprintf(
"\n%s:\n",
getLocalizedMessage("payment"),
)
for _, v := range dataOrder.Payments {
mb += fmt.Sprintf(
"%s; %s",
v.Name,
getLocalizedTemplateMessage(
"cost_currency",
map[string]interface{}{
"Amount": v.Amount.Value,
"Currency": currency[strings.ToLower(v.Amount.Currency)],
},
),
)
if v.Status != nil && v.Status.Name != "" {
mb += fmt.Sprintf(
" (%s)",
v.Status.Name,
)
}
mb += "\n"
}
}
if dataOrder.Cost != nil && dataOrder.Cost.Value != 0 {
mb += fmt.Sprintf(
"\n%s: %s",
getLocalizedMessage("cost"),
getLocalizedTemplateMessage(
"cost_currency",
map[string]interface{}{
"Amount": dataOrder.Cost.Value,
"Currency": currency[strings.ToLower(dataOrder.Cost.Currency)],
},
),
)
}
return mb
}

View File

@ -15,7 +15,6 @@ import (
"github.com/aws/aws-sdk-go/aws/credentials" "github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3/s3manager" "github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/nicksnyder/go-i18n/v2/i18n"
"github.com/retailcrm/api-client-go/v5" "github.com/retailcrm/api-client-go/v5"
) )
@ -52,12 +51,14 @@ func getAPIClient(url, key string) (*v5.Client, error, int) {
if res := checkCredentials(cr.Credentials); len(res) != 0 { if res := checkCredentials(cr.Credentials); len(res) != 0 {
logger.Error(url, status, res) logger.Error(url, status, res)
return nil, return nil,
errors.New(localizer.MustLocalize(&i18n.LocalizeConfig{ errors.New(
MessageID: "missing_credentials", getLocalizedTemplateMessage(
TemplateData: map[string]interface{}{ "missing_credentials",
"Credentials": strings.Join(res, ", "), map[string]interface{}{
}, "Credentials": strings.Join(res, ", "),
})), },
),
),
http.StatusBadRequest http.StatusBadRequest
} }

View File

@ -41,3 +41,10 @@ video: "[video]"
voice: "[voice message]" voice: "[voice message]"
photo: "[photo]" photo: "[photo]"
undefined: "[undefined format of a message]" undefined: "[undefined format of a message]"
item_cost: "Cost"
order: "Order"
delivery: "Delivery"
payment: "Payment"
cost: "Amount"
cost_currency: "{{.Amount}} {{.Currency}}"

View File

@ -41,3 +41,10 @@ video: "[video]"
voice: "[mensaje de voz]" voice: "[mensaje de voz]"
photo: "[foto]" photo: "[foto]"
other: "[formato indefinido de mensaje]" other: "[formato indefinido de mensaje]"
item_cost: "Costo"
order: "Orden"
delivery: "Entrega"
payment: "Pago"
cost: "Monto"
cost_currency: "{{.Amount}} {{.Currency}}"

View File

@ -41,3 +41,10 @@ video: "[видео]"
voice: "[голосовое сообщение]" voice: "[голосовое сообщение]"
photo: "[изображение]" photo: "[изображение]"
undefined: "[неопределенный формат сообщения]" undefined: "[неопределенный формат сообщения]"
item_cost: "Цена"
order: "Заказ"
delivery: "Доставка"
payment: "Оплата"
cost: "Сумма"
cost_currency: "{{.Amount}} {{.Currency}}"