Skip to content

Commit

Permalink
internal/plugin,internal/telegram,internal/tts: refactor hub router ,…
Browse files Browse the repository at this point in the history
… switch between different configs
  • Loading branch information
scbizu committed Nov 28, 2023
1 parent 596075c commit e2cef62
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 15 deletions.
17 changes: 11 additions & 6 deletions internal/plugin/hub/hub.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/scbizu/Astral/internal/plugin/py"
"github.com/scbizu/Astral/internal/plugin/sayhi"
anime "github.com/scbizu/Astral/internal/plugin/today-anime"
"github.com/scbizu/Astral/internal/plugin/tts"
"github.com/sirupsen/logrus"
)

Expand All @@ -20,6 +21,7 @@ var plugins = []plugin.IPlugin{
&sayhi.Handler{},
&anime.Handler{},
&ai.AICommands{},
&tts.TTSCommand{},
// py plugin must be put in the end
&py.Handler{},
}
Expand Down Expand Up @@ -62,17 +64,20 @@ func (ph *TGPluginHub) AddTGPlugin(
}

// Do iters telegram plugin
func (ph *TGPluginHub) Do(rawmsg *tgbotapi.Message) (msg tgbotapi.Chattable) {
func (ph *TGPluginHub) Do(rawmsg *tgbotapi.Message) tgbotapi.Chattable {
for _, p := range ph.GetEnabledTelegramPlugins() {
msg, _ = p.Run(rawmsg)
if _, ok := msg.(tgbotapi.MessageConfig); ok {
msgConf := msg.(tgbotapi.MessageConfig)
msg, _ := p.Run(rawmsg)
switch msgConf := msg.(type) {
case tgbotapi.VoiceConfig:
logrus.Infof("[chatID:%d]", msgConf.ChatID)
return msgConf
case tgbotapi.MessageConfig:
logrus.Infof("[chatID:%d,msg:%s]", msgConf.ChatID, msgConf.Text)
if p.Validate(msgConf) {
return
return msgConf
}
}
}

return
return tgbotapi.NewMessage(rawmsg.Chat.ID, "hello ?")
}
11 changes: 7 additions & 4 deletions internal/plugin/tts/tts.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package tts

import (
"bytes"
"context"
"fmt"
"strings"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/scbizu/Astral/internal/plugin"
Expand All @@ -28,13 +30,14 @@ func (t *TTSCommand) Process(msg *tgbotapi.Message) tgbotapi.Chattable {
"tts",
func(msg *tgbotapi.Message) tgbotapi.Chattable {
c := itts.NewElevenLabClient()
bs, err := c.ToSpeech(context.Background(), msg.Text)
text := strings.TrimPrefix(msg.Text, "/tts")
bs, err := c.ToSpeech(context.Background(), text)
if err != nil {
return tgbotapi.NewMessage(msg.Chat.ID, err.Error())
}
return tgbotapi.NewVoice(msg.Chat.ID, tgbotapi.FileBytes{
Name: fmt.Sprintf("%s.mp3", msg.Text),
Bytes: bs,
return tgbotapi.NewVoice(msg.Chat.ID, tgbotapi.FileReader{
Name: fmt.Sprintf("%s.ogg", msg.Text),
Reader: bytes.NewBuffer(bs),
})
},
)
Expand Down
17 changes: 13 additions & 4 deletions internal/telegram/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,14 @@ func (b *Bot) ServeBotUpdateMessage() error {
}
pluginHub := hub.NewTGPluginHub(update.Message)
msg := pluginHub.Do(update.Message)
if msgConf, ok := msg.(tgbotapi.MessageConfig); ok {
switch msgConf := msg.(type) {
case tgbotapi.VoiceConfig:
msgConf.ReplyToMessageID = update.Message.MessageID
logrus.Debugf("[raw msg][sent]:%#v\n", msg)
if _, err := b.bot.Send(msg); err != nil {
logrus.Errorf("send msg failed:%q", err)
}
case tgbotapi.MessageConfig:
if isMsgBadRequest(msgConf) {
continue
}
Expand All @@ -83,9 +90,11 @@ func (b *Bot) ServeBotUpdateMessage() error {
continue
}
msgConf.ReplyToMessageID = update.Message.MessageID
b.bot.Send(msgConf)
} else {
b.bot.Send(msg)
if _, err := b.bot.Send(msgConf); err != nil {
logrus.Errorf("send msg failed:%q", err)
}
default:
logrus.Errorf("unknown msg type:%T", msgConf)
}
}
return nil
Expand Down
21 changes: 20 additions & 1 deletion internal/tts/eleven_lab.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
Expand All @@ -17,12 +18,19 @@ type Client struct {
model string
}

type BadResponse struct {
Detail struct {
Status string `json:"status"`
Message string `json:"message"`
} `json:"detail"`
}

func NewElevenLabClient() *Client {
return &Client{
domain: "https://api.elevenlabs.io/v1/text-to-speech/",
voiceID: "21m00Tcm4TlvDq8ikWAM",
apiKey: os.Getenv("ELEVEN_LAB_API_KEY"),
model: "eleven_monolingual_v2",
model: "eleven_monolingual_v1",
}
}

Expand All @@ -46,6 +54,9 @@ func (c *Client) ToSpeech(ctx context.Context, text string) ([]byte, error) {
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("xi-api-key", c.apiKey)

resp, err := http.DefaultClient.Do(req.WithContext(ctx))
if err != nil {
return nil, err
Expand All @@ -57,5 +68,13 @@ func (c *Client) ToSpeech(ctx context.Context, text string) ([]byte, error) {
return nil, err
}

if resp.StatusCode != http.StatusOK {
details := &BadResponse{}
if err := json.NewDecoder(bytes.NewBuffer(respBody)).Decode(&details); err != nil {
return nil, err
}
return nil, fmt.Errorf("[11lab] bad response: %s", details.Detail.Message)
}

return respBody, nil
}

0 comments on commit e2cef62

Please sign in to comment.