Compare commits
No commits in common. "main" and "v0.2" have entirely different histories.
@ -125,7 +125,7 @@ slack:
|
|||||||
api_token: SLACK_API_TOKEN
|
api_token: SLACK_API_TOKEN
|
||||||
create_from_weekday: 3
|
create_from_weekday: 3
|
||||||
default_conversation: SLACK_UID
|
default_conversation: SLACK_UID
|
||||||
sticky_users:
|
sticky_users:
|
||||||
- SLACK_UID
|
- SLACK_UID
|
||||||
|
|
||||||
```
|
```
|
||||||
2
main.go
2
main.go
@ -13,7 +13,7 @@ import (
|
|||||||
const (
|
const (
|
||||||
serviceName = "service-notifications"
|
serviceName = "service-notifications"
|
||||||
serviceDescription = "Notifications for church services"
|
serviceDescription = "Notifications for church services"
|
||||||
serviceVersion = "0.2.1"
|
serviceVersion = "0.2"
|
||||||
)
|
)
|
||||||
|
|
||||||
// App is the global application structure for communicating between servers and storing information.
|
// App is the global application structure for communicating between servers and storing information.
|
||||||
|
|||||||
36
update.go
36
update.go
@ -110,8 +110,7 @@ func UpdatePCData() {
|
|||||||
// Get the plans for this service type.
|
// Get the plans for this service type.
|
||||||
allPlans, err := PCGetAll(fmt.Sprintf("/services/v2/service_types/%d/plans", serviceTypeID))
|
allPlans, err := PCGetAll(fmt.Sprintf("/services/v2/service_types/%d/plans", serviceTypeID))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error getting plans for service type:", serviceTypeID, err)
|
log.Fatalln(err)
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
// For each plan, update data in database and pull other plan releated items for updates.
|
// For each plan, update data in database and pull other plan releated items for updates.
|
||||||
for _, data := range allPlans {
|
for _, data := range allPlans {
|
||||||
@ -153,8 +152,7 @@ func UpdatePCData() {
|
|||||||
// Get all times for this plan.
|
// Get all times for this plan.
|
||||||
allPlanTimes, err := PCGetAll(fmt.Sprintf("/services/v2/service_types/%d/plans/%d/plan_times", serviceTypeID, planID))
|
allPlanTimes, err := PCGetAll(fmt.Sprintf("/services/v2/service_types/%d/plans/%d/plan_times", serviceTypeID, planID))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error getting plan times for plan:", planID, err)
|
log.Fatalln(err)
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
// With each time, save it to the database.
|
// With each time, save it to the database.
|
||||||
for _, data := range allPlanTimes {
|
for _, data := range allPlanTimes {
|
||||||
@ -190,8 +188,7 @@ func UpdatePCData() {
|
|||||||
// Get all members of the plan.
|
// Get all members of the plan.
|
||||||
allTeamMembers, err := PCGetAll(fmt.Sprintf("/services/v2/service_types/%d/plans/%d/team_members", serviceTypeID, planID))
|
allTeamMembers, err := PCGetAll(fmt.Sprintf("/services/v2/service_types/%d/plans/%d/team_members", serviceTypeID, planID))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error getting team members for plan:", planID, err)
|
log.Fatalln(err)
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
// With each member, update the database.
|
// With each member, update the database.
|
||||||
for _, data := range allTeamMembers {
|
for _, data := range allTeamMembers {
|
||||||
@ -229,13 +226,11 @@ func UpdateSlackData() {
|
|||||||
// Get all users from Slack.
|
// Get all users from Slack.
|
||||||
users, err := app.slack.GetUsers()
|
users, err := app.slack.GetUsers()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error getting Slack users:", err)
|
log.Fatalln(err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
// If no users returned, error as we should have some...
|
// If no users returned, error as we should have some...
|
||||||
if len(users) == 0 {
|
if len(users) == 0 {
|
||||||
log.Println("No users found in Slack.")
|
log.Fatalln("No users found in Slack.")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
// With each user, update the database.
|
// With each user, update the database.
|
||||||
for _, user := range users {
|
for _, user := range users {
|
||||||
@ -359,8 +354,7 @@ func CreateSlackChannels() {
|
|||||||
app.db.Where("time_type='service' AND starts_at > ? AND starts_at < ?", startDate, lastDate).Find(&planTimes)
|
app.db.Where("time_type='service' AND starts_at > ? AND starts_at < ?", startDate, lastDate).Find(&planTimes)
|
||||||
// If no plan times matched, exit here.
|
// If no plan times matched, exit here.
|
||||||
if len(planTimes) == 0 {
|
if len(planTimes) == 0 {
|
||||||
log.Println("No services found for this time frame.")
|
log.Fatalln("No services found for this time frame.")
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// With each plan time found, create a slack channel.
|
// With each plan time found, create a slack channel.
|
||||||
@ -438,28 +432,22 @@ func CreateSlackChannels() {
|
|||||||
log.Println("Creating channel:", channel.Name)
|
log.Println("Creating channel:", channel.Name)
|
||||||
schan, err := app.slack.CreateConversation(channelInfo)
|
schan, err := app.slack.CreateConversation(channelInfo)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Failed to create channel:", err)
|
log.Fatalln("Failed to create channel:", err)
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// If topic is defined, set the topic and purpose.
|
// If topic is defined, set the topic and purpose.
|
||||||
if topic != "" {
|
if topic != "" {
|
||||||
// Keep count of failures so we can try again.
|
// Sleep before, as it takes time for Slack APIs
|
||||||
failed := 0
|
// to recongize the channel was created.
|
||||||
_, err = app.slack.SetTopicOfConversation(schan.ID, topic)
|
time.Sleep(120 * time.Second)
|
||||||
|
_, err = app.slack.SetTopicOfConversation(channel.ID, topic)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
failed++
|
|
||||||
log.Println("Failed to set topic:", err)
|
log.Println("Failed to set topic:", err)
|
||||||
}
|
}
|
||||||
_, err = app.slack.SetPurposeOfConversation(schan.ID, topic)
|
_, err = app.slack.SetPurposeOfConversation(channel.ID, topic)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
failed++
|
|
||||||
log.Println("Failed to set purpose:", err)
|
log.Println("Failed to set purpose:", err)
|
||||||
}
|
}
|
||||||
// If it failed, make topic empty so we can try again next run.
|
|
||||||
if failed != 0 {
|
|
||||||
topic = ""
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save the channel to the database.
|
// Save the channel to the database.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user