Skip to content
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

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

Merged
merged 5 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-sqlite3 v1.14.16 h1:yOQRA0RpS5PFz/oikGwBEqvAWhWg5ufRz4ETLjwpU1Y=
github.com/mattn/go-sqlite3 v1.14.16/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/maxbrunsfeld/counterfeiter/v6 v6.6.1 h1:9XE5ykDiC8eNSqIPkxx0EsV3kMX1oe4kQWRZjIgytUA=
Expand All @@ -151,6 +153,8 @@ github.com/nekomeowww/xo v1.4.0 h1:aueb6SZPV5yF8Eauir8WF7iCBCa0Wjkrw6148y8iCsA=
github.com/nekomeowww/xo v1.4.0/go.mod h1:2puSnnYUtvKHV3krZfCRolEqTv9nuU+I59GhxADADFY=
github.com/nicksnyder/go-i18n/v2 v2.4.0 h1:3IcvPOAvnCKwNm0TB0dLDTuawWEj+ax/RERNC+diLMM=
github.com/nicksnyder/go-i18n/v2 v2.4.0/go.mod h1:nxYSZE9M0bf3Y70gPQjN9ha7XNHX7gMc814+6wVyEI4=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/onsi/ginkgo/v2 v2.16.0 h1:7q1w9frJDzninhXxjZd+Y/x54XNjG/UlRLIYPZafsPM=
github.com/onsi/ginkgo/v2 v2.16.0/go.mod h1:llBI3WDLL9Z6taip6f33H76YcWtJv+7R3HigUjbIBOs=
github.com/onsi/gomega v1.31.1 h1:KYppCUK+bUgAZwHOu7EXVBKyQA6ILvOESHkn/tgoqvo=
Expand Down Expand Up @@ -193,6 +197,10 @@ github.com/slack-go/slack v0.12.5 h1:ddZ6uz6XVaB+3MTDhoW04gG+Vc/M/X1ctC+wssy2cqs
github.com/slack-go/slack v0.12.5/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
Expand Down
44 changes: 44 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,44 @@
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
}

err = h.smrQueue.AddTask(data)

if err != nil {
h.logger.Error("failed to move task back to queue", zap.Error(err))
return nil, nil
}

// 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{
InlineKeyboard: [][]tgbotapi.InlineKeyboardButton{},
}))

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))
}
48 changes: 41 additions & 7 deletions internal/services/smr/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"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) processOutput(info types.TaskInfo, result *smr.URLSummarizatio
}
}

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 @@ func (s *Service) processError(err error, language string) string {
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,23 @@ func (s *Service) sendResult(output *smr.URLSummarizationOutput, info types.Task
ParseMode: tgbotapi.ModeHTML,
}

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

if err != nil {
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 +131,7 @@ func (s *Service) sendResult(output *smr.URLSummarizationOutput, info types.Task
)
}
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 +160,7 @@ func (s *Service) sendResult(output *smr.URLSummarizationOutput, info types.Task
)
}
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 +208,12 @@ func (s *Service) processor(info types.TaskInfo) {
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, !errors.Is(err, smr.ErrContentNotSupported))

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