Skip to content

feat: provide retry button when smr failed on telegram #202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions internal/bots/telegram/handlers/summarize/retry_callback_query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package summarize

import (
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/nekomeowww/insights-bot/pkg/bots/tgbot"
"github.com/nekomeowww/insights-bot/pkg/types/smr"
"go.uber.org/zap"
)

func (h *Handlers) handleCallbackQueryRetry(c *tgbot.Context) (tgbot.Response, error) {
messageID := c.Update.CallbackQuery.Message.MessageID
var data smr.TaskInfo

err := c.BindFromCallbackQueryData(&data)
if err != nil {
h.logger.Error("failed to bind callback query data when retry smr",
zap.Error(err),
zap.Int("message_id", messageID),
zap.Int64("chat_id", c.Update.CallbackQuery.Message.Chat.ID),
zap.Int64("from_id", c.Update.CallbackQuery.From.ID),
zap.String("data", c.Update.CallbackQuery.Data),
)

return nil, nil
}

h.smrQueue.AddTask(data)

Check failure on line 27 in internal/bots/telegram/handlers/summarize/retry_callback_query.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `h.smrQueue.AddTask` is not checked (errcheck)
// remove the retry button
c.Bot.MayRequest(tgbotapi.NewEditMessageTextAndMarkup(data.ChatID, messageID, h.i18n.TWithLanguage(data.Language, "commands.groups.summarization.commands.smr.reading"), tgbotapi.InlineKeyboardMarkup{}))

return nil, nil
}
1 change: 1 addition & 0 deletions internal/bots/telegram/handlers/summarize/summarize.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,5 @@ func (h *Handlers) Install(dispatcher *tgbot.Dispatcher) {

dispatcher.OnChannelPost(tgbot.NewHandler(h.HandleChannelPost))
dispatcher.OnCallbackQuery("smr/summarization/feedback/react", tgbot.NewHandler(h.handleCallbackQueryReact))
dispatcher.OnCallbackQuery("smr/summarization/retry", tgbot.NewHandler(h.handleCallbackQueryRetry))
}
47 changes: 40 additions & 7 deletions internal/services/smr/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"go.uber.org/zap"
)

func (s *Service) processOutput(info types.TaskInfo, result *smr.URLSummarizationOutput) string {
func (s *Service) formatOutput(info types.TaskInfo, result *smr.URLSummarizationOutput) string {
switch info.Platform {
case bot.FromPlatformTelegram:
return result.FormatSummarizationAsHTML()
Expand All @@ -31,7 +31,7 @@
}
}

func (s *Service) processError(err error, language string) string {
func (s *Service) formatError(err error, language string) string {
if errors.Is(err, smr.ErrContentNotSupported) {
return s.i18n.TWithLanguage(language, "commands.groups.summarization.commands.smr.contentNotSupported")
} else if errors.Is(err, smr.ErrNetworkError) || errors.Is(err, smr.ErrRequestFailed) {
Expand All @@ -41,7 +41,22 @@
return s.i18n.TWithLanguage(language, "commands.groups.summarization.commands.smr.failedToRead")
}

func (s *Service) sendResult(output *smr.URLSummarizationOutput, info types.TaskInfo, result string) {
func (s *Service) newRetryButtonMarkup(info types.TaskInfo) (tgbotapi.InlineKeyboardMarkup, error) {
data, err := s.tgBot.Bot().AssignOneCallbackQueryData("smr/summarization/retry", &info)

if err != nil {
return tgbotapi.InlineKeyboardMarkup{}, err
}

return tgbotapi.NewInlineKeyboardMarkup([]tgbotapi.InlineKeyboardButton{
{
Text: s.i18n.TWithLanguage(info.Language, "commands.groups.summarization.commands.smr.retry"),
CallbackData: &data,
},
}), nil
}

func (s *Service) sendResult(output *smr.URLSummarizationOutput, info types.TaskInfo, result string, provideRetryButton bool) {
switch info.Platform {
case bot.FromPlatformTelegram:
msgEdit := tgbotapi.EditMessageTextConfig{
Expand All @@ -53,6 +68,22 @@
ParseMode: tgbotapi.ModeHTML,
}

if provideRetryButton {
var err error
retryButtonMarkup, err := s.newRetryButtonMarkup(info)
if err != nil {

Check failure on line 74 in internal/services/smr/processor.go

View workflow job for this annotation

GitHub Actions / Lint

only one cuddle assignment allowed before if statement (wsl)
s.logger.Error("smr service: failed to create retry button markup",
zap.Error(err),
zap.Int64("chat_id", info.ChatID),
zap.String("platform", info.Platform.String()),
)

return
}

msgEdit.ReplyMarkup = &retryButtonMarkup
}

if output == nil {
_, err := s.tgBot.Send(msgEdit)
if err != nil {
Expand Down Expand Up @@ -99,6 +130,7 @@
)
}
case bot.FromPlatformSlack:
// TODO: provide retry button
token, err := s.ent.SlackOAuthCredentials.Query().
Where(slackoauthcredentials.TeamID(info.TeamID)).
First(context.Background())
Expand Down Expand Up @@ -127,6 +159,7 @@
)
}
case bot.FromPlatformDiscord:
// TODO: provide retry button
channelID, _ := snowflake.Parse(info.ChannelID)
_, err := s.discordBot.Rest().
CreateMessage(channelID, discord.NewMessageCreateBuilder().
Expand Down Expand Up @@ -174,12 +207,12 @@
smrResult, err := s.model.SummarizeInputURL(ctx, info.URL, info.Platform)
if err != nil {
s.logger.Warn("smr service: summarization failed", zap.Error(err))
errStr := s.processError(err, lo.Ternary(info.Language == "", "en", info.Language))
s.sendResult(nil, info, errStr)
errStr := s.formatError(err, lo.Ternary(info.Language == "", "en", info.Language))
s.sendResult(nil, info, errStr, true)

return
}

finalResult := s.processOutput(info, smrResult)
s.sendResult(smrResult, info, finalResult)
finalResult := s.formatOutput(info, smrResult)
s.sendResult(smrResult, info, finalResult, false)
}
1 change: 1 addition & 0 deletions locales/en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ commands:
failedToRead: Quantum Speed-Reading was unsuccessful. Would you like to retry?
failedToReadDueToFailedToFetch: Encountered an issue retrieving the content for Quantum Speed-Reading. Perhaps another attempt might succeed?
contentNotSupported: This content is not supported by Quantum Speed-Reading. Considering another link might be beneficial.
retry: Retry

prompts:
smr:
Expand Down
1 change: 1 addition & 0 deletions locales/zh-CN.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ commands:
failedToReadDueToFailedToFetch: 量子速读的链接读取失败了哦。可以再试试?
contentNotSupported: 暂时不支持量子速读这样的内容呢,可以换个别的链接试试。
permissionDenied: 本应用没有权限向这个频道发送消息,尝试重新安装一下?
retry: 重试

modules:
telegram:
Expand Down
Loading