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: split out the smr i18n #189

Merged
merged 4 commits into from
Mar 14, 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
5 changes: 4 additions & 1 deletion internal/bots/discord/listeners/listeners.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ func (b *Listeners) smrCmd(event *events.ApplicationCommandInteractionCreate, da
if smr.IsUrlCheckError(err) {
err = event.CreateMessage(
discord.NewMessageCreateBuilder().
SetContent(smr.FormatUrlCheckError(err, bot.FromPlatformDiscord)).
// TODO: i18n support for discord
SetContent(smr.FormatUrlCheckError(err, bot.FromPlatformDiscord, "", nil)).
Build(),
)
if err != nil {
Expand Down Expand Up @@ -89,6 +90,8 @@ func (b *Listeners) smrCmd(event *events.ApplicationCommandInteractionCreate, da
Platform: bot.FromPlatformDiscord,
URL: urlString,
ChannelID: event.Channel().ID().String(),
// TODO: support i18n for discord and slack
Language: "zh-CN",
})
if err != nil {
b.logger.Warn("discord: failed to add task", zap.Error(err))
Expand Down
5 changes: 4 additions & 1 deletion internal/bots/slack/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ func (h *Handlers) PostCommandInfo(ctx *gin.Context) {
err, originErr := smr.CheckUrl(urlString)
if err != nil {
if smr.IsUrlCheckError(err) {
ctx.JSON(http.StatusOK, slackbot.NewSlackWebhookMessage(smr.FormatUrlCheckError(err, bot.FromPlatformSlack)))
// TODO: i18n support for slack
ctx.JSON(http.StatusOK, slackbot.NewSlackWebhookMessage(smr.FormatUrlCheckError(err, bot.FromPlatformSlack, "", nil)))
return
}

Expand Down Expand Up @@ -125,6 +126,8 @@ func (h *Handlers) PostCommandInfo(ctx *gin.Context) {
URL: urlString,
ChannelID: body.ChannelID,
TeamID: body.TeamID,
// TODO: support i18n for discord and slack
Language: "zh-CN",
})
if err != nil {
h.logger.Warn("slack: failed to add task", zap.Error(err))
Expand Down
4 changes: 3 additions & 1 deletion internal/bots/telegram/handlers/recap/recap.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ func NewHandlers() func(NewHandlersParams) *Handlers {
}

func (h *Handlers) Install(dispatcher *tgbot.Dispatcher) {
dispatcher.OnCommandGroup("聊天回顾", []tgbot.Command{
dispatcher.OnCommandGroup(func(c *tgbot.Context) string {
return "聊天回顾"
}, []tgbot.Command{
{
Command: "recap",
Handler: tgbot.NewHandler(h.command.handleRecapCommand),
Expand Down
22 changes: 15 additions & 7 deletions internal/bots/telegram/handlers/summarize/smr_command.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package summarize

import (
"fmt"
"strings"
"time"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/samber/lo"
"go.uber.org/zap"

"github.com/nekomeowww/insights-bot/internal/services/smr"
"github.com/nekomeowww/insights-bot/pkg/bots/tgbot"
"github.com/nekomeowww/insights-bot/pkg/i18n"
"github.com/nekomeowww/insights-bot/pkg/types/bot"
types "github.com/nekomeowww/insights-bot/pkg/types/smr"
"github.com/samber/lo"
"go.uber.org/zap"
)

func (h *Handlers) Handle(c *tgbot.Context) (tgbot.Response, error) {
Expand All @@ -29,15 +30,15 @@ func (h *Handlers) Handle(c *tgbot.Context) (tgbot.Response, error) {
if err != nil {
if smr.IsUrlCheckError(err) {
return nil, tgbot.
NewMessageError(smr.FormatUrlCheckError(err, bot.FromPlatformTelegram)).
NewMessageError(smr.FormatUrlCheckError(err, bot.FromPlatformTelegram, c.Language(), h.i18n)).
WithReply(c.Update.Message).
WithParseModeHTML()
}

return nil, tgbot.NewExceptionError(originErr).WithReply(c.Update.Message)
}

message := tgbotapi.NewMessage(c.Update.Message.Chat.ID, "请稍等,量子速读中...")
message := tgbotapi.NewMessage(c.Update.Message.Chat.ID, c.T("commands.groups.summarization.commands.smr.reading"))
message.ReplyToMessageID = c.Update.Message.MessageID

processingMessage, err := c.Bot.Send(message)
Expand All @@ -54,7 +55,10 @@ func (h *Handlers) Handle(c *tgbot.Context) (tgbot.Response, error) {
}
if !ok {
return nil, tgbot.
NewMessageError(fmt.Sprintf("很抱歉,您的操作触发了我们的限制机制,为了保证系统的可用性,本命令每最多 %d 分钟最多使用一次,请您耐心等待 %d 分钟后再试,感谢您的理解和支持。", perSeconds, lo.Ternary(ttl/time.Minute <= 1, 1, ttl/time.Minute))).
NewMessageError(c.T("", i18n.M{
"Seconds": perSeconds,
"SecondsToBeWaited": lo.Ternary(ttl/time.Minute <= 1, 1, ttl/time.Minute),
})).
WithReply(c.Update.Message)
}

Expand All @@ -63,9 +67,13 @@ func (h *Handlers) Handle(c *tgbot.Context) (tgbot.Response, error) {
URL: urlString,
ChatID: c.Update.Message.Chat.ID,
MessageID: processingMessage.MessageID,
Language: c.Language(),
})
if err != nil {
return nil, tgbot.NewExceptionError(err).WithMessage("量子速读失败了,可以再试试?").WithEdit(&processingMessage)
return nil, tgbot.
NewExceptionError(err).
WithMessage(c.T("commands.groups.summarization.commands.smr.failedToRead")).
WithEdit(&processingMessage)
}

return nil, nil
Expand Down
10 changes: 8 additions & 2 deletions internal/bots/telegram/handlers/summarize/summarize.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/nekomeowww/insights-bot/internal/models/smr"
"github.com/nekomeowww/insights-bot/internal/services/smr/smrqueue"
"github.com/nekomeowww/insights-bot/pkg/bots/tgbot"
"github.com/nekomeowww/insights-bot/pkg/i18n"
"github.com/nekomeowww/insights-bot/pkg/logger"
"go.uber.org/fx"
)
Expand All @@ -18,6 +19,7 @@ type NewHandlersParams struct {
fx.In

Logger *logger.Logger
I18n *i18n.I18n
SMR *smr.Model
SmrQueue *smrqueue.Queue
}
Expand All @@ -26,6 +28,7 @@ var _ tgbot.HandlerGroup = (*Handlers)(nil)

type Handlers struct {
logger *logger.Logger
i18n *i18n.I18n
smr *smr.Model
smrQueue *smrqueue.Queue
}
Expand All @@ -34,6 +37,7 @@ func NewHandlers() func(NewHandlersParams) *Handlers {
return func(param NewHandlersParams) *Handlers {
handler := &Handlers{
logger: param.Logger,
i18n: param.I18n,
smrQueue: param.SmrQueue,
smr: param.SMR,
}
Expand All @@ -43,12 +47,14 @@ func NewHandlers() func(NewHandlersParams) *Handlers {
}

func (h *Handlers) Install(dispatcher *tgbot.Dispatcher) {
dispatcher.OnCommandGroup("量子速读", []tgbot.Command{
dispatcher.OnCommandGroup(func(c *tgbot.Context) string {
return c.T("commands.groups.summarization.name")
}, []tgbot.Command{
{
Command: "smr",
Handler: tgbot.NewHandler(h.Handle),
HelpMessage: func(c *tgbot.Context) string {
return "量子速读网页文章(也支持在频道中使用) 用法:/smr <code>&lt;链接&gt;</code>"
return c.T("commands.groups.summarization.commands.smr.help")
},
},
})
Expand Down
11 changes: 6 additions & 5 deletions internal/services/smr/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ func (s *Service) processOutput(info types.TaskInfo, result *smr.URLSummarizatio
}
}

func (s *Service) processError(err error) string {
func (s *Service) processError(err error, language string) string {
if errors.Is(err, smr.ErrContentNotSupported) {
return "暂时不支持量子速读这样的内容呢,可以换个别的链接试试。"
return s.i18n.TWithLanguage(language, "commands.groups.summarization.commands.smr.contentNotSupported")
} else if errors.Is(err, smr.ErrNetworkError) || errors.Is(err, smr.ErrRequestFailed) {
return "量子速读的链接读取失败了哦。可以再试试?"
return s.i18n.TWithLanguage(language, "commands.groups.summarization.commands.smr.failedToReadDueToFailedToFetch")
}

return "量子速读失败了。可以再试试?"
return s.i18n.TWithLanguage(language, "commands.groups.summarization.commands.smr.failedToRead")
}

func (s *Service) sendResult(output *smr.URLSummarizationOutput, info types.TaskInfo, result string) {
Expand Down Expand Up @@ -174,7 +174,8 @@ 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)
// TODO: support i18n for discord and slack
errStr := s.processError(err, lo.Ternary(info.Language == "", "en", info.Language))
s.sendResult(nil, info, errStr)

return
Expand Down
6 changes: 5 additions & 1 deletion internal/services/smr/smr.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/nekomeowww/insights-bot/pkg/bots/discordbot"
"github.com/nekomeowww/insights-bot/pkg/bots/slackbot"
"github.com/nekomeowww/insights-bot/pkg/bots/tgbot"
"github.com/nekomeowww/insights-bot/pkg/i18n"
"github.com/nekomeowww/insights-bot/pkg/logger"
"github.com/redis/rueidis"
"github.com/samber/lo"
Expand All @@ -34,6 +35,7 @@ type NewServiceParam struct {

Config *configs.Config
Logger *logger.Logger
I18n *i18n.I18n

RedisClient *datastore.Redis
Ent *datastore.Ent
Expand All @@ -49,6 +51,7 @@ type NewServiceParam struct {
type Service struct {
logger *logger.Logger
config *configs.Config
i18n *i18n.I18n

ent *datastore.Ent

Expand All @@ -73,8 +76,9 @@ func NewService() func(param NewServiceParam) (*Service, error) {
return func(param NewServiceParam) (*Service, error) {
s := &Service{
logger: param.Logger,
ent: param.Ent,
i18n: param.I18n,
config: param.Config,
ent: param.Ent,
model: param.Model,
queue: param.Queue,
tgBot: param.TgBot,
Expand Down
9 changes: 6 additions & 3 deletions internal/services/smr/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"net/url"

"github.com/nekomeowww/insights-bot/pkg/i18n"
"github.com/nekomeowww/insights-bot/pkg/types/bot"
"github.com/samber/lo"
)
Expand All @@ -24,12 +25,13 @@ func CheckUrl(urlString string) (error, error) {
return nil, nil
}

func FormatUrlCheckError(err error, platform bot.FromPlatform) string {
func FormatUrlCheckError(err error, platform bot.FromPlatform, language string, i18n *i18n.I18n) string {
switch {
case errors.Is(err, ErrNoLink):
switch platform {
case bot.FromPlatformTelegram:
return "没有找到链接,可以发送一个有效的链接吗?用法:<code>/smr &lt;链接&gt;</code>"
return i18n.TWithLanguage(language, "commands.groups.summarization.commands.smr.noLinksFound")
// TODO: support i18n for discord and slack
case bot.FromPlatformDiscord, bot.FromPlatformSlack:
return "没有找到链接,可以发送一个有效的链接吗?用法:`/smr <链接>`"
default:
Expand All @@ -38,8 +40,9 @@ func FormatUrlCheckError(err error, platform bot.FromPlatform) string {
case errors.Is(err, ErrParse), errors.Is(err, ErrScheme):
switch platform {
case bot.FromPlatformTelegram:
return "你发来的链接无法被理解,可以重新发一个试试。用法:<code>/smr &lt;链接&gt;</code>"
return i18n.TWithLanguage(language, "commands.groups.summarization.commands.smr.invalidLink")
case bot.FromPlatformDiscord, bot.FromPlatformSlack:
// TODO: support i18n for discord and slack
return "你发来的链接无法被理解,可以重新发一个试试。用法:`/smr <链接>`"
default:
return err.Error()
Expand Down
65 changes: 52 additions & 13 deletions locales/en.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,54 @@
system:
commands:
start:
help: Start interacting with the bot
help:
help: Get help
message: |
Hi! 👋 Welcome using Insights Bot!

I currently support these commands:

{{ .Commands }}
cancel:
help: Cancel the current activated operation
alreadyCancelledAll: There is no activated operation to cancel
groups:
basic:
name: 基础命令
commands:
start:
help: Start interacting with the bot
help:
help: Get help
message: |
Hi! 👋 Welcome using Insights Bot!

I currently support these commands:

{{ .Commands }}
cancel:
help: Cancel the current activated operation
alreadyCancelledAll: There is no activated operation to cancel

commands:
groups:
summarization:
name: 量子速读
commands:
smr:
help: 量子速读网页文章(也支持在频道中使用) 用法:/smr <code>&lt;链接&gt;</code>
noLinksFound: 没有找到链接,可以发送一个有效的链接吗?用法:<code>/smr &lt;链接&gt;</code>
invalidLink: 你发来的链接无法被理解,可以重新发一个试试。用法:<code>/smr &lt;链接&gt;</code>
reading: 请稍等,量子速读中...
rateLimitExceeded: 很抱歉,您的操作触发了我们的限制机制,为了保证系统的可用性,本命令每最多 {{ .Seconds }} 秒使用一次,请您耐心等待 {{ .SecondsToBeWaited }} 秒后再试,感谢您的理解和支持。
failedToRead: 量子速读失败了,可以再试试?
failedToReadDueToFailedToFetch: 量子速读的链接读取失败了哦。可以再试试?
contentNotSupported: 暂时不支持量子速读这样的内容呢,可以换个别的链接试试。

prompts:
smr:
- role: system
content: |
你是我的网页文章阅读助理。我将为你提供文章的标题、作
者、所抓取的网页中的正文等信息,然后你将对文章做出总结。\n请你在总结时满足以下要求:
1. 首先如果文章的标题不是中文的请依据上下文将标题信达雅的翻译为简体中文并放在第一行
2. 然后从我提供的文章信息中总结出一个三百字以内的文章的摘要
3. 最后,你将利用你已有的知识和经验,对我提供的文章信息提出 3 个具有创造性和发散思维的问题
4. 请用简体中文进行回复
最终你回复的消息格式应像这个例句一样(例句中的双花括号为需要替换的内容):\n
{{简体中文标题,可省略}}\n\n摘要:{{文章的摘要}}\n\n关联提问:\n1. {{关联提问 1}}\n2. {{关联提问 2}}\n2. {{关联提问 3}}
- role: user
content: |
我的第一个要求相关的信息如下:
文章标题:{{ .Title }}
文章作者:{{ .By }}
文章正文:{{ .Content }}
接下来请你完成我所要求的任务。
65 changes: 52 additions & 13 deletions locales/zh-CN.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,54 @@
system:
commands:
start:
help: 开始与 Bot 的交互
help:
help: 获取帮助
message: |
你好!👋 欢迎使用 Insights Bot!

我当前支持这些命令:

{{ .Commands }}
cancel:
help: 取消当前操作
alreadyCancelledAll: 已经没有正在进行的操作了
groups:
basic:
name: 基础命令
commands:
start:
help: 开始与 Bot 的交互
help:
help: 获取帮助
message: |
你好!👋 欢迎使用 Insights Bot!

我当前支持这些命令:

{{ .Commands }}
cancel:
help: 取消当前操作
alreadyCancelledAll: 已经没有正在进行的操作了

commands:
groups:
summarization:
name: 量子速读
commands:
smr:
help: 量子速读网页文章(也支持在频道中使用) 用法:/smr <code>&lt;链接&gt;</code>
noLinksFound: 没有找到链接,可以发送一个有效的链接吗?用法:<code>/smr &lt;链接&gt;</code>
invalidLink: 你发来的链接无法被理解,可以重新发一个试试。用法:<code>/smr &lt;链接&gt;</code>
reading: 请稍等,量子速读中...
rateLimitExceeded: 很抱歉,您的操作触发了我们的限制机制,为了保证系统的可用性,本命令每最多 {{ .Seconds }} 秒使用一次,请您耐心等待 {{ .SecondsToBeWaited }} 秒后再试,感谢您的理解和支持。
failedToRead: 量子速读失败了,可以再试试?
failedToReadDueToFailedToFetch: 量子速读的链接读取失败了哦。可以再试试?
contentNotSupported: 暂时不支持量子速读这样的内容呢,可以换个别的链接试试。

prompts:
smr:
- role: system
content: |
你是我的网页文章阅读助理。我将为你提供文章的标题、作
者、所抓取的网页中的正文等信息,然后你将对文章做出总结。\n请你在总结时满足以下要求:
1. 首先如果文章的标题不是中文的请依据上下文将标题信达雅的翻译为简体中文并放在第一行
2. 然后从我提供的文章信息中总结出一个三百字以内的文章的摘要
3. 最后,你将利用你已有的知识和经验,对我提供的文章信息提出 3 个具有创造性和发散思维的问题
4. 请用简体中文进行回复
最终你回复的消息格式应像这个例句一样(例句中的双花括号为需要替换的内容):\n
{{简体中文标题,可省略}}\n\n摘要:{{文章的摘要}}\n\n关联提问:\n1. {{关联提问 1}}\n2. {{关联提问 2}}\n2. {{关联提问 3}}
- role: user
content: |
我的第一个要求相关的信息如下:
文章标题:{{ .Title }}
文章作者:{{ .By }}
文章正文:{{ .Content }}
接下来请你完成我所要求的任务。
Loading
Loading