Skip to content

Commit

Permalink
add declineCallback, remove cancel command
Browse files Browse the repository at this point in the history
  • Loading branch information
samarets committed Oct 13, 2022
1 parent 219cc49 commit 2c09dae
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 51 deletions.
4 changes: 2 additions & 2 deletions internal/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ func (b *bot) InitUpdates() {
switch callbackKey {
case acceptCallback:
b.AcceptCallback(update, update.CallbackQuery.From.ID, userID)
case declineCallback:
b.DeclineCallback(update, update.CallbackQuery.From.ID, userID)
}

continue
Expand All @@ -101,8 +103,6 @@ func (b *bot) InitUpdates() {
b.StartCommand(update, userState)
case breakCommand:
b.BreakCommand(update)
case cancelCommand:
b.CancelCommand(update)
case getIDCommand:
b.GetID(update)
case setGroupCommand:
Expand Down
59 changes: 58 additions & 1 deletion internal/bot/callbacks.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,65 @@ func (b *bot) sendBufferMessages(supportID, userID int64) error {
return nil
}

func (b *bot) DeclineCallback() {
func (b *bot) DeclineCallback(update tgbotapi.Update, supportID, userID int64) {
st, err := b.db.getUserState(userID)
if err != nil {
log.Error().Err(err).Send()
return
}
if st != queueState {
callback := tgbotapi.NewCallback(
update.CallbackQuery.ID,
b.tl.GetMessageWithoutPrefix(b.db.languageDB().get(supportID), "user_no_expect"),
)
_, err := b.bot.Request(callback)
if err != nil {
log.Error().Err(err).Send()
return
}

return
}

updateMsg := tgbotapi.NewEditMessageText(
update.FromChat().ID,
update.CallbackQuery.Message.MessageID,
b.tl.GetMessageWithoutPrefix(
"", "success_decline_appeal", map[string]interface{}{
"LastMessage": update.CallbackQuery.Message.Text,
"Name": update.SentFrom().FirstName + " " + update.SentFrom().LastName,
"ID": supportID,
},
),
)
updateMsg.ParseMode = tgbotapi.ModeMarkdown
_, err = b.bot.Request(updateMsg)
if err != nil {
log.Error().Err(err).Send()
return
}

err = b.db.queueDB().delete(userID)
if err != nil {
log.Error().Err(err).Send()
return
}

err = b.db.bufferDB().delete(userID)
if err != nil {
log.Error().Err(err).Send()
return
}

msg := tgbotapi.NewMessage(
userID,
b.tl.GetMessage(b.db.languageDB().get(userID), "request_declined"),
)
_, err = b.bot.Send(msg)
if err != nil {
log.Error().Err(err).Send()
return
}
}

func createCallbackData(key string, userID int64) string {
Expand Down
57 changes: 18 additions & 39 deletions internal/bot/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import (
)

const (
startCommand = "start"
breakCommand = "break"
cancelCommand = "cancel"
getIDCommand = "getid"
startCommand = "start"
breakCommand = "break"
getIDCommand = "getid"

setGroupCommand = "set_group"
addSupportCommand = "add_support"
Expand Down Expand Up @@ -53,65 +52,45 @@ func (b *bot) StartCommand(update tgbotapi.Update, userState state) {
}

func (b *bot) BreakCommand(update tgbotapi.Update) {
whomBreak, err := b.db.roomsDB().get(update.Message.Chat.ID)
if err != nil {
log.Error().Err(err).Send()
return
}
if whomBreak == nil {
return
}

err = b.db.roomsDB().delete(*whomBreak)
if err != nil {
log.Error().Err(err).Send()
return
}

err = b.db.roomsDB().delete(update.Message.Chat.ID)
if err != nil {
log.Error().Err(err).Send()
if !b.hasRight(update.SentFrom().ID) {
return
}

msg := tgbotapi.NewMessage(*whomBreak, "🤖 Розмову з оператором було завершено")
_, err = b.bot.Send(msg)
whomBreak, err := b.db.roomsDB().get(update.SentFrom().ID)
if err != nil {
log.Error().Err(err).Send()
return
}

msg = tgbotapi.NewMessage(update.Message.Chat.ID, "🤖 Ви завершили розмову з користувачем")
_, err = b.bot.Send(msg)
if err != nil {
log.Error().Err(err).Send()
if whomBreak == nil {
return
}
}

func (b *bot) CancelCommand(update tgbotapi.Update) {
userTg, err := b.db.queueDB().get(update.Message.Chat.ID)
err = b.db.roomsDB().delete(*whomBreak)
if err != nil {
log.Error().Err(err).Send()
return
}
if userTg == nil {
return
}

err = b.db.queueDB().delete(update.Message.Chat.ID)
err = b.db.roomsDB().delete(update.SentFrom().ID)
if err != nil {
log.Error().Err(err).Send()
return
}

err = b.db.bufferDB().delete(update.Message.Chat.ID)
msg := tgbotapi.NewMessage(
update.SentFrom().ID,
b.tl.GetMessage(b.db.languageDB().get(update.Message.Chat.ID), "chat_end"),
)
_, err = b.bot.Send(msg)
if err != nil {
log.Error().Err(err).Send()
return
}

msg := tgbotapi.NewMessage(update.Message.Chat.ID, "🤖 Ви були видалені з черги")
msg = tgbotapi.NewMessage(
*whomBreak,
b.tl.GetMessage(b.db.languageDB().get(*whomBreak), "operator_leave"),
)
_, err = b.bot.Send(msg)
if err != nil {
log.Error().Err(err).Send()
Expand All @@ -120,7 +99,7 @@ func (b *bot) CancelCommand(update tgbotapi.Update) {
}

func (b *bot) GetID(update tgbotapi.Update) {
msg := tgbotapi.NewMessage(update.Message.Chat.ID, fmt.Sprintf("🤖 %d", update.SentFrom().ID))
msg := tgbotapi.NewMessage(update.Message.Chat.ID, fmt.Sprintf(b.tl.Prefix+" %d", update.SentFrom().ID))
_, err := b.bot.Send(msg)
if err != nil {
log.Error().Err(err).Send()
Expand Down
6 changes: 3 additions & 3 deletions internal/translations/translation.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

type Translator struct {
i18n *i18n.I18n
prefix string
Prefix string
}

func NewTranslator(localesDir, defaultLanguage, botPrefix string) (*Translator, error) {
Expand Down Expand Up @@ -42,12 +42,12 @@ func NewTranslator(localesDir, defaultLanguage, botPrefix string) (*Translator,

return &Translator{
i18n: i,
prefix: botPrefix,
Prefix: botPrefix,
}, nil
}

func (t *Translator) GetMessage(lang, id string, args ...interface{}) string {
return t.prefix + " " + t.i18n.Tr(lang, id, args...)
return t.Prefix + " " + t.i18n.Tr(lang, id, args...)
}

func (t *Translator) GetMessageWithoutPrefix(lang, id string, args ...interface{}) string {
Expand Down
6 changes: 4 additions & 2 deletions locales/en-US/admin.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
"user_block_bot_update": "*{{ .LastMessage }}*\n\n_The user blocked the bot, message received agent {{ .ID }}_ [{{ .Name }}](tg:https://user?id={{ .ID }})",
"user_block_bot": "The user blocked the bot, so the connection was lost",
"you_block_bot": "You have blocked the bot, unable to create a chat",
"success_accept_appeal": "*{{ .LastMessage }}*\n\n_The appeal was accepted by the agent {{ .ID }}_ [{{ .Name }}](tg:https://user?id={{ .ID }})",
"no_rights": "You do not have sufficient rights to perform this action"
"success_accept_appeal": "*{{ .LastMessage }}*\n\n_✅ The appeal was accepted by the agent {{ .ID }}_ [{{ .Name }}](tg:https://user?id={{ .ID }})",
"success_decline_appeal": "*{{ .LastMessage }}*\n\n_❌ The appeal was declined by the agent {{ .ID }}_ [{{ .Name }}](tg:https://user?id={{ .ID }})",
"no_rights": "You do not have sufficient rights to perform this action",
"chat_end": "You have ended the conversation with the user"
}
4 changes: 3 additions & 1 deletion locales/en-US/user.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
"queue_start": "You are in the support queue, your question has been transferred, please wait for a support operator to join you",
"room_start": "The operator has already joined you, write your questions in this chat",
"operator_connected": "An operator has joined you",
"got_message": "We received your message, if you want to add something, write it.\n\nAn operator will join you shortly"
"got_message": "We received your message, if you want to add something, write it.\n\nAn operator will join you shortly",
"request_declined": "Your request has been rejected. If you believe that the request was rejected without meaning, then try again",
"operator_leave": "The conversation with the operator has been completed"
}
6 changes: 4 additions & 2 deletions locales/uk-UA/admin.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
"user_block_bot_update": "*{{ .LastMessage }}*\n\n_Користувач заблокував бота, повідомлення отримав агент {{ .ID }}_ [{{ .Name }}](tg:https://user?id={{ .ID }})",
"user_block_bot": "Користувач заблокував бота, тому зв'язок був розірваний",
"you_block_bot": "Ви заблокували бота, неможливо створити чат",
"success_accept_appeal": "*{{ .LastMessage }}*\n\n_Звернення було прийняте агентом {{ .ID }}_ [{{ .Name }}](tg:https://user?id={{ .ID }})",
"no_rights": "Недостатньо прав для виконання цієї дії"
"success_accept_appeal": "*{{ .LastMessage }}*\n\n_✅ Звернення було прийняте агентом {{ .ID }}_ [{{ .Name }}](tg:https://user?id={{ .ID }})",
"success_decline_appeal": "*{{ .LastMessage }}*\n\n_❌ Запит був відхилений агентом {{ .ID }}_ [{{ .Name }}](tg:https://user?id={{ .ID }})",
"no_rights": "Недостатньо прав для виконання цієї дії",
"chat_end": "Ви завершили розмову з користувачем"
}
4 changes: 3 additions & 1 deletion locales/uk-UA/user.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
"queue_start": "Ви перебуваєте в черзі підтримки, ваше запитання переслано, будь ласка, зачекайте, поки до вас приєднається оператор підтримки",
"room_start": "Оператор вже приєднався до вас, напишіть ваше запитання у цей чат",
"operator_connected": "До вас приєднався оператор",
"got_message": "Ваше повідомлення було отримано, якщо у вас є що додати - напишіть.\n\nСкоро до вас доєднається оператор технічної підтримки"
"got_message": "Ваше повідомлення було отримано, якщо у вас є що додати - напишіть.\n\nСкоро до вас доєднається оператор технічної підтримки",
"request_declined": "Ваш запит був відхилений. Якщо вважаєте, що запит був беззмістовно відхилений, то спробуйте ще раз",
"operator_leave": "Розмову з оператором було завершено"
}

0 comments on commit 2c09dae

Please sign in to comment.