Compare commits
No commits in common. "main" and "v0.1" have entirely different histories.
7 changed files with 26 additions and 157 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
|
@ -1,4 +1,3 @@
|
||||||
config.yaml
|
config.yaml
|
||||||
service-notifications
|
service-notifications
|
||||||
service-notifications.db
|
service-notifications.db
|
||||||
sync.sh
|
|
||||||
|
|
@ -110,7 +110,6 @@ Get Slack API token by creating an app at https://api.slack.com/apps then go to
|
||||||
|
|
||||||
Get Planning Center API secrets at https://api.planningcenteronline.com/oauth/applications by creating a personal access token.
|
Get Planning Center API secrets at https://api.planningcenteronline.com/oauth/applications by creating a personal access token.
|
||||||
|
|
||||||
You can get a slack user ID by viewing the profile and under the 3 dot menu choose Copy member ID.
|
|
||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
---
|
---
|
||||||
|
|
@ -123,9 +122,6 @@ planning_center:
|
||||||
|
|
||||||
slack:
|
slack:
|
||||||
api_token: SLACK_API_TOKEN
|
api_token: SLACK_API_TOKEN
|
||||||
create_from_weekday: 3
|
admin_id: SLACK_UID
|
||||||
default_conversation: SLACK_UID
|
|
||||||
sticky_users:
|
|
||||||
- SLACK_UID
|
|
||||||
|
|
||||||
```
|
```
|
||||||
31
api.go
31
api.go
|
|
@ -2,14 +2,12 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/slack-go/slack"
|
"github.com/slack-go/slack"
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Commonly used strings.
|
// Commonly used strings.
|
||||||
|
|
@ -98,36 +96,15 @@ func (s *HTTPServer) RegisterAPIRoutes(r *mux.Router) {
|
||||||
|
|
||||||
// Get current time and default conversation.
|
// Get current time and default conversation.
|
||||||
now := time.Now().UTC()
|
now := time.Now().UTC()
|
||||||
conversation := app.config.Slack.DefaultConversation
|
conversation := app.config.Slack.AdminID
|
||||||
|
|
||||||
// Find plan times that are occuring right now. A 60-minute buffer
|
// Find plan times that are occuring right now.
|
||||||
// is applied past ends_at so services that run long still resolve
|
|
||||||
// to their event channel instead of falling back to the default.
|
|
||||||
// Order by starts_at DESC so the most recent active service wins
|
|
||||||
// when a later service's window overlaps an earlier service's buffer.
|
|
||||||
var planTime PlanTimes
|
var planTime PlanTimes
|
||||||
err = app.db.Where("time_type='service' AND starts_at < ? AND ends_at > ?", now, now.Add(-60*time.Minute)).Order("starts_at DESC").First(&planTime).Error
|
app.db.Where("time_type='service' AND starts_at < ? AND ends_at > ?", now, now).First(&planTime)
|
||||||
// A "record not found" simply means no service is occuring right now, in
|
|
||||||
// which case we fall back to the default conversation. Any other error
|
|
||||||
// (e.g. "database is locked") must NOT be swallowed: treating it as "no
|
|
||||||
// service" would silently misroute the message to the default
|
|
||||||
// conversation instead of the event channel. Fail so the caller retries.
|
|
||||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
||||||
log.Println("Error looking up plan time:", err)
|
|
||||||
s.APISendGeneralResp(w, APIERR, "Error looking up plan time")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if planTime.Plan != 0 {
|
if planTime.Plan != 0 {
|
||||||
// If plan found, check for the slack channel.
|
// If plan found, check for the slack channel.
|
||||||
var channel SlackChannels
|
var channel SlackChannels
|
||||||
err = app.db.Where("pc_plan = ?", planTime.Plan).First(&channel).Error
|
app.db.Where("pc_plan = ?", planTime.Plan).First(&channel)
|
||||||
// As above, only "record not found" is a benign result here. On any
|
|
||||||
// other error we must not fall through to the default conversation.
|
|
||||||
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
|
|
||||||
log.Println("Error looking up slack channel:", err)
|
|
||||||
s.APISendGeneralResp(w, APIERR, "Error looking up slack channel")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if channel.ID != "" {
|
if channel.ID != "" {
|
||||||
// If slack channel found, update the conversation to the channel ID.
|
// If slack channel found, update the conversation to the channel ID.
|
||||||
conversation = channel.ID
|
conversation = channel.ID
|
||||||
|
|
|
||||||
|
|
@ -35,11 +35,9 @@ type PlanningCenterConfig struct {
|
||||||
|
|
||||||
// Configurations relating to Slack API/channel creation.
|
// Configurations relating to Slack API/channel creation.
|
||||||
type SlackConfig struct {
|
type SlackConfig struct {
|
||||||
CreateFromWeekday int `fig:"create_from_weekday"` // Create ahead from this weekday. -1 value is default and will instead create from the current time of operation.
|
|
||||||
CreateChannelsAhead time.Duration `fig:"create_channels_ahead"` // Amount of time of future services to create channels head for. Defaults to 8 days head.
|
CreateChannelsAhead time.Duration `fig:"create_channels_ahead"` // Amount of time of future services to create channels head for. Defaults to 8 days head.
|
||||||
APIToken string `fig:"api_token"`
|
APIToken string `fig:"api_token"`
|
||||||
StickyUsers []string `fig:"sticky_users"` // Users to add to every channel.
|
AdminID string `fig:"admin_id"` // Slack user that administers this app.
|
||||||
DefaultConversation string `fig:"default_conversation"` // Slack user that administers this app.
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Configuration Structure.
|
// Configuration Structure.
|
||||||
|
|
@ -88,7 +86,6 @@ func (a *App) ReadConfig() {
|
||||||
Connection: "service-notifications.db",
|
Connection: "service-notifications.db",
|
||||||
},
|
},
|
||||||
Slack: SlackConfig{
|
Slack: SlackConfig{
|
||||||
CreateFromWeekday: -1,
|
|
||||||
CreateChannelsAhead: time.Hour * 24 * 8,
|
CreateChannelsAhead: time.Hour * 24 * 8,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
||||||
17
database.go
17
database.go
|
|
@ -2,7 +2,6 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gorm.io/driver/mysql"
|
"gorm.io/driver/mysql"
|
||||||
|
|
@ -127,21 +126,7 @@ func (a *App) InitDB() {
|
||||||
}
|
}
|
||||||
// Depending on connection configuration, open the database.
|
// Depending on connection configuration, open the database.
|
||||||
if a.config.DB.Type == "sqlite3" {
|
if a.config.DB.Type == "sqlite3" {
|
||||||
// Enable WAL journaling and a busy timeout. Without WAL, a single
|
a.db, err = gorm.Open(sqlite.Open(a.config.DB.Connection), dbConfig)
|
||||||
// long-running writer (e.g. the channel-creation/sync routine, which
|
|
||||||
// interleaves slow Slack API calls with its writes) blocks all readers,
|
|
||||||
// causing "database is locked" on concurrent reads such as the
|
|
||||||
// send_message channel lookup. WAL lets reads proceed alongside the
|
|
||||||
// writer, and the busy timeout makes any remaining contention wait
|
|
||||||
// rather than fail immediately. Append as DSN pragmas, preserving any
|
|
||||||
// query string already present in the configured connection.
|
|
||||||
conn := a.config.DB.Connection
|
|
||||||
sep := "?"
|
|
||||||
if strings.Contains(conn, "?") {
|
|
||||||
sep = "&"
|
|
||||||
}
|
|
||||||
conn += sep + "_journal_mode=WAL&_busy_timeout=10000"
|
|
||||||
a.db, err = gorm.Open(sqlite.Open(conn), dbConfig)
|
|
||||||
} else if a.config.DB.Type == "mysql" {
|
} else if a.config.DB.Type == "mysql" {
|
||||||
a.db, err = gorm.Open(mysql.Open(a.config.DB.Connection), dbConfig)
|
a.db, err = gorm.Open(mysql.Open(a.config.DB.Connection), dbConfig)
|
||||||
} else if a.config.DB.Type == "postgres" {
|
} else if a.config.DB.Type == "postgres" {
|
||||||
|
|
|
||||||
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.2"
|
serviceVersion = "0.1"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 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.
|
||||||
|
|
|
||||||
119
update.go
119
update.go
|
|
@ -13,19 +13,6 @@ import (
|
||||||
|
|
||||||
// Update planning center database tables with data from PC API.
|
// Update planning center database tables with data from PC API.
|
||||||
func UpdatePCData() {
|
func UpdatePCData() {
|
||||||
// We don't need to update the archive if we already have data from the past,
|
|
||||||
// as such we get the current time and see if we already have an entry in the future.
|
|
||||||
// Its possible that some people only schedule services once a week, so we check with
|
|
||||||
// the current date subtracted by 14 days.
|
|
||||||
var updateFrom time.Time
|
|
||||||
now := time.Now().UTC()
|
|
||||||
var futurePlan Plans
|
|
||||||
app.db.Where("first_time_at >= ?", now.Add(time.Hour*24*14*-1)).Order("first_time_at ASC").First(&futurePlan)
|
|
||||||
if futurePlan.ID != 0 {
|
|
||||||
// If a future plan exists, we update from past 30 days.
|
|
||||||
updateFrom = now.Add(time.Hour * 24 * 30 * -1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get all people.
|
// Get all people.
|
||||||
allPeople, err := PCGetAll("/services/v2/people")
|
allPeople, err := PCGetAll("/services/v2/people")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -110,8 +97,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 {
|
||||||
|
|
@ -132,13 +118,6 @@ func UpdatePCData() {
|
||||||
p.MultiDay = attributes.GetBool("multi_day")
|
p.MultiDay = attributes.GetBool("multi_day")
|
||||||
p.Dates = attributes.GetString("dates")
|
p.Dates = attributes.GetString("dates")
|
||||||
|
|
||||||
// If either updated at or first time at for the plan is before the update from date,
|
|
||||||
// we can process the update of data. Otherwise, we ignore this service as we do not care
|
|
||||||
// about updating historic data. Updating historic data causes more API traffic than needed.
|
|
||||||
if p.UpdatedAt.Before(updateFrom) && p.FirstTimeAt.Before(updateFrom) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// If plan wasn't already created, create it.
|
// If plan wasn't already created, create it.
|
||||||
if p.ID == 0 {
|
if p.ID == 0 {
|
||||||
p.ID = planID
|
p.ID = planID
|
||||||
|
|
@ -153,8 +132,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 +168,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 +206,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 {
|
||||||
|
|
@ -316,41 +291,13 @@ func UpdateSlackData() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
|
|
||||||
Delay on channel descript/topic may not be long enough.
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
// Create slack channels for upcoming services.
|
// Create slack channels for upcoming services.
|
||||||
func CreateSlackChannels() {
|
func CreateSlackChannels() {
|
||||||
// Start at now.
|
// For now, we're using the start time of now. I want to update this later to allow
|
||||||
now := time.Now().UTC()
|
// setting a day of the week for slack channels to be created on.
|
||||||
startDate := now
|
// Doing a day of the week will allow for channels to be created ahead of time, then
|
||||||
// If create from weekday is a valid weekday, attempt to turn back the clock to the
|
// if people are added to the plan later on, they can be added at the next cron run.
|
||||||
// most recently past weekday. Use that day as the stating point so we do not
|
startDate := time.Now().UTC()
|
||||||
// create channels in the future past the date we expect to have channels.
|
|
||||||
// This is useful if you want to run the cron every day to keep channel title
|
|
||||||
// and members up to date, but only want so many channels ahead of a certain weekday.
|
|
||||||
if app.config.Slack.CreateFromWeekday != -1 && app.config.Slack.CreateFromWeekday <= 6 {
|
|
||||||
// Get the current weekday and set the days to subtract to 0.
|
|
||||||
thisWeekday := int(now.Weekday())
|
|
||||||
var daysSub int = 0
|
|
||||||
|
|
||||||
// If this weekday is the day we intend to create from, or if the weekday is
|
|
||||||
// after. We want to just subtract this weekday from create form weekday which
|
|
||||||
// should get us back to the most recent weekday.
|
|
||||||
if thisWeekday >= app.config.Slack.CreateFromWeekday {
|
|
||||||
daysSub = app.config.Slack.CreateFromWeekday - thisWeekday
|
|
||||||
} else {
|
|
||||||
// Otherwise, we have started a new week from that weekday and we need to
|
|
||||||
// add 7 days to the current weekday in our subtraction. This will bring us
|
|
||||||
// not to the next weekday, but the past weekday.
|
|
||||||
daysSub = app.config.Slack.CreateFromWeekday - (thisWeekday + 7)
|
|
||||||
}
|
|
||||||
// Subtract the number of days calculated to bring us to the weekday to create form.
|
|
||||||
startDate = now.Add(time.Hour * 24 * time.Duration(daysSub))
|
|
||||||
}
|
|
||||||
// Last date is start date plus duration of create channels ahead.
|
// Last date is start date plus duration of create channels ahead.
|
||||||
lastDate := startDate.Add(app.config.Slack.CreateChannelsAhead)
|
lastDate := startDate.Add(app.config.Slack.CreateChannelsAhead)
|
||||||
|
|
||||||
|
|
@ -359,8 +306,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 +384,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(10 * 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.
|
||||||
|
|
@ -481,31 +421,6 @@ func CreateSlackChannels() {
|
||||||
// Keep a list of users we need to invite as they are new.
|
// Keep a list of users we need to invite as they are new.
|
||||||
var usersToInvite []string
|
var usersToInvite []string
|
||||||
|
|
||||||
// For each sticky user, invite them.
|
|
||||||
for _, stickyUser := range app.config.Slack.StickyUsers {
|
|
||||||
// Check if they were already invited.
|
|
||||||
alreadyInvited := false
|
|
||||||
for _, uid := range invited {
|
|
||||||
if uid == stickyUser {
|
|
||||||
alreadyInvited = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Make sure they were not already added to the list of users.
|
|
||||||
for _, uid := range usersToInvite {
|
|
||||||
if uid == stickyUser {
|
|
||||||
alreadyInvited = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// If not already invited, add to the list of users to invite.
|
|
||||||
if !alreadyInvited {
|
|
||||||
usersToInvite = append(usersToInvite, stickyUser)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// For each person on the plan, see if we need to invite them.
|
// For each person on the plan, see if we need to invite them.
|
||||||
for _, personOnPlan := range peopleOnPlan {
|
for _, personOnPlan := range peopleOnPlan {
|
||||||
// Find the slack user for the planning center person.
|
// Find the slack user for the planning center person.
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue