Skip to content

Commit

Permalink
provider refactor and discord added
Browse files Browse the repository at this point in the history
  • Loading branch information
mrasif committed Sep 23, 2024
1 parent 4c8f97c commit 72da41f
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 68 deletions.
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<!-- ![GitHub pull requests](https://img.shields.io/github/issues-pr/tech-thinker/chatz) -->
<!-- ![Coverage](https://img.shields.io/codecov/c/github/tech-thinker/chatz) -->

`chatz` is a versatile messaging app designed to send notifications to Google Chat, Slack, and Telegram. It provides a unified interface to automate messaging for various use cases, such as:
*Chatz* is an open-source application designed to send messages to popular communication platforms like Google Chat, Slack, Discord and Telegram. Whether you're monitoring server health, tracking job completions, or needing a quick notification tool, Chatz offers a unified way to communicate with your teams and devices, such as:

- Event Notifications: Automatically notify users of events occurring in background jobs, system processes, or application workflows.
- Alerts : Integrate with script to alert teams of system status changes, errors, or other critical updates.
Expand Down Expand Up @@ -50,20 +50,20 @@ chatz.exe
```ini
[default]
PROVIDER=slack
SLACK_TOKEN=<token>
TOKEN=<token>
CHANNEL_ID=<one-channel-id>

[another]
PROVIDER=slack
SLACK_TOKEN=<token>
TOKEN=<token>
CHANNEL_ID=<another-channel-id>
```

- Config for slack provider
```ini
[default]
PROVIDER=slack
SLACK_TOKEN=<token>
TOKEN=<token>
CHANNEL_ID=<channel-id>
```

Expand All @@ -78,8 +78,15 @@ WEB_HOOK_URL=<webhook-url>
```ini
[default]
PROVIDER=telegram
TELEGRAM_BOT_TOKEN=<bot-token>
TELEGRAM_CHAT_ID=<chat-id>
TOKEN=<bot-token>
CHAT_ID=<chat-id>
```

- Config for discord provider
```ini
[default]
PROVIDER=discord
WEB_HOOK_URL=<webhook-url>
```

## Usage
Expand Down
18 changes: 11 additions & 7 deletions chatz.ini
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
[default]
PROVIDER=slack
SLACK_TOKEN=
CHANNEL_ID=
TOKEN=<bot-token>
CHANNEL_ID=<channel-id>

[slack]
PROVIDER=slack
SLACK_TOKEN=
CHANNEL_ID=
TOKEN=<bot-token>
CHANNEL_ID=<channel-id>

[google]
PROVIDER=google
WEB_HOOK_URL=
WEB_HOOK_URL=<webhook-url>

[telegram]
PROVIDER=telegram
TELEGRAM_BOT_TOKEN=
TELEGRAM_CHAT_ID=
TOKEN=<bot-token>
CHAT_ID=<chat-id>

[discord]
PROVIDER=discord
WEB_HOOK_URL=<webhook-url>
9 changes: 4 additions & 5 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package config
// Environment
type Config struct {
Provider string
SlackToken string
SlackChannelId string
GoogleWebHookURL string
TelegramBotToken string
TelegramChatId string
WebHookURL string
Token string
ChannelId string
ChatId string
}
8 changes: 8 additions & 0 deletions constants/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package constants

const (
PROVIDER_SLACK="slack"
PROVIDER_DISCORD="discord"
PROVIDER_TELEGRAM="telegram"
PROVIDER_GOOGLE="google"
)
25 changes: 14 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import (
"fmt"
"os"

"github.com/tech-thinker/chatz/agents"
"github.com/tech-thinker/chatz/constants"
"github.com/tech-thinker/chatz/providers"
"github.com/tech-thinker/chatz/utils"
"github.com/urfave/cli/v2"
)
Expand Down Expand Up @@ -79,27 +80,29 @@ func main() {
if err!=nil {
return nil
}
var agent agents.Agent
var provider providers.Provider
switch env.Provider {
case "slack":
agent = agents.NewSlackAgent(env)
case "google":
agent = agents.NewGoogleAgent(env)
case "telegram":
agent = agents.NewTelegramAgent(env)
case constants.PROVIDER_SLACK:
provider = providers.NewSlackProvider(env)
case constants.PROVIDER_GOOGLE:
provider = providers.NewGoogleProvider(env)
case constants.PROVIDER_TELEGRAM:
provider = providers.NewTelegramProvider(env)
case constants.PROVIDER_DISCORD:
provider = providers.NewDiscordProvider(env)
default:
fmt.Println("No valid provider. Please choose from [slack, google, telegram].")
fmt.Println("No valid provider. Please choose from [slack, google, telegram, discord].")
return nil
}

if len(threadId) > 0 {
res, _ := agent.Reply(threadId, message)
res, _ := provider.Reply(threadId, message)
if output {
fmt.Println(res)
}
return nil
}
res, _ := agent.Post(message)
res, _ := provider.Post(message)
if output {
fmt.Println(res)
}
Expand Down
4 changes: 2 additions & 2 deletions agents/agent.go → providers/agent.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package agents
package providers

type Agent interface {
type Provider interface {
Post(message string) (interface{}, error)
Reply(threadId string, message string) (interface{}, error)
}
Expand Down
49 changes: 49 additions & 0 deletions providers/discord.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package providers

import (
"errors"
"fmt"
"io"
"net/http"
"strings"

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

type discordProvider struct {
config *config.Config
}

func (agent *discordProvider) Post(message string) (interface{}, error) {
url := agent.config.WebHookURL

payloadStr := fmt.Sprintf(
`{"content": "%s"}`,
message,
)

payload := strings.NewReader(payloadStr)

req, _ := http.NewRequest("POST", url, payload)

req.Header.Add("Content-Type", "application/json")
req.Header.Add("User-Agent", "tech-thinker/chatz")

res, err := http.DefaultClient.Do(req)
if err!=nil {
return nil, err
}

defer res.Body.Close()
body, err := io.ReadAll(res.Body)
return string(body), err
}

func (agent *discordProvider) Reply(threadId string, message string) (interface{}, error) {
fmt.Println("Reply to discord not supported yet.")
return nil, errors.New("Reply to discord not supported yet.")
}

func NewDiscordProvider(config *config.Config) Provider {
return &discordProvider{config: config}
}
16 changes: 8 additions & 8 deletions agents/google.go → providers/google.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package agents
package providers

import (
"fmt"
Expand All @@ -9,12 +9,12 @@ import (
"github.com/tech-thinker/chatz/config"
)

type googleAgent struct {
type googleProvider struct {
config *config.Config
}

func (agent *googleAgent) Post(message string) (interface{}, error) {
url := agent.config.GoogleWebHookURL
func (agent *googleProvider) Post(message string) (interface{}, error) {
url := agent.config.WebHookURL

payloadStr := fmt.Sprintf(
`{"text": "%s"}`,
Expand All @@ -38,8 +38,8 @@ func (agent *googleAgent) Post(message string) (interface{}, error) {
return string(body), err
}

func (agent *googleAgent) Reply(threadId string, message string) (interface{}, error) {
url := fmt.Sprintf("%s&messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD",agent.config.GoogleWebHookURL)
func (agent *googleProvider) Reply(threadId string, message string) (interface{}, error) {
url := fmt.Sprintf("%s&messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD",agent.config.WebHookURL)

payloadStr := fmt.Sprintf(
`{"text": "%s", "thread": {"name": "%s"}}`,
Expand All @@ -63,6 +63,6 @@ func (agent *googleAgent) Reply(threadId string, message string) (interface{}, e
return string(body), err
}

func NewGoogleAgent(config *config.Config) Agent {
return &googleAgent{config: config}
func NewGoogleProvider(config *config.Config) Provider {
return &googleProvider{config: config}
}
20 changes: 10 additions & 10 deletions agents/slack.go → providers/slack.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package agents
package providers

import (
"fmt"
Expand All @@ -9,16 +9,16 @@ import (
"github.com/tech-thinker/chatz/config"
)

type slackAgent struct {
type slackProvider struct {
config *config.Config
}

func (agent *slackAgent) Post(message string) (interface{}, error) {
func (agent *slackProvider) Post(message string) (interface{}, error) {
url := "https://slack.com/api/chat.postMessage"

payloadStr := fmt.Sprintf(
`{"channel": "%s","text": "%s"}`,
agent.config.SlackChannelId, message,
agent.config.ChannelId, message,
)

payload := strings.NewReader(payloadStr)
Expand All @@ -27,7 +27,7 @@ func (agent *slackAgent) Post(message string) (interface{}, error) {

req.Header.Add("Content-Type", "application/json")
req.Header.Add("User-Agent", "tech-thinker/chatz")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", agent.config.SlackToken))
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", agent.config.Token))

res, err := http.DefaultClient.Do(req)
if err!=nil {
Expand All @@ -39,12 +39,12 @@ func (agent *slackAgent) Post(message string) (interface{}, error) {
return string(body), err
}

func (agent *slackAgent) Reply(threadId string, message string) (interface{}, error) {
func (agent *slackProvider) Reply(threadId string, message string) (interface{}, error) {
url := "https://slack.com/api/chat.postMessage"

payloadStr := fmt.Sprintf(
`{"channel": "%s", "text": "%s", "thread_ts": "%s"}`,
agent.config.SlackChannelId, message, threadId,
agent.config.ChannelId, message, threadId,
)

payload := strings.NewReader(payloadStr)
Expand All @@ -53,7 +53,7 @@ func (agent *slackAgent) Reply(threadId string, message string) (interface{}, er

req.Header.Add("Content-Type", "application/json")
req.Header.Add("User-Agent", "tech-thinker/chatz")
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", agent.config.SlackToken))
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", agent.config.Token))

res, err := http.DefaultClient.Do(req)
if err!=nil {
Expand All @@ -65,6 +65,6 @@ func (agent *slackAgent) Reply(threadId string, message string) (interface{}, er
return string(body), err
}

func NewSlackAgent(config *config.Config) Agent {
return &slackAgent{config: config}
func NewSlackProvider(config *config.Config) Provider {
return &slackProvider{config: config}
}
20 changes: 10 additions & 10 deletions agents/telegram.go → providers/telegram.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package agents
package providers

import (
"fmt"
Expand All @@ -9,19 +9,19 @@ import (
"github.com/tech-thinker/chatz/config"
)

type telegramAgent struct {
type telegramProvider struct {
config *config.Config
}

func (agent *telegramAgent) Post(message string) (interface{}, error) {
func (agent *telegramProvider) Post(message string) (interface{}, error) {
url := fmt.Sprintf(
`https://api.telegram.org/bot%s/sendMessage`,
agent.config.TelegramBotToken,
agent.config.Token,
)

payloadStr := fmt.Sprintf(
`{"chat_id": "%s","text": "%s"}`,
agent.config.TelegramChatId, message,
agent.config.ChatId, message,
)

payload := strings.NewReader(payloadStr)
Expand All @@ -41,15 +41,15 @@ func (agent *telegramAgent) Post(message string) (interface{}, error) {
return string(body), err
}

func (agent *telegramAgent) Reply(threadId string, message string) (interface{}, error) {
func (agent *telegramProvider) Reply(threadId string, message string) (interface{}, error) {
url := fmt.Sprintf(
`https://api.telegram.org/bot%s/sendMessage`,
agent.config.TelegramBotToken,
agent.config.Token,
)

payloadStr := fmt.Sprintf(
`{"chat_id": "%s", "text": "%s", "reply_to_message_id": "%s"}`,
agent.config.TelegramChatId, message, threadId,
agent.config.ChatId, message, threadId,
)

payload := strings.NewReader(payloadStr)
Expand All @@ -69,6 +69,6 @@ func (agent *telegramAgent) Reply(threadId string, message string) (interface{},
return string(body), err
}

func NewTelegramAgent(config *config.Config) Agent {
return &telegramAgent{config: config}
func NewTelegramProvider(config *config.Config) Provider {
return &telegramProvider{config: config}
}
Loading

0 comments on commit 72da41f

Please sign in to comment.