-
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.
ping users that still did not respond
- Loading branch information
1 parent
348dde0
commit 5686725
Showing
2 changed files
with
86 additions
and
0 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,69 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/charmbracelet/log" | ||
"github.com/slack-go/slack" | ||
bolt "go.etcd.io/bbolt" | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
||
const pingCron = "30 12 * * 1,3,5" | ||
|
||
func PingMissingUsers() error { | ||
users := map[string][]string{} | ||
|
||
err := App.db.View(func(tx *bolt.Tx) error { | ||
return tx.Bucket([]byte("messages")).ForEach(func(k, v []byte) error { | ||
var qi QuestionInstance | ||
err := json.Unmarshal(v, &qi) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = qi.LoadQuestion() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
ts, err := strconv.ParseFloat(qi.Timestamp, 64) | ||
if err != nil { | ||
log.Error("Cannot parse timestamp for message.", "message", string(k), "err", err) | ||
return nil // we ignore this error as it should not really happen, and it should not break the loop | ||
} | ||
|
||
posted := time.Unix(int64(ts), 0) | ||
if time.Now().Sub(posted) < 24*time.Hour { | ||
return nil | ||
} | ||
|
||
for user, replied := range qi.Responses { | ||
if replied { | ||
users[user] = append(users[user], qi.Question.Channel) | ||
} | ||
} | ||
return nil | ||
}) | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for user, channels := range users { | ||
log.Info("Pinging.", "user", user, "channels", channels) | ||
msg := "Ahoj, zatiaľ si sa nevyjadril/-a do môjho update threadu v týchto kanáloch:\n%s\nNájdi si prosím minútku a doplň odpovede 😇" | ||
var channelMentions []string | ||
for _, channel := range channels { | ||
channelMentions = append(channelMentions, fmt.Sprintf("<#%s>", channel)) | ||
} | ||
|
||
_, _, err := App.slack.PostMessage(user, slack.MsgOptionText(fmt.Sprintf(msg, strings.Join(channelMentions, ", ")), false)) | ||
if err != nil { | ||
log.Error("Could not send ping message.", "user", user, "err", 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