-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
143 lines (118 loc) · 4.46 KB
/
main.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"fmt"
"github.com/gorilla/mux"
bslack "github.com/twodarek/barcampgr-teams-bot/barcampgr/slack"
"github.com/twodarek/barcampgr-teams-bot/barcampgr/teams"
"github.com/twodarek/barcampgr-teams-bot/database"
webexteams "github.com/twodarek/go-cisco-webex-teams/sdk"
"log"
"net/http"
"os"
"github.com/slack-go/slack"
"github.com/twodarek/barcampgr-teams-bot/barcampgr"
"github.com/twodarek/barcampgr-teams-bot/server"
)
func main() {
log.Println("Attempting to start barcampgr-teams-bot")
router := mux.NewRouter()
httpClient := &http.Client{}
conf := barcampgr.Config{
SlackAPIToken: os.Getenv("SLACK_API_TOKEN"),
TeamsAPIToken: os.Getenv("CISCO_TEAMS_API_TOKEN"),
BarCampGRWebexId: os.Getenv("BARCAMPGR_WEBEX_ID"),
BaseCallbackURL: os.Getenv("BARCAMPGR_BASE_CALLBACK_URL"),
MySqlUser: os.Getenv("MYSQL_USER"),
MySqlPass: os.Getenv("MYSQL_PASS"),
MySqlServer: os.Getenv("MYSQL_SERVER"),
MySqlPort: os.Getenv("MYSQL_PORT"),
MySqlDatabase: os.Getenv("MYSQL_DATABASE"),
AdminPassword: os.Getenv("BARCAMPGR_ADMIN_PASSWORD"),
InvitePassword: os.Getenv("BARCAMPGR_INVITE_PASSWORD"),
SlackCallbackURL: os.Getenv("SLACK_CALLBACK_URL"),
SlackUsername: os.Getenv("SLACK_USERNAME"),
SlackVerificationToken: os.Getenv("SLACK_VERIFICATION_TOKEN"),
WebexTeamID: os.Getenv("BARCAMPGR_TEAM_ID"),
WebexOrgID: os.Getenv("WEBEX_ORG_ID"),
WebexRoomID: os.Getenv("WEBEX_ROOM_ID"),
WebexCallbackURL: os.Getenv("WEBEX_CALLBACK_URL"),
WebexMembershipCallbackURL: os.Getenv("WEBEX_MEMBERSHIP_CALLBACK_URL"),
}
conf.SetWebexAllRooms(os.Getenv("WEBEX_ALL_ROOMS"))
log.Println("Attempting to start slack client")
slackClient := slack.New(conf.SlackAPIToken, slack.OptionDebug(true))
log.Println("Slack client started")
log.Println("Attempting to start webex teams client")
teamsClient := webexteams.NewClient()
initTeamsClient(teamsClient, conf)
log.Println("Webex teams client started, connecting to database")
sdb := database.NewDatabase(conf.MySqlUser, conf.MySqlPass, conf.MySqlServer, conf.MySqlPort, conf.MySqlDatabase)
log.Println("Database connected")
ac := barcampgr.NewAppController(
httpClient,
sdb,
conf,
)
sac := bslack.NewAppController(
ac,
slackClient,
httpClient,
sdb,
conf,
)
tac := teams.NewAppController(
ac,
teamsClient,
httpClient,
sdb,
conf,
)
s := server.New(ac, sac, tac, conf, router)
// Multiple codepaths use the DefaultServeMux so we start listening at the top
go http.ListenAndServe("0.0.0.0:8080", s)
log.Println("Barcampgr-teams-bot started")
select {}
}
func initTeamsClient(client *webexteams.Client, config barcampgr.Config) error {
client.SetAuthToken(config.TeamsAPIToken)
// Clean up old webhooks
webhooksQueryParams := &webexteams.ListWebhooksQueryParams{
Max: 10,
}
webhooks, _, err := client.Webhooks.ListWebhooks(webhooksQueryParams)
if err != nil {
log.Printf("Unable to get old webhooks, continuing anyway")
}
for _, webhook := range webhooks.Items {
_, err := client.Webhooks.DeleteWebhook(webhook.ID)
if err != nil {
log.Printf("Unable to clean up old webhook %s on endpoint %s", webhook.ID, webhook.TargetURL)
}
}
// Create new @bot message webhook
webhookRequest := &webexteams.WebhookCreateRequest{
Name: "BarCampGR Webhook",
TargetURL: fmt.Sprintf("%s%s", config.BaseCallbackURL, config.WebexCallbackURL),
Resource: "messages",
Event: "created",
}
testWebhook, _, err := client.Webhooks.CreateWebhook(webhookRequest)
if err != nil {
log.Fatal(fmt.Printf("Failed to create webhook: %s", err))
}
log.Printf("Created chatop webhook. ID: %s, Name: %s, target URL: %s, created: %s", testWebhook.ID, testWebhook.Name, testWebhook.TargetURL, testWebhook.Created)
// Create new memberships webhook
membershipWebhookRequest := &webexteams.WebhookCreateRequest{
Name: "BarCampGR Memberships Webhook",
TargetURL: fmt.Sprintf("%s%s", config.BaseCallbackURL, config.WebexMembershipCallbackURL),
Resource: "memberships",
Event: "created",
Filter: fmt.Sprintf("roomId=%s", config.WebexRoomID),
}
testMembershipWebhook, _, err := client.Webhooks.CreateWebhook(membershipWebhookRequest)
if err != nil {
log.Fatal(fmt.Printf("Failed to create webhook: %s", err))
}
log.Printf("Created membership webhook. ID: %s, Name: %s, target URL: %s, created: %s", testMembershipWebhook.ID, testMembershipWebhook.Name, testMembershipWebhook.TargetURL, testMembershipWebhook.Created)
return nil
}