summaryrefslogtreecommitdiff
path: root/main.go
diff options
context:
space:
mode:
authorAlexander Kiryukhin <alexander@kiryukhin.su>2019-01-31 01:02:31 +0300
committerAlexander Kiryukhin <alexander@kiryukhin.su>2019-01-31 01:02:31 +0300
commit91c16487472cb23671ffdc9c230120441f85b620 (patch)
tree7454aa0053a2b613ff02c3a75db8bc5e09d6b5b2 /main.go
Initial commit
Diffstat (limited to 'main.go')
-rw-r--r--main.go92
1 files changed, 92 insertions, 0 deletions
diff --git a/main.go b/main.go
new file mode 100644
index 0000000..e3d2228
--- /dev/null
+++ b/main.go
@@ -0,0 +1,92 @@
+package main
+
+import (
+ "bytes"
+ "encoding/json"
+ "errors"
+ "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() {
+ e := echo.New()
+ e.POST("/events-endpoint", func(c echo.Context) error {
+ ev := &event{}
+ 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)
+ }
+ defer resp.Body.Close()
+ s, _ := ioutil.ReadAll(resp.Body)
+ log.Println(string(s))
+
+ return nil
+ })
+ if err := e.Start(":3000"); err != nil {
+ log.Fatalln(err)
+ }
+}