-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
08b0836
commit c4be7c7
Showing
9 changed files
with
238 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package cmd | ||
|
||
import ( | ||
"log" | ||
"time" | ||
|
||
psh "github.com/gregdel/pushover" | ||
air "github.com/mehanizm/airtable" | ||
"github.com/spf13/cobra" | ||
|
||
"github.com/charlieegan3/airtable-contacts/pkg/airtable" | ||
"github.com/charlieegan3/airtable-contacts/pkg/pushover" | ||
"github.com/charlieegan3/airtable-contacts/pkg/specialdays" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var dayCmd = &cobra.Command{ | ||
Use: "day", | ||
Short: "send events for current day", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
// get the latest data | ||
airtableClient := air.NewClient(viper.GetString("airtable.key")) | ||
records, err := airtable.Download( | ||
airtableClient, | ||
viper.GetString("airtable.base"), | ||
viper.GetString("airtable.table"), | ||
viper.GetString("airtable.view"), | ||
) | ||
if err != nil { | ||
log.Fatalf("failed to download contacts: %s", err) | ||
} | ||
|
||
// set the notification period | ||
periodStart := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.UTC) | ||
alert, title, message, err := specialdays.Generate(records, periodStart, 1) | ||
if err != nil { | ||
log.Fatalf("failed to generate alert message: %s", err) | ||
} | ||
|
||
// send the alert if needed | ||
if alert { | ||
pushoverRecipient := psh.NewRecipient(viper.GetString("pushover.user_key")) | ||
pushoverApp := psh.New(viper.GetString("pushover.app_token")) | ||
pushover.Notify(pushoverApp, pushoverRecipient, title, message) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
notifyCmd.AddCommand(dayCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
var notifyCmd = &cobra.Command{ | ||
Use: "notify", | ||
Short: "send alerts for events related to contacts", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Println("notify called") | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(notifyCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package cmd | ||
|
||
import ( | ||
"log" | ||
"time" | ||
|
||
"github.com/charlieegan3/airtable-contacts/pkg/airtable" | ||
"github.com/charlieegan3/airtable-contacts/pkg/pushover" | ||
"github.com/charlieegan3/airtable-contacts/pkg/specialdays" | ||
psh "github.com/gregdel/pushover" | ||
air "github.com/mehanizm/airtable" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var weekCmd = &cobra.Command{ | ||
Use: "week", | ||
Short: "send events for the current week", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
// get the latest data | ||
airtableClient := air.NewClient(viper.GetString("airtable.key")) | ||
records, err := airtable.Download( | ||
airtableClient, | ||
viper.GetString("airtable.base"), | ||
viper.GetString("airtable.table"), | ||
viper.GetString("airtable.view"), | ||
) | ||
if err != nil { | ||
log.Fatalf("failed to download contacts: %s", err) | ||
} | ||
|
||
// set the notification period | ||
periodStart := time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 0, 0, 0, 0, time.UTC) | ||
alert, title, message, err := specialdays.Generate(records, periodStart, 14) | ||
if err != nil { | ||
log.Fatalf("failed to generate alert message: %s", err) | ||
} | ||
|
||
// send the alert if needed | ||
pushoverRecipient := psh.NewRecipient(viper.GetString("pushover.user_key")) | ||
pushoverApp := psh.New(viper.GetString("pushover.app_token")) | ||
if alert { | ||
pushover.Notify(pushoverApp, pushoverRecipient, title, message) | ||
} else { | ||
pushover.Notify(pushoverApp, pushoverRecipient, "No Events", "There are no events in the next two weeks") | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
notifyCmd.AddCommand(weekCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package pushover | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/gregdel/pushover" | ||
psh "github.com/gregdel/pushover" | ||
) | ||
|
||
func Notify(app *psh.Pushover, recipient *psh.Recipient, title, body string) error { | ||
message := pushover.NewMessageWithTitle(body, title) | ||
|
||
_, err := app.SendMessage(message, recipient) | ||
if err != nil { | ||
return fmt.Errorf("failed to send pushover notification: %s", err) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters