forked from stakwork/sphinx-tribes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalerts.go
123 lines (108 loc) · 3.23 KB
/
alerts.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package main
import (
"bytes"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"os"
"strconv"
)
type Action struct {
Action string `json:"action"` // "dm"
ChatUuid string `json:"chat_uuid"` // tribe uuid
Pubkey string `json:"pubkey"`
Content string `json:"content"`
BotId string `json:"bot_id"`
}
func processAlerts(p Person) {
// get the existing person's github_issues
// compare to see if there are new ones
// check the new ones for coding languages like "#Javascript"
// people need a new "extras"->"alerts" that lets them toggle on and off alerts
// pull people who have alerts on
// of those people, who have "Javascript" in their "coding_languages"?
// if they match, build an Action with their pubkey
// post all the Actions you have build to relay with the HMAC header
relayUrl := os.Getenv("ALERT_URL")
alertSecret := os.Getenv("ALERT_SECRET")
alertTribeUuid := os.Getenv("ALERT_TRIBE_UUID")
botId := os.Getenv("ALERT_BOT_ID")
if relayUrl == "" || alertSecret == "" || alertTribeUuid == "" || botId == "" {
fmt.Println("Ticket alerts: ENV information not found")
return
}
var action Action
action.ChatUuid = alertTribeUuid
action.Action = "dm"
action.BotId = botId
action.Content = "A new ticket relevant to your interests has been created on Sphinx Community - https://community.sphinx.chat/p?owner_id="
action.Content += p.OwnerPubKey
action.Content += "&created=" + strconv.Itoa(int(p.NewTicketTime))
// Check that new ticket time exists
if p.NewTicketTime == 0 {
fmt.Println("Ticket alerts: New ticket time not found")
return
}
var issue PropertyMap = nil
wanteds, ok := p.Extras["wanted"].([]interface{})
if !ok {
fmt.Println("Ticket alerts: No tickets found for person")
}
for _, wanted := range wanteds {
w, ok2 := wanted.(map[string]interface{})
if !ok2 {
continue
}
timeF, ok3 := w["created"].(float64)
if !ok3 {
continue
}
time := int64(timeF)
if time == p.NewTicketTime {
issue = w
break
}
}
if issue == nil {
fmt.Println("Ticket alerts: No ticket identified with the correct timestamp")
}
languages, ok4 := issue["codingLanguage"].([]interface{})
if !ok4 {
fmt.Println("Ticket alerts: No languages found in ticket")
return
}
var err error
people, err := DB.getPeopleForNewTicket(languages)
if err != nil {
fmt.Println("Ticket alerts: DB query to get interested people failed", err)
return
}
client := http.Client{}
for _, per := range people {
action.Pubkey = per.OwnerPubKey
buf, err := json.Marshal(action)
if err != nil {
fmt.Println("Ticket alerts: Unable to parse message into byte buffer", err)
return
}
request, err := http.NewRequest("POST", relayUrl, bytes.NewReader(buf))
if err != nil {
fmt.Println("Ticket alerts: Unable to create a request to send to relay", err)
return
}
mac := hmac.New(sha256.New, []byte(alertSecret))
mac.Write(buf)
hmac256Byte := mac.Sum(nil)
hmac256Hex := "sha256=" + hex.EncodeToString(hmac256Byte)
request.Header.Set("x-hub-signature-256", hmac256Hex)
request.Header.Set("Content-Type", "application/json")
_, err = client.Do(request)
if err != nil {
fmt.Println("Ticket alerts: Unable to communicate request to relay", err)
}
}
return
}