From de6ee9462a15a762bc00f1a368f9c85fd59331ed Mon Sep 17 00:00:00 2001 From: DmitryZagorulko Date: Wed, 20 Jun 2018 16:14:51 +0300 Subject: [PATCH] sending text stub --- migrations/1528538599_app.down.sql | 2 +- repository.go | 4 +- telegram.go | 210 ++++++++++++++++------------- translate/translate.en.yml | 12 +- translate/translate.ru.yml | 10 ++ 5 files changed, 144 insertions(+), 94 deletions(-) diff --git a/migrations/1528538599_app.down.sql b/migrations/1528538599_app.down.sql index c8e61f9..28e2e47 100644 --- a/migrations/1528538599_app.down.sql +++ b/migrations/1528538599_app.down.sql @@ -1 +1 @@ -alter table mg_user rename to user; \ No newline at end of file +alter table mg_user rename to users; diff --git a/repository.go b/repository.go index 99727e6..8cb1dae 100644 --- a/repository.go +++ b/repository.go @@ -69,9 +69,9 @@ func (c Connection) getBotsByClientID() Bots { return b } -func getBotByChannelAndCID(connectionID int, ch uint64) *Bot { +func getBot(cid int, ch uint64) *Bot { var bot Bot - orm.DB.First(&bot, "connection_id = ? AND channel = ?", connectionID, ch) + orm.DB.First(&bot, "connection_id = ? AND channel = ?", cid, ch) return &bot } diff --git a/telegram.go b/telegram.go index 95f9237..dfec7ea 100644 --- a/telegram.go +++ b/telegram.go @@ -15,6 +15,7 @@ import ( "github.com/aws/aws-sdk-go/service/s3/s3manager" "github.com/getsentry/raven-go" "github.com/go-telegram-bot-api/telegram-bot-api" + "github.com/nicksnyder/go-i18n/v2/i18n" "github.com/retailcrm/mg-transport-api-client-go/v1" ) @@ -76,112 +77,118 @@ func telegramWebhookHandler(w http.ResponseWriter, r *http.Request, token string var client = v1.New(c.MGURL, c.MGToken) if update.Message != nil { - if update.Message.Text != "" { - nickname := update.Message.From.UserName - user := getUserByExternalID(update.Message.From.ID) + if update.Message.Text == "" { + setLocale(update.Message.From.LanguageCode) + update.Message.Text = localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: getMessageID(update.Message)}) + } - if update.Message.From.UserName == "" { - nickname = update.Message.From.FirstName - } + nickname := update.Message.From.UserName + user := getUserByExternalID(update.Message.From.ID) - if user.Expired(config.UpdateInterval) || user.ID == 0 { - fileID, fileURL, err := GetFileIDAndURL(b.Token, update.Message.From.ID) - if err != nil { - raven.CaptureErrorAndWait(err, nil) - logger.Error(err) - w.WriteHeader(http.StatusInternalServerError) - return - } + if update.Message.From.UserName == "" { + nickname = update.Message.From.FirstName + } - if fileID != user.UserPhotoID && fileURL != "" { - picURL, err := UploadUserAvatar(fileURL) - if err != nil { - raven.CaptureErrorAndWait(err, nil) - logger.Error(err) - w.WriteHeader(http.StatusInternalServerError) - return - } - - user.UserPhotoID = fileID - user.UserPhotoURL = picURL - } - - if user.ExternalID == 0 { - user.ExternalID = update.Message.From.ID - } - - err = user.save() - if err != nil { - raven.CaptureErrorAndWait(err, nil) - logger.Error(err) - w.WriteHeader(http.StatusInternalServerError) - return - } - } - - if config.Debug { - logger.Debugf("telegramWebhookHandler user %v", user) - } - - 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: nickname, - Firstname: update.Message.From.FirstName, - Avatar: user.UserPhotoURL, - Lastname: update.Message.From.LastName, - Language: update.Message.From.LanguageCode, - }, - Channel: b.Channel, - ExternalChatID: strconv.FormatInt(update.Message.Chat.ID, 10), - } - - data, st, err := client.Messages(snd) + if user.Expired(config.UpdateInterval) || user.ID == 0 { + fileID, fileURL, err := GetFileIDAndURL(b.Token, update.Message.From.ID) if err != nil { raven.CaptureErrorAndWait(err, nil) - logger.Error(token, err.Error(), st, data) + logger.Error(err) w.WriteHeader(http.StatusInternalServerError) return } - if config.Debug { - logger.Debugf("telegramWebhookHandler Type: SendMessage, Bot: %v, Message: %v, Response: %v", b.ID, snd, data) + if fileID != user.UserPhotoID && fileURL != "" { + picURL, err := UploadUserAvatar(fileURL) + if err != nil { + raven.CaptureErrorAndWait(err, nil) + logger.Error(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + + user.UserPhotoID = fileID + user.UserPhotoURL = picURL } + + if user.ExternalID == 0 { + user.ExternalID = update.Message.From.ID + } + + err = user.save() + if err != nil { + raven.CaptureErrorAndWait(err, nil) + logger.Error(err) + w.WriteHeader(http.StatusInternalServerError) + return + } + } + + if config.Debug { + logger.Debugf("telegramWebhookHandler user %v", user) + } + + 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: nickname, + Firstname: update.Message.From.FirstName, + Avatar: user.UserPhotoURL, + Lastname: update.Message.From.LastName, + Language: update.Message.From.LanguageCode, + }, + Channel: b.Channel, + ExternalChatID: strconv.FormatInt(update.Message.Chat.ID, 10), + } + + 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 config.Debug { + logger.Debugf("telegramWebhookHandler Type: SendMessage, Bot: %v, Message: %v, Response: %v", b.ID, snd, data) } } if update.EditedMessage != nil { - if update.EditedMessage.Text != "" { - snd := v1.UpdateData{ - Message: v1.UpdateMessage{ - Message: v1.Message{ - ExternalID: strconv.Itoa(update.EditedMessage.MessageID), - Type: "text", - Text: update.EditedMessage.Text, - }, + if update.EditedMessage.Text == "" { + setLocale(update.EditedMessage.From.LanguageCode) + update.EditedMessage.Text = localizer.MustLocalize(&i18n.LocalizeConfig{MessageID: getMessageID(update.Message)}) + } + + snd := v1.UpdateData{ + Message: v1.UpdateMessage{ + Message: v1.Message{ + ExternalID: strconv.Itoa(update.EditedMessage.MessageID), + Type: "text", + Text: update.EditedMessage.Text, }, - Channel: b.Channel, - } + }, + Channel: b.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 - } + 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 + } - if config.Debug { - logger.Debugf("telegramWebhookHandler Type: UpdateMessage, Bot: %v, Message: %v, Response: %v", b.ID, snd, data) - } + if config.Debug { + logger.Debugf("telegramWebhookHandler Type: UpdateMessage, Bot: %v, Message: %v, Response: %v", b.ID, snd, data) } } @@ -229,7 +236,7 @@ func mgWebhookHandler(w http.ResponseWriter, r *http.Request) { uid, _ := strconv.Atoi(msg.Data.ExternalMessageID) cid, _ := strconv.ParseInt(msg.Data.ExternalChatID, 10, 64) - b := getBotByChannelAndCID(c.ID, msg.Data.ChannelID) + b := getBot(c.ID, msg.Data.ChannelID) if b.ID == 0 || !b.Active { logger.Error(msg.Data.ChannelID, "mgWebhookHandler: missing or deactivated") w.WriteHeader(http.StatusBadRequest) @@ -377,3 +384,26 @@ func UploadUserAvatar(url string) (picURLs3 string, err error) { return } + +func getMessageID(data *tgbotapi.Message) string { + switch { + case data.Sticker != nil: + return "sticker" + case data.Audio != nil: + return "audio" + case data.Contact != nil: + return "contact" + case data.Document != nil: + return "document" + case data.Location != nil: + return "location" + case data.Video != nil: + return "video" + case data.Voice != nil: + return "voice" + case data.Photo != nil: + return "photo" + default: + return "undefined" + } +} diff --git a/translate/translate.en.yml b/translate/translate.en.yml index a1d1b5a..36cc424 100644 --- a/translate/translate.en.yml +++ b/translate/translate.en.yml @@ -27,4 +27,14 @@ error_creating_webhook: Error while creating webhook error_adding_bot: Error while adding bot error_save: An error occurred while saving, contact technical support missing_credentials: "Necessary credentials: {{.Credentials}}" -error_activity_mg: Check the activity with MessageGateway in CRM settings \ No newline at end of file +error_activity_mg: Check the activity with MessageGateway in CRM settings + +sticker: "[sticker]" +audio: "[audio file]" +contact: "[contact]" +document: "[document]" +location: "[location]" +video: "[video]" +voice: "[voice message]" +photo: "[image]" +undefined: "[undefined message format]" diff --git a/translate/translate.ru.yml b/translate/translate.ru.yml index 4aa4b70..e69c97f 100644 --- a/translate/translate.ru.yml +++ b/translate/translate.ru.yml @@ -28,3 +28,13 @@ error_adding_bot: Ошибка при добавлении бота error_save: Ошибка при сохранении, обратитесь в службу технической поддержки missing_credentials: "Необходимые методы: {{.Credentials}}" error_activity_mg: Проверьте активность интеграции с MessageGateway в настройках CRM + +sticker: "[стикер]" +audio: "[аудио файл]" +contact: "[контакт]" +document: "[документ]" +location: "[местонахождение]" +video: "[видео]" +voice: "[голосовое сообщение]" +photo: "[изображение]" +other: "[неопределенный формат сообщения]"