tgbot_misaka_5882f7/botcmd_translate.go

112 lines
2.6 KiB
Go

package main
import (
"regexp"
"strings"
"github.com/samber/lo"
tele "gopkg.in/telebot.v3"
"git.gensokyo.cafe/kkyy/tgbot_misaka_5882f7/openai"
"git.gensokyo.cafe/kkyy/tgbot_misaka_5882f7/openai/prompts"
)
var (
translateMenu = &tele.ReplyMarkup{}
translateBtnZhTw = translateMenu.Data("繁中", "btn_tr_zhtw", "Taiwanese Chinese")
translateBtnZhCn = translateMenu.Data("简中", "btn_tr_zhcn", "Mandarin Chinese")
translateBtnEn = translateMenu.Data("English", "btn_tr_en", "English")
translateBtnJa = translateMenu.Data("日本語", "btn_tr_ja", "Japanese")
translateBtnRetry = translateMenu.Data("Try again", "btn_tr_retry")
translateBtns = []*tele.Btn{
&translateBtnZhTw, &translateBtnZhCn, &translateBtnEn, &translateBtnJa,
&translateBtnRetry,
}
translateCmdRe = regexp.MustCompile(`^\s*\/tr(anslate)?(@\S*)?\s*`)
)
func init() {
translateMenu.Inline(
translateMenu.Row(translateBtnZhTw, translateBtnZhCn),
translateMenu.Row(translateBtnEn, translateBtnJa),
)
}
func handleTranslateCmd(c tele.Context) error {
msg := c.Message()
if msg == nil {
return nil
}
if msg.ReplyTo != nil {
msg = msg.ReplyTo
}
payload := strings.TrimSpace(translateCmdRe.ReplaceAllString(msg.Text, ""))
if payload == "" {
return c.Reply("Usage: `/tr <text>`",
&tele.SendOptions{ParseMode: tele.ModeMarkdown},
tele.Silent,
)
}
_, err := c.Bot().Reply(msg, "Sure. To what language?", tele.Silent, translateMenu)
return err
}
func handleTranslateBtn(c tele.Context) error {
msg := c.Message()
if msg == nil || msg.ReplyTo == nil {
return nil
}
origMsg := msg.ReplyTo
targetLang := c.Data()
txt := origMsg.Text
payload := strings.TrimSpace(translateCmdRe.ReplaceAllString(txt, ""))
if targetLang == "" || payload == "" {
return nil
}
req := openai.ChatRequest{
Model: openai.ModelGpt0305Turbo,
Messages: []openai.ChatMessage{
{
Role: openai.ChatRoleSystem,
Content: prompts.Translate(targetLang),
},
{
Role: openai.ChatRoleUser,
Content: payload,
},
},
Temperature: lo.ToPtr(0.2),
}
actionCh := setTyping(c)
err := assistantStreamedResponse(req, func(text string, finished bool) error {
var err error
<-actionCh
if finished {
retryBtn := translateBtnRetry
retryBtn.Data = targetLang
respMenu := &tele.ReplyMarkup{}
respMenu.Inline(respMenu.Row(retryBtn))
msg, err = c.Bot().Edit(msg, text, respMenu)
} else {
msg, err = c.Bot().Edit(msg, text)
}
return err
})
if err != nil {
logger.Errorf("failed to translate: req: %#+v, err: %v", req, err)
_, _ = c.Bot().Reply(origMsg, stickerFromID(stickerPanic), tele.Silent)
return err
}
return nil
}