summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Kiryukhin <alexander@kiryukhin.su>2019-02-01 01:20:38 +0300
committerAlexander Kiryukhin <alexander@kiryukhin.su>2019-02-01 01:20:38 +0300
commit8c499a32b7bb614814da84bace3f3e924be92465 (patch)
tree3bc9d97a01fdb6e4bf219d5de81f1873232fe696
parentfc9aefc97d872ec8faed767a7b9e2a89ee8630f0 (diff)
fixes
-rw-r--r--channel.go39
-rw-r--r--main.go98
-rw-r--r--members.go19
-rw-r--r--message.go32
-rw-r--r--requests.go40
5 files changed, 146 insertions, 82 deletions
diff --git a/channel.go b/channel.go
new file mode 100644
index 0000000..95cdd7d
--- /dev/null
+++ b/channel.go
@@ -0,0 +1,39 @@
+package main
+
+import (
+ "encoding/json"
+ "errors"
+ "log"
+ "net/http"
+ "net/url"
+)
+
+func getUsers(token string, channel string) ([]string, error) {
+ u, _ := url.Parse("https://slack.com/api/channels.info")
+ u.Query().Set("token", token)
+ u.Query().Set("channel", channel)
+ req, err := http.NewRequest(http.MethodGet, u.String(), nil)
+ if err != nil {
+ return nil, err
+ }
+ req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
+ resp, err := getClient().Do(req)
+ if err != nil {
+ log.Println(err)
+ return nil, err
+ }
+ r := struct {
+ OK bool `json:"ok"`
+ Error string `json:"error"`
+ Channel struct {
+ Members []string `json:"members"`
+ } `json:"channel"`
+ }{}
+ if err := json.NewDecoder(resp.Body).Decode(&r); err != nil {
+ return nil, err
+ }
+ if !r.OK {
+ return nil, errors.New(r.Error)
+ }
+ return r.Channel.Members, nil
+}
diff --git a/main.go b/main.go
index e3d2228..9632cf0 100644
--- a/main.go
+++ b/main.go
@@ -1,92 +1,50 @@
package main
import (
- "bytes"
+ "crypto/tls"
"encoding/json"
- "errors"
+ "fmt"
"github.com/labstack/echo"
- "io/ioutil"
"log"
"net/http"
)
-type event struct {
- Payload string `form:"payload"`
-}
-
-type payload struct {
- Type string `json:"type" form:"type"`
- Token string `json:"token"`
- ActionTS string `json:"action_ts"`
-
- Team struct {
- ID string `json:"id"`
- Domain string `json:"domain"`
- }
- User struct {
- ID string `json:"id"`
- Name string `json:"name"`
- } `json:"user"`
- Channel struct {
- ID string `json:"id"`
- Name string `json:"name"`
- } `json:"channel"`
- CallbackID string `json:"callback_id"`
- TriggerID string `json:"trigger_id"`
- MessageTS string `json:"message_ts"`
- Message struct {
- ClientMsgID string `json:"client_msg_id"`
- Type string `json:"type"`
- Text string `json:"text"`
- User string `json:"user"`
- TS string `json:"ts"`
- } `json:"message"`
- ResponseUrl string `json:"response_url"`
-}
-
-type message struct {
- Text string `json:"text"`
- ResponseType string `json:"response_type"`
- Attachments []Attachment `json:"attachments"`
-}
-type Attachment struct {
- Color string `json:"color"`
- Text string `json:"text"`
-}
-
func main() {
+ //token := os.Getenv("TOKEN")
e := echo.New()
e.POST("/events-endpoint", func(c echo.Context) error {
- ev := &event{}
- c.Bind(ev)
+ ev := struct {
+ Payload string `form:"payload"`
+ }{}
+ c.Bind(&ev)
pl := &payload{}
if err := json.Unmarshal([]byte(ev.Payload), pl); err != nil {
- log.Println(err)
return err
}
-
- member := getRandomMember()
- text := "Назначен: " + member
- jm := message{
- Text: pl.Message.Text,
- ResponseType: "in_channel",
- Attachments: []Attachment{{Text: text, Color: "#36a64f"}},
- }
- b, _ := json.Marshal(jm)
- resp, err := http.DefaultClient.Post(pl.ResponseUrl, "application/json", bytes.NewReader(b))
- if err != nil {
- return err
- }
- if resp.StatusCode >= 300 || resp.StatusCode < 200 {
- return errors.New("unexpected status " + resp.Status)
+ // TODO how to get members???
+ //members, err := getUsers(token, pl.Channel.ID)
+ //if err != nil {
+ // return err
+ //}
+ members := []string{
+ "U8UJT0UJX",
+ "UCF7JA5CZ",
+ "UC86HMG1G",
+ "UCFHM9G04",
+ "UC9GZ0C2J",
}
- defer resp.Body.Close()
- s, _ := ioutil.ReadAll(resp.Body)
- log.Println(string(s))
-
- return nil
+ member := getRandomMember(members, pl.User.ID)
+ text := fmt.Sprintf("Назначен: <@%s>", member)
+ return sendMessage(pl.ResponseUrl, pl.Message.Text, text)
})
if err := e.Start(":3000"); err != nil {
log.Fatalln(err)
}
}
+
+func getClient() *http.Client {
+ transCfg := &http.Transport{
+ TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
+ }
+ return &http.Client{Transport: transCfg}
+}
diff --git a/members.go b/members.go
index 2886bed..81f0f15 100644
--- a/members.go
+++ b/members.go
@@ -5,19 +5,14 @@ import (
"time"
)
-func getRandomMember() string {
+func getRandomMember(team []string, except string) string {
rand.Seed(time.Now().Unix())
- team := []string{
- "<@U8UJT0UJX>",
- "<@UCF7JA5CZ>",
- "<@UC86HMG1G>",
- "<@UCFHM9G04>",
- "<@UC9GZ0C2J>",
+ ok := false
+ member := ""
+ for !ok {
+ n := rand.Int() % len(team)
+ member = team[n]
+ ok = member != except
}
-
- n := rand.Int() % len(team)
- member := team[n]
-
return member
}
-
diff --git a/message.go b/message.go
new file mode 100644
index 0000000..cb13e75
--- /dev/null
+++ b/message.go
@@ -0,0 +1,32 @@
+package main
+
+import (
+ "bytes"
+ "encoding/json"
+ "errors"
+ "io/ioutil"
+ "log"
+)
+
+func sendMessage(responseUrl, origText, text string) error {
+ jm := message{
+ Text: origText,
+ //ResponseType: "in_channel",
+ Attachments: []Attachment{{Text: text, Color: "#36a64f"}},
+ }
+ b, _ := json.Marshal(jm)
+
+ resp, err := getClient().Post(responseUrl, "application/json", bytes.NewReader(b))
+ if err != nil {
+ log.Println(err)
+ return err
+ }
+ defer resp.Body.Close()
+ s, _ := ioutil.ReadAll(resp.Body)
+ if resp.StatusCode >= 300 || resp.StatusCode < 200 {
+ log.Println("unexpected status " + resp.Status)
+ log.Println(string(s))
+ return errors.New("unexpected status " + resp.Status)
+ }
+ return nil
+}
diff --git a/requests.go b/requests.go
new file mode 100644
index 0000000..f8fa98d
--- /dev/null
+++ b/requests.go
@@ -0,0 +1,40 @@
+package main
+
+type payload struct {
+ Type string `json:"type" form:"type"`
+ Token string `json:"token"`
+ ActionTS string `json:"action_ts"`
+ Team struct {
+ ID string `json:"id"`
+ Domain string `json:"domain"`
+ }
+ User struct {
+ ID string `json:"id"`
+ Name string `json:"name"`
+ } `json:"user"`
+ Channel struct {
+ ID string `json:"id"`
+ Name string `json:"name"`
+ } `json:"channel"`
+ CallbackID string `json:"callback_id"`
+ TriggerID string `json:"trigger_id"`
+ MessageTS string `json:"message_ts"`
+ Message struct {
+ ClientMsgID string `json:"client_msg_id"`
+ Type string `json:"type"`
+ Text string `json:"text"`
+ User string `json:"user"`
+ TS string `json:"ts"`
+ } `json:"message"`
+ ResponseUrl string `json:"response_url"`
+}
+
+type message struct {
+ Text string `json:"text"`
+ ResponseType string `json:"response_type"`
+ Attachments []attachment `json:"attachments"`
+}
+type attachment struct {
+ Color string `json:"color"`
+ Text string `json:"text"`
+}