Skip to content

Commit e10864d

Browse files
committed
slack and google provider added
1 parent 291ca90 commit e10864d

File tree

14 files changed

+601
-0
lines changed

14 files changed

+601
-0
lines changed

.github/workflows/go-tests.yaml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: Go Tests
2+
3+
on:
4+
push:
5+
branches: [ 'main', 'develop' ]
6+
pull_request:
7+
branches: [ '*' ]
8+
9+
jobs:
10+
tests:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v2
14+
- name: Set up Go
15+
uses: actions/setup-go@v2
16+
with:
17+
go-version: 1.22
18+
- name: Build
19+
run: make build
20+
- name: Test
21+
run: make test
22+
- name: Upload coverage to Codecov
23+
uses: codecov/codecov-action@v2

.github/workflows/release.yaml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: Build and Release
2+
3+
on:
4+
push:
5+
tags:
6+
- 'v*.*.*'
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
12+
strategy:
13+
matrix:
14+
go-version: [1.22]
15+
16+
steps:
17+
- name: Checkout code
18+
uses: actions/checkout@v3
19+
20+
- name: Set up Go
21+
uses: actions/setup-go@v4
22+
with:
23+
go-version: ${{ matrix.go-version }}
24+
25+
- name: Install dependencies
26+
run: make install
27+
28+
- name: Build
29+
run: make build-all
30+
31+
- name: Archive build artifacts
32+
uses: actions/upload-artifact@v3
33+
with:
34+
name: build
35+
path: build
36+
if-no-files-found: error
37+
38+
release:
39+
needs: build
40+
runs-on: ubuntu-latest
41+
steps:
42+
- name: Checkout code
43+
uses: actions/checkout@v3
44+
45+
- name: Download build artifacts
46+
uses: actions/download-artifact@v3
47+
with:
48+
name: build
49+
path: build
50+
51+
- name: Create GitHub Release
52+
uses: softprops/action-gh-release@v1
53+
with:
54+
files: ./build/*
55+
env:
56+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
build
22

33
chat
4+
coverage.out

Makefile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
install:
2+
go mod tidy
3+
4+
test:
5+
go test -v ./... -race -coverprofile=coverage.out -covermode=atomic
6+
7+
run: build
8+
./chat "This is test."
9+
10+
build:
11+
go build -o chat .
12+
13+
build-all:
14+
GOOS=linux GOARCH=amd64 go build -o build/chat-linux-amd64
15+
GOOS=linux GOARCH=arm64 go build -o build/chat-linux-arm64
16+
GOOS=linux GOARCH=arm go build -o build/chat-linux-arm
17+
GOOS=darwin GOARCH=amd64 go build -o build/chat-darwin-amd64
18+
GOOS=darwin GOARCH=arm64 go build -o build/chat-darwin-arm64
19+
GOOS=windows GOARCH=amd64 go build -o build/chat-windows-amd64.exe
20+
GOOS=windows GOARCH=386 go build -o build/chat-windows-i386.exe
21+
22+
clean:
23+
rm -rf chat build

agents/agent.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package agents
2+
3+
type Agent interface {
4+
Post(message string) (interface{}, error)
5+
Reply(threadId string, message string) (interface{}, error)
6+
}
7+

agents/google.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package agents
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"net/http"
7+
"strings"
8+
9+
"github.com/tech-thinker/chat/config"
10+
)
11+
12+
type googleAgent struct {
13+
config *config.Config
14+
}
15+
16+
func (agent *googleAgent) Post(message string) (interface{}, error) {
17+
url := agent.config.GoogleWebHookURL
18+
19+
payloadStr := fmt.Sprintf(
20+
`{"text": "%s"}`,
21+
message,
22+
)
23+
24+
payload := strings.NewReader(payloadStr)
25+
26+
req, _ := http.NewRequest("POST", url, payload)
27+
28+
req.Header.Add("Content-Type", "application/json")
29+
req.Header.Add("User-Agent", "tech-thinker/chat")
30+
31+
res, err := http.DefaultClient.Do(req)
32+
if err!=nil {
33+
return nil, err
34+
}
35+
36+
defer res.Body.Close()
37+
body, err := io.ReadAll(res.Body)
38+
return string(body), err
39+
}
40+
41+
func (agent *googleAgent) Reply(threadId string, message string) (interface{}, error) {
42+
url := fmt.Sprintf("%s&messageReplyOption=REPLY_MESSAGE_FALLBACK_TO_NEW_THREAD",agent.config.GoogleWebHookURL)
43+
44+
payloadStr := fmt.Sprintf(
45+
`{"text": "%s", "thread": {"name": "%s"}}`,
46+
message, threadId,
47+
)
48+
49+
payload := strings.NewReader(payloadStr)
50+
51+
req, _ := http.NewRequest("POST", url, payload)
52+
53+
req.Header.Add("Content-Type", "application/json")
54+
req.Header.Add("User-Agent", "tech-thinker/chat")
55+
56+
res, err := http.DefaultClient.Do(req)
57+
if err!=nil {
58+
return nil, err
59+
}
60+
61+
defer res.Body.Close()
62+
body, err := io.ReadAll(res.Body)
63+
return string(body), err
64+
}
65+
66+
func NewGoogleAgent(config *config.Config) Agent {
67+
return &googleAgent{config: config}
68+
}

agents/slack.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package agents
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"net/http"
7+
"strings"
8+
9+
"github.com/tech-thinker/chat/config"
10+
)
11+
12+
type slackAgent struct {
13+
config *config.Config
14+
}
15+
16+
func (agent *slackAgent) Post(message string) (interface{}, error) {
17+
url := "https://slack.com/api/chat.postMessage"
18+
19+
payloadStr := fmt.Sprintf(
20+
`{"channel": "%s","text": "%s"}`,
21+
agent.config.SlackChannelId, message,
22+
)
23+
24+
payload := strings.NewReader(payloadStr)
25+
26+
req, _ := http.NewRequest("POST", url, payload)
27+
28+
req.Header.Add("Content-Type", "application/json")
29+
req.Header.Add("User-Agent", "tech-thinker/chat")
30+
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", agent.config.SlackToken))
31+
32+
res, err := http.DefaultClient.Do(req)
33+
if err!=nil {
34+
return nil, err
35+
}
36+
37+
defer res.Body.Close()
38+
body, err := io.ReadAll(res.Body)
39+
return string(body), err
40+
}
41+
42+
func (agent *slackAgent) Reply(threadId string, message string) (interface{}, error) {
43+
url := "https://slack.com/api/chat.postMessage"
44+
45+
payloadStr := fmt.Sprintf(
46+
`{"channel": "%s", "text": "%s", "thread_ts": "%s"}`,
47+
agent.config.SlackChannelId, message, threadId,
48+
)
49+
50+
payload := strings.NewReader(payloadStr)
51+
52+
req, _ := http.NewRequest("POST", url, payload)
53+
54+
req.Header.Add("Content-Type", "application/json")
55+
req.Header.Add("User-Agent", "tech-thinker/chat")
56+
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", agent.config.SlackToken))
57+
58+
res, err := http.DefaultClient.Do(req)
59+
if err!=nil {
60+
return nil, err
61+
}
62+
63+
defer res.Body.Close()
64+
body, err := io.ReadAll(res.Body)
65+
return string(body), err
66+
}
67+
68+
func NewSlackAgent(config *config.Config) Agent {
69+
return &slackAgent{config: config}
70+
}

chat.ini

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[default]
2+
PROVIDER=slack
3+
SLACK_TOKEN=
4+
CHANNEL_ID=
5+
6+
[slack]
7+
PROVIDER=slack
8+
SLACK_TOKEN=
9+
CHANNEL_ID=
10+
11+
[google]
12+
PROVIDER=google
13+
WEB_HOOK_URL=
14+

config/config.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package config
2+
3+
// Environment
4+
type Config struct {
5+
Provider string
6+
SlackToken string
7+
SlackChannelId string
8+
GoogleWebHookURL string
9+
}

go.mod

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module github.com/tech-thinker/chat
2+
3+
go 1.23.0
4+
5+
require (
6+
github.com/spf13/viper v1.19.0
7+
github.com/urfave/cli/v2 v2.27.4
8+
)
9+
10+
require (
11+
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
12+
github.com/fsnotify/fsnotify v1.7.0 // indirect
13+
github.com/hashicorp/hcl v1.0.0 // indirect
14+
github.com/magiconair/properties v1.8.7 // indirect
15+
github.com/mitchellh/mapstructure v1.5.0 // indirect
16+
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
17+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
18+
github.com/sagikazarmark/locafero v0.4.0 // indirect
19+
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
20+
github.com/sourcegraph/conc v0.3.0 // indirect
21+
github.com/spf13/afero v1.11.0 // indirect
22+
github.com/spf13/cast v1.6.0 // indirect
23+
github.com/spf13/pflag v1.0.5 // indirect
24+
github.com/subosito/gotenv v1.6.0 // indirect
25+
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
26+
go.uber.org/atomic v1.9.0 // indirect
27+
go.uber.org/multierr v1.9.0 // indirect
28+
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
29+
golang.org/x/sys v0.18.0 // indirect
30+
golang.org/x/text v0.14.0 // indirect
31+
gopkg.in/ini.v1 v1.67.0 // indirect
32+
gopkg.in/yaml.v3 v3.0.1 // indirect
33+
)

0 commit comments

Comments
 (0)