Skip to content

Commit

Permalink
all: rewrite astral
Browse files Browse the repository at this point in the history
* Migrates deployment to fly.io
* Astral now opt-in AI features
* Removes TL.net StarCraft2 feeds (also Telegram/Discord Channel Push). 🙏 R.I.P StarCraft2
* bump Go to 1.21
* bump telegram bot to support v5 APIs
* Refactor PluginHub
* 再见啦,7年前的自己

Signed-off-by: scbizu <[email protected]>
  • Loading branch information
scbizu committed Oct 8, 2023
1 parent fb95a36 commit b88fe03
Show file tree
Hide file tree
Showing 22 changed files with 250 additions and 458 deletions.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# flyctl launch added from .gitignore
**/.env
fly.toml
8 changes: 5 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@

FROM golang:1.17 AS BUILDER
FROM golang:1.21 AS BUILDER

WORKDIR /project/Astral

ADD . /project/Astral

RUN export GO11MODULE="on" && go build -o astral .
RUN go build -o astral .

FROM golang:1.17
FROM golang:1.21

WORKDIR /Astral

COPY --from=BUILDER /project/Astral/astral /Astral/astral

EXPOSE 8443

ENTRYPOINT [ "/Astral/astral" ]
49 changes: 30 additions & 19 deletions cmd/launch.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,40 @@
package cmd

import (
"github.com/scbizu/Astral/internal/discord"
"github.com/scbizu/Astral/internal/telegram"
"github.com/scbizu/Astral/service/push"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

// AstralCmd defines astral main cmd
var AstralCmd = &cobra.Command{
Use: "astral",
Short: "Astral Bot & Push Service",
Short: "Astral Bot",
Run: func(_ *cobra.Command, _ []string) {
bot, err := telegram.NewBot(true)
if err != nil {
logrus.Fatal(err)
return
}

if err := bot.ServeBotUpdateMessage(); err != nil {
logrus.Fatal(err)
return
}
},
}

// ServiceCmd defines astral service cmd
var ServiceCmd = &cobra.Command{
Use: "service",
Short: "Astral service cmd(command,autopush)",
Short: "Astral is a self-host bot of @scnace",
}

// CommandService defines astral bot command service
var CommandService = &cobra.Command{
Use: "Command",
Short: "Astral command service",
// TGService defines astral bot command service
var TGService = &cobra.Command{
Use: "telegram",
Short: "a telegram bot",
Run: func(cmd *cobra.Command, args []string) {
bot, err := telegram.NewBot(false)
if err != nil {
Expand All @@ -34,27 +46,26 @@ var CommandService = &cobra.Command{
logrus.Fatal(err)
return
}

return
},
}

// CNSC2EventInfoService defines autopush CNSC2Event channle message service
var CNSC2EventInfoService = &cobra.Command{
Use: "SC2EventInfo",
Short: "CNSC2 Event info service",
Run: func(cmd *cobra.Command, args []string) {
s := push.NewPushService()
if err := s.ServePushSC2Event(); err != nil {
var DiscordService = &cobra.Command{
Use: "discord",
Short: "a discord bot",
RunE: func(cmd *cobra.Command, args []string) error {
_, err := discord.NewBot()
if err != nil {
logrus.Fatal(err)
return err
}
return nil
},
}

//Execute exec astral
// Execute exec astral
func Execute() (err error) {
ServiceCmd.AddCommand(CommandService)
ServiceCmd.AddCommand(CNSC2EventInfoService)
ServiceCmd.AddCommand(TGService)
ServiceCmd.AddCommand(DiscordService)
AstralCmd.AddCommand(ServiceCmd)
return AstralCmd.Execute()
}
18 changes: 18 additions & 0 deletions fly.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# fly.toml app configuration file generated for astral-on-telegram on 2023-10-08T23:30:09+08:00
#
# See https://fly.io/docs/reference/configuration/ for information about how to use this file.
#

app = "astral-on-telegram"
primary_region = "hkg"

[build]

[http_service]
internal_port = 8443
force_https = true
auto_stop_machines = true
auto_start_machines = true
min_machines_running = 0
processes = ["app"]

9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module github.com/scbizu/Astral

go 1.21

require (
github.com/PuerkitoBio/goquery v1.4.1
github.com/andybalholm/cascadia v1.0.0 // indirect
github.com/bwmarrin/discordgo v0.23.2
github.com/go-telegram-bot-api/telegram-bot-api v4.6.1+incompatible
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/robfig/cron v0.0.0-20180505203441-b41be1df6967
Expand All @@ -13,19 +14,19 @@ require (
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.2 // indirect
github.com/stretchr/testify v1.3.0 // indirect
github.com/technoweenie/multipartstreamer v1.0.1 // indirect
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 // indirect
gopkg.in/airbrake/gobrake.v2 v2.0.9 // indirect
gopkg.in/gemnasium/logrus-airbrake-hook.v2 v2.1.2 // indirect
)

replace github.com/bwmarrin/discordgo v0.23.2 => github.com/bwmarrin/discordgo v0.23.3-0.20211010150959-f0b7e81468f7

require github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1

require (
github.com/gorilla/websocket v1.4.2 // indirect
github.com/rakyll/openai-go v1.0.9 // indirect
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b // indirect
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 // indirect
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 // indirect
)

go 1.17
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/set v0.2.1 h1:nn2CaJyknWE/6txyUDGwysr3G5QC6xWB/PtVjPBbeaA=
github.com/fatih/set v0.2.1/go.mod h1:+RKtMCH+favT2+3YecHGxcc0b4KyVWA1QWWJUs4E0CI=
github.com/go-telegram-bot-api/telegram-bot-api v4.6.1+incompatible h1:KWd4+1Z6hk4FqQPCvPBSMCTCy0vjTVJmq0JdkflLxy8=
github.com/go-telegram-bot-api/telegram-bot-api v4.6.1+incompatible/go.mod h1:qf9acutJ8cwBUhm1bqgz6Bei9/C/c93FPDljKWwsOgM=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
Expand All @@ -18,6 +18,8 @@ github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaR
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rakyll/openai-go v1.0.9 h1:oxaSEPbmYT2RCEG3D9id+Fj7qEi+FnHV7qs/qRSSwhM=
github.com/rakyll/openai-go v1.0.9/go.mod h1:hQpeaAVYbRXtWjFUew82LmbEIASmjihU3aI5NfJdqxA=
github.com/robfig/cron v0.0.0-20180505203441-b41be1df6967 h1:x7xEyJDP7Hv3LVgvWhzioQqbC/KtuUhTigKlH/8ehhE=
github.com/robfig/cron v0.0.0-20180505203441-b41be1df6967/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
github.com/scylladb/go-set v1.0.2 h1:SkvlMCKhP0wyyct6j+0IHJkBkSZL+TDzZ4E7f7BCcRE=
Expand All @@ -31,8 +33,6 @@ github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnIn
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/technoweenie/multipartstreamer v1.0.1 h1:XRztA5MXiR1TIRHxH2uNxXxaIkKQDeX7m2XsSOlQEnM=
github.com/technoweenie/multipartstreamer v1.0.1/go.mod h1:jNVxdtShOxzAsukZwTSw6MDx5eUJoiEBsSvzDU9uzog=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b h1:7mWr3k41Qtv8XlltBkDkl8LoP3mpSgBW8BUoxtEdbXg=
golang.org/x/crypto v0.0.0-20210421170649-83a5a9bb288b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down
5 changes: 5 additions & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import (
"os"
)

var (
OpenAIAPIKEy = os.Getenv("OPENAI_API_KEY")
OpenAIAPIEndpoint = os.Getenv("OPENAI_COMPLETION_ENDPOINT")
)

const (
DiscordCNSC2ChannelID = "586225314078654484"
discordBotClientIDKey = "ASTRAL_DISCORD_CLIENT_ID"
Expand Down
24 changes: 24 additions & 0 deletions internal/openai/openai.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package openai

import (
"sync"

"github.com/rakyll/openai-go"
"github.com/rakyll/openai-go/chat"
"github.com/scbizu/Astral/internal/config"
)

var (
openAIClientOnce sync.Once
openAIChatClient *chat.Client
)

func GetOpenAIClient() *chat.Client {
openAIClientOnce.Do(func() {
openAISession := openai.NewSession(config.OpenAIAPIKEy)
c := chat.NewClient(openAISession, "")
c.CreateCompletionEndpoint = config.OpenAIAPIEndpoint
openAIChatClient = c
})
return openAIChatClient
}
48 changes: 48 additions & 0 deletions internal/plugin/ai/ai.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package ai

import (
"bytes"
"context"
"fmt"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/rakyll/openai-go/chat"
"github.com/scbizu/Astral/internal/openai"
"github.com/scbizu/Astral/internal/plugin"
"github.com/scbizu/Astral/internal/telegram/command"
)

var _ plugin.IPlugin = (*AICommands)(nil)

type AICommands struct{}

func (ai *AICommands) Name() command.CommanderName {
return command.CommandAIChat
}

func (ai *AICommands) Enable() bool {
return true
}

func (ai *AICommands) Process(msg *tgbotapi.Message) tgbotapi.MessageConfig {
cmd := command.NewCommand(
command.CommandAIChat,
"chat with openAI",
func(msg *tgbotapi.Message) tgbotapi.MessageConfig {
resp, err := openai.GetOpenAIClient().CreateCompletion(context.TODO(), &chat.CreateCompletionParams{
Messages: []*chat.Message{
{Role: "user", Content: msg.Text},
},
})
if err != nil {
return tgbotapi.NewMessage(msg.Chat.ID, err.Error())
}
msgBuffer := bytes.NewBuffer(nil)
for _, choice := range resp.Choices {
fmt.Fprintf(msgBuffer, "%s\n", choice.Message.Content)
}
return tgbotapi.NewMessage(msg.Chat.ID, msgBuffer.String())
},
)
return cmd.Do(msg)
}
42 changes: 28 additions & 14 deletions internal/plugin/hub/hub.go
Original file line number Diff line number Diff line change
@@ -1,34 +1,44 @@
package hub

import (
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
plugin "github.com/scbizu/Astral/internal/plugin"
"github.com/scbizu/Astral/internal/plugin/ai"
"github.com/scbizu/Astral/internal/plugin/py"
"github.com/scbizu/Astral/internal/plugin/sayhi"
"github.com/scbizu/Astral/internal/plugin/today-anime"
anime "github.com/scbizu/Astral/internal/plugin/today-anime"
"github.com/sirupsen/logrus"
)

// TGPluginHub defines the telegram plugin hub
type TGPluginHub struct {
msg *tgbotapi.Message
plugins []*plugin.TGPlugin
}

var plugins = []plugin.IPlugin{
&sayhi.Handler{},
&anime.Handler{},
&ai.AICommands{},
// py plugin must be put in the end
&py.Handler{},
}

// NewTGPluginHub init empty plugin hub
func NewTGPluginHub(msg *tgbotapi.Message) *TGPluginHub {
hub := &TGPluginHub{
plugins: []*plugin.TGPlugin{},
msg: msg,
}
hub.Init(msg)
hub.Init()
return hub
}

// Init regist all command
func (ph *TGPluginHub) Init(msg *tgbotapi.Message) {
ph.AddPlugin(sayhi.Sayhi(msg))
ph.AddPlugin(anime.Anime(msg))
// py plugin must be put in the end
defer ph.AddPlugin(py.PY(msg))
func (ph *TGPluginHub) Init() {
for _, p := range plugins {
ph.AddTGPlugin(p)
}
}

// GetEnabledTelegramPlugins get the all enable plugins
Expand All @@ -41,14 +51,18 @@ func (ph *TGPluginHub) GetEnabledTelegramPlugins() (activePlugins []*plugin.TGPl
return
}

// AddPlugin adds the plugin
func (ph *TGPluginHub) AddPlugin(p *plugin.TGPlugin) {
ph.plugins = append(ph.plugins, p)
func (ph *TGPluginHub) AddTGPlugin(
p plugin.IPlugin,
) {
ph.plugins = append(ph.plugins,
plugin.NewTGPlugin(p.Name(),
p.Process(ph.msg),
),
)
}

// RegistTGEnabledPlugins regists telegram plugin
func (ph *TGPluginHub) RegistTGEnabledPlugins(rawmsg *tgbotapi.Message) (msg tgbotapi.MessageConfig) {

// Do iters telegram plugin
func (ph *TGPluginHub) Do(rawmsg *tgbotapi.Message) (msg tgbotapi.MessageConfig) {
for _, p := range ph.GetEnabledTelegramPlugins() {
msg, _ = p.Run(rawmsg)
logrus.Infof("[chatID:%d,msg:%s]", msg.ChatID, msg.Text)
Expand Down
Loading

0 comments on commit b88fe03

Please sign in to comment.