Skip to content

Commit

Permalink
redis provider added
Browse files Browse the repository at this point in the history
  • Loading branch information
mrasif committed Sep 24, 2024
1 parent 8e165fb commit c7dbb6f
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 1 deletion.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ With chatz, you can streamline your notification processes across multiple platf
- Google Chat: [Read documentation](docs/google.md)
- Telegram: [Read documentation](docs/telegram.md)
- Discord: [Read documentation](docs/discord.md)
- Redis: [Read documentation](docs/redis.md)

## Installation
Download and install executable binary from GitHub releases page.
Expand Down Expand Up @@ -93,6 +94,14 @@ PROVIDER=discord
WEB_HOOK_URL=<webhook-url>
```

- Config for redis provider
```ini
[default]
PROVIDER=redis
CONNECTION_URL=<redis-connection-url>
CHANNEL_ID=<redis-publish-channel>
```

## Usage
- Send message using `default` profile
```sh
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ type Config struct {
Token string
ChannelId string
ChatId string
ConnectionURL string
}
1 change: 1 addition & 0 deletions constants/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ const (
PROVIDER_DISCORD="discord"
PROVIDER_TELEGRAM="telegram"
PROVIDER_GOOGLE="google"
PROVIDER_REDIS="redis"
)
22 changes: 22 additions & 0 deletions docs/redis.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## Introduction

Redis is an in-memory data structure store, often used as a database, cache, and message broker. One of its powerful features is the Publish/Subscribe (Pub/Sub) messaging paradigm, which allows messages to be sent between clients without a direct connection between them.

## What is Redis Pub/Sub?

Redis Pub/Sub is a messaging pattern that enables message broadcasting. In this model, publishers send messages to channels, and subscribers listen for messages on those channels. This decouples the sender (publisher) from the receivers (subscribers), allowing for a flexible and scalable messaging system.

### Key Concepts:

- **Publisher**: A client that sends messages to one or more channels.
- **Subscriber**: A client that listens for messages on specific channels.
- **Channel**: A named entity that subscribers can subscribe to in order to receive messages.

## Use Cases

Redis Pub/Sub is suitable for various applications, including:

- Real-time messaging systems (e.g., chat applications).
- Event-driven architectures where events trigger actions.
- Notifications and alerts in web applications.
- Live updates in collaborative applications (e.g., document editing).
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ require (
)

require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/redis/go-redis/v9 v9.6.1 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
Expand All @@ -25,6 +29,8 @@ github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/redis/go-redis/v9 v9.6.1 h1:HHDteefn6ZkTtY5fGUE8tj8uy85AHk6zP7CpzIAM0y4=
github.com/redis/go-redis/v9 v9.6.1/go.mod h1:0C0c6ycQsdpVNQpxb1njEQIqkx5UcsM8FJCQLgE9+RA=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func main() {

app := cli.NewApp()
app.Name = "chatz"
app.Description = "chatz is a versatile messaging app designed to send notifications to Google Chat, Slack, Discord and Telegram."
app.Description = "chatz is a versatile messaging app designed to send notifications to Google Chat, Slack, Discord, Telegram and Redis."
app.Flags = []cli.Flag {
&cli.BoolFlag{
Name: "output",
Expand Down
2 changes: 2 additions & 0 deletions providers/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ func NewProvider(env *config.Config) (Provider, error) {
return &TelegramProvider{config: env}, nil
case constants.PROVIDER_DISCORD:
return &DiscordProvider{config: env}, nil
case constants.PROVIDER_REDIS:
return &RedisProvider{config: env}, nil
default:
return nil, errors.New("Invalid provider config in ~/.chatz.ini")
}
Expand Down
42 changes: 42 additions & 0 deletions providers/redis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package providers

import (
"context"
"errors"
"fmt"

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

type RedisProvider struct {
config *config.Config
}

func (agent *RedisProvider) Post(message string) (interface{}, error) {
options, err := redis.ParseURL(agent.config.ConnectionURL)
if err != nil {
return nil, err
}

rdb := redis.NewClient(options)
ctx := context.Background()

_, err = rdb.Ping(ctx).Result()
if err != nil {
return nil, err
}

err = rdb.Publish(ctx, agent.config.ChannelId, message).Err()
if err != nil {
return nil, err
}

return "Published", nil
}

func (agent *RedisProvider) 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.")
}

2 changes: 2 additions & 0 deletions utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func LoadEnv(profile string) (*config.Config, error) {
channelId := viper.GetString(fmt.Sprintf("%s.CHANNEL_ID", profile))
webHookURL := viper.GetString(fmt.Sprintf("%s.WEB_HOOK_URL", profile))
chatId := viper.GetString(fmt.Sprintf("%s.CHAT_ID", profile))
connectionURL := viper.GetString(fmt.Sprintf("%s.CONNECTION_URL", profile))


var env config.Config
Expand All @@ -46,6 +47,7 @@ func LoadEnv(profile string) (*config.Config, error) {
env.Token = slackToken
env.ChannelId = channelId
env.ChatId = chatId
env.ConnectionURL = connectionURL

return &env, nil
}
Expand Down

0 comments on commit c7dbb6f

Please sign in to comment.