Skip to content
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
16 changes: 9 additions & 7 deletions constants/common.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package constants

type ProviderType string

const (
PROVIDER_SLACK = "slack"
PROVIDER_DISCORD = "discord"
PROVIDER_TELEGRAM = "telegram"
PROVIDER_GOOGLE = "google"
PROVIDER_REDIS = "redis"
PROVIDER_SMTP = "smtp"
PROVIDER_GOTIFY = "gotify"
PROVIDER_SLACK ProviderType = "slack"
PROVIDER_DISCORD ProviderType = "discord"
PROVIDER_TELEGRAM ProviderType = "telegram"
PROVIDER_GOOGLE ProviderType = "google"
PROVIDER_REDIS ProviderType = "redis"
PROVIDER_SMTP ProviderType = "smtp"
PROVIDER_GOTIFY ProviderType = "gotify"
)
34 changes: 15 additions & 19 deletions providers/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,25 @@ import (
"github.com/tech-thinker/chatz/models"
)

var providerRegistry = make(map[constants.ProviderType]Provider)

type Provider interface {
setup(env *config.Config) error
Post(message string, option models.Option) (any, error)
Reply(threadId string, message string, option models.Option) (any, error)
}

func NewProvider(env *config.Config) (Provider, error) {
switch env.Provider {
case constants.PROVIDER_SLACK:
return &SlackProvider{config: env}, nil
case constants.PROVIDER_GOOGLE:
return &GoogleProvider{config: env}, nil
case constants.PROVIDER_TELEGRAM:
return &TelegramProvider{config: env}, nil
case constants.PROVIDER_DISCORD:
return &DiscordProvider{config: env}, nil
case constants.PROVIDER_REDIS:
return &RedisProvider{config: env}, nil
case constants.PROVIDER_SMTP:
return &SMTPProvider{config: env}, nil
case constants.PROVIDER_GOTIFY:
return &GotifyProvider{config: env}, nil
default:
return nil, errors.New("Invalid provider config in ~/.chatz.ini")
}
func RegisterProvider(providerType constants.ProviderType, provider Provider) {
providerRegistry[providerType] = provider
}

func NewProvider(config *config.Config) (Provider, error) {
if provider, ok := providerRegistry[constants.ProviderType(config.Provider)]; ok {
err := provider.setup(config)
if err != nil {
return nil, err
}
return provider, nil
}
return nil, errors.New("Invalid provider config in ~/.chatz.ini")
}
10 changes: 10 additions & 0 deletions providers/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/tech-thinker/chatz/config"
"github.com/tech-thinker/chatz/constants"
"github.com/tech-thinker/chatz/models"
)

Expand Down Expand Up @@ -44,3 +45,12 @@ func (agent *DiscordProvider) Reply(threadId string, message string, option mode
fmt.Println("Reply to discord not supported yet.")
return nil, errors.New("Reply to discord not supported yet.")
}

func (agent *DiscordProvider) setup(env *config.Config) error {
agent.config = env
return nil
}

func init() {
RegisterProvider(constants.PROVIDER_DISCORD, new(DiscordProvider))
}
10 changes: 10 additions & 0 deletions providers/google.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/tech-thinker/chatz/config"
"github.com/tech-thinker/chatz/constants"
"github.com/tech-thinker/chatz/models"
)

Expand Down Expand Up @@ -63,3 +64,12 @@ func (agent *GoogleProvider) Reply(threadId string, message string, option model
body, err := io.ReadAll(res.Body)
return string(body), err
}

func (agent *GoogleProvider) setup(env *config.Config) error {
agent.config = env
return nil
}

func init() {
RegisterProvider(constants.PROVIDER_GOOGLE, new(GoogleProvider))
}
10 changes: 10 additions & 0 deletions providers/gotify.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

"github.com/tech-thinker/chatz/config"
"github.com/tech-thinker/chatz/constants"
"github.com/tech-thinker/chatz/models"
"github.com/tech-thinker/chatz/utils"
)
Expand Down Expand Up @@ -64,3 +65,12 @@ func (agent *GotifyProvider) Reply(threadId string, message string, option model
fmt.Println("Reply to gotify not supported yet.")
return nil, errors.New("reply to gotify not supported yet")
}

func (agent *GotifyProvider) setup(env *config.Config) error {
agent.config = env
return nil
}

func init() {
RegisterProvider(constants.PROVIDER_GOTIFY, new(GotifyProvider))
}
10 changes: 10 additions & 0 deletions providers/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/redis/go-redis/v9"
"github.com/tech-thinker/chatz/config"
"github.com/tech-thinker/chatz/constants"
"github.com/tech-thinker/chatz/models"
)

Expand Down Expand Up @@ -40,3 +41,12 @@ func (agent *RedisProvider) Reply(threadId string, message string, option models
fmt.Println("Reply to redis not supported yet.")
return nil, errors.New("Reply to redis not supported yet.")
}

func (agent *RedisProvider) setup(env *config.Config) error {
agent.config = env
return nil
}

func init() {
RegisterProvider(constants.PROVIDER_REDIS, new(RedisProvider))
}
10 changes: 10 additions & 0 deletions providers/slack.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/tech-thinker/chatz/config"
"github.com/tech-thinker/chatz/constants"
"github.com/tech-thinker/chatz/models"
)

Expand Down Expand Up @@ -65,3 +66,12 @@ func (agent *SlackProvider) Reply(threadId string, message string, option models
body, err := io.ReadAll(res.Body)
return string(body), err
}

func (agent *SlackProvider) setup(env *config.Config) error {
agent.config = env
return nil
}

func init() {
RegisterProvider(constants.PROVIDER_SLACK, new(SlackProvider))
}
10 changes: 10 additions & 0 deletions providers/smtp.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"github.com/tech-thinker/chatz/config"
"github.com/tech-thinker/chatz/constants"
"github.com/tech-thinker/chatz/models"
"github.com/tech-thinker/chatz/utils"
)
Expand Down Expand Up @@ -145,3 +146,12 @@ func (agent *SMTPProvider) Reply(threadId string, message string, option models.
fmt.Println("Reply to SMTP not supported yet.")
return nil, errors.New("Reply to SMTP not supported yet.")
}

func (agent *SMTPProvider) setup(env *config.Config) error {
agent.config = env
return nil
}

func init() {
RegisterProvider(constants.PROVIDER_SMTP, new(SMTPProvider))
}
10 changes: 10 additions & 0 deletions providers/telegram.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/tech-thinker/chatz/config"
"github.com/tech-thinker/chatz/constants"
"github.com/tech-thinker/chatz/models"
)

Expand Down Expand Up @@ -69,3 +70,12 @@ func (agent *TelegramProvider) Reply(threadId string, message string, option mod
body, err := io.ReadAll(res.Body)
return string(body), err
}

func (agent *TelegramProvider) setup(env *config.Config) error {
agent.config = env
return nil
}

func init() {
RegisterProvider(constants.PROVIDER_TELEGRAM, new(TelegramProvider))
}