Skip to content

Commit

Permalink
Summarise activity on the next 2 days (#13)
Browse files Browse the repository at this point in the history
When running the `!psr` or `!bspp` bot commands, activity will be computed for today and the next 2 days.

This commit also ignores message commands older than 2 minutes.
  • Loading branch information
fabien-chebel authored Jun 21, 2022
1 parent 39351e4 commit 6d7fe98
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 18 deletions.
47 changes: 34 additions & 13 deletions bot-service.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"fmt"
"github.com/fabien-chebel/pegass-cli/whatsapp"
log "github.com/sirupsen/logrus"
Expand All @@ -13,25 +14,45 @@ type BotService struct {
chatClient *whatsapp.WhatsAppClient
}

func (b *BotService) SendActivitySummary(recipient types.JID, kind ActivityKind) {
func (b *BotService) SendActivitySummary(recipient types.JID, kind ActivityKind, dayCount int) {
var kindName string
if kind == SAMU {
kindName = "SAMU"
} else {
kindName = "BSPP"
}

err := b.chatClient.SendMessage("🤖 C'est reçu. Je génère l'état des postes de demain.", recipient)
err := b.chatClient.SendMessage(fmt.Sprintf("🤖 C'est reçu. Je génère l'état des postes %s sur %d jours.", kindName, dayCount), recipient)
if err != nil {
log.Errorf("failed to send whatsapp message: %s", err.Error())
return
}

day := time.Now().AddDate(0, 0, 1).Format("2006-01-02")
log.Info("Fetching activity summary for day ", day)
summary, err := b.pegassClient.GetActivityOnDay(day, kind, false)
if err != nil {
log.Errorf("failed to generate activity summary for day '%s' and kind '%d'", day, kind)
return
for i := 0; i < dayCount; i++ {
var buf = new(bytes.Buffer)

day := time.Now().AddDate(0, 0, i).Format("2006-01-02")
log.Infof("fetching activity summary for day '%s' and kind '%#v'", day, kind)
summary, err := b.pegassClient.GetActivityOnDay(day, kind, false)
if err != nil {
log.Errorf("failed to generate activity summary for day '%s' and kind '%d'. error='%s'", day, kind, err.Error())
return
}

switch i {
case 0:
buf.WriteString(fmt.Sprintf("*Aujourd'hui* (%s) :\n", day))
case 1:
buf.WriteString(fmt.Sprintf("*Demain* (%s) :\n", day))
case 2:
buf.WriteString(fmt.Sprintf("*Après-demain* (%s) :\n", day))
}

buf.WriteString(summary + "\n")
err = b.chatClient.SendMessage(
buf.String(),
recipient,
)
}
summary = fmt.Sprintf("Etat du réseau de secours de demain (%s):\n%s", day, summary)

err = b.chatClient.SendMessage(
summary,
recipient,
)
}
13 changes: 10 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,20 +301,27 @@ func main() {
pegassClient: &pegassClient,
chatClient: &whatsAppClient,
}
whatsAppClient.SetMessageCallback(func(senderName string, senderId types.JID, chatId types.JID, content string) {
whatsAppClient.SetMessageCallback(func(senderName string, senderId types.JID, chatId types.JID, content string, timestamp time.Time) {
if !isGroupOwnedByCRF(config.WhatsAppBotGroups, chatId.String()) {
// Security: only whitelisted groups are able to use bot features
return
}
log.Infof("Received message from '%s': %s", senderName, content)
if time.Since(timestamp) > 2*time.Minute {
log.Infof("ignoring message, as it was sent more than 2 minutes ago.")
return
}

var recipient = chatId
lowerMessage := strings.ToLower(content)

if strings.HasPrefix(lowerMessage, "!psr") {
botService.SendActivitySummary(recipient, SAMU)
botService.SendActivitySummary(recipient, SAMU, 3)
} else if strings.HasPrefix(lowerMessage, "!bspp") {
botService.SendActivitySummary(recipient, BSPP)
botService.SendActivitySummary(recipient, BSPP, 3)
} else if strings.HasPrefix(lowerMessage, "!today") {
botService.SendActivitySummary(recipient, SAMU, 1)
botService.SendActivitySummary(recipient, BSPP, 1)
}
})
err = whatsAppClient.StartBot()
Expand Down
4 changes: 2 additions & 2 deletions whatsapp/whatsapp-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func (w *WhatsAppClient) eventHandler(evt interface{}) {
switch v := evt.(type) {
case *events.Message:
if w.onMessageReceived != nil {
w.onMessageReceived(v.Info.PushName, v.Info.Sender, v.Info.Chat, v.Message.GetConversation())
w.onMessageReceived(v.Info.PushName, v.Info.Sender, v.Info.Chat, v.Message.GetConversation(), v.Info.Timestamp)
}
case *events.Disconnected:
log.Warn("WhatsApp client was disconnected")
Expand All @@ -162,4 +162,4 @@ func (w *WhatsAppClient) eventHandler(evt interface{}) {
}
}

type MessageCallback func(senderName string, senderId types.JID, chatId types.JID, content string)
type MessageCallback func(senderName string, senderId types.JID, chatId types.JID, content string, timestamp time.Time)

0 comments on commit 6d7fe98

Please sign in to comment.