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") translateBtns = []*tele.Btn{ &translateBtnZhTw, &translateBtnZhCn, &translateBtnEn, &translateBtnJa, } 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 `", &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 } // change the temporary message if err := c.Edit("Sure, please wait..."); err != nil { logger.Warnf("failed to alter the temporary message: %v", err) } // pretend to be typing if err := c.Bot().Notify(msg.Chat, tele.Typing); err != nil { logger.Warnf("failed to send typing action: %v", err) } ai := openai.NewClient(config.OpenAIApiKey) 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), } logger.Debugf("Openai chat request: %#+v", req) resp, err := ai.ChatCompletion(req) if err != nil { logger.Errorf("failed to translate: req: %#+v, err: %v", req, err) _, err := c.Bot().Reply(origMsg, stickerFromID(stickerPanic), tele.Silent) return err } respText := resp.Choices[0].Message.Content _, err = c.Bot().Reply(origMsg, respText, tele.Silent) // delete the temporary message if err := c.Delete(); err != nil { logger.Warnf("failed to delete the temporary message: %v", err) } return err }