Skip to content

Commit

Permalink
improve offline bot checking
Browse files Browse the repository at this point in the history
  • Loading branch information
adbenitez committed Feb 13, 2024
1 parent 2fd338d commit 2600a9f
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 19 deletions.
12 changes: 12 additions & 0 deletions src/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var cfg *Config

type Config struct {
StatusLastChecked time.Time
OffLastChecked time.Time
BotsData BotsData

rpc *deltachat.Rpc
Expand Down Expand Up @@ -136,6 +137,17 @@ func (self *Config) SaveStatusLastChecked() error {
return os.WriteFile(self.path, output, 0666)
}

func (self *Config) SaveOffLastChecked() error {
self.mutex.Lock()
defer self.mutex.Unlock()
self.OffLastChecked = time.Now()
output, err := json.Marshal(self)
if err != nil {
return err
}
return os.WriteFile(self.path, output, 0666)
}

func GetMD5Hash(data []byte) string {
hash := md5.Sum(data)
return hex.EncodeToString(hash[:])
Expand Down
90 changes: 71 additions & 19 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"io"
"math"
"net/http"
"path/filepath"
"strings"
Expand Down Expand Up @@ -50,6 +49,60 @@ func onBotStart(cli *botcli.BotCli, bot *deltachat.Bot, cmd *cobra.Command, args
}
go updateBotsLoop()
go updateStatusLoop(bot.Rpc)
go updateOfflineBotsStatusLoop(bot.Rpc)
}

func updateOfflineBotsStatusLoop(rpc *deltachat.Rpc) {
logger := cli.Logger.With("origin", "off-bots-status-loop")
delay := 60 * time.Minute
for {
toSleep := delay - time.Since(cfg.OffLastChecked)
if toSleep > 0 {
logger.Debugf("Sleeping for %v", toSleep)
time.Sleep(toSleep)
}
if err := cfg.SaveOffLastChecked(); err != nil {
cli.Logger.Error(err)
}
botsData := cfg.GetBotsData()
selfAddrs := getSelfAddrs(rpc)
accId := getFirstAccount(rpc)
for _, bot := range botsData.Bots {
if accId == 0 {
break
}
if _, ok := selfAddrs[bot.Addr]; ok {
continue
}
logger := logger.With("acc", accId, "bot", bot.Addr)
contactId, err := rpc.CreateContact(accId, bot.Addr, "")
if err != nil {
logger.Error(err)
continue
}
chatId, err := rpc.GetChatIdByContactId(accId, contactId)
if err != nil {
logger.Error(err)
continue
}
if chatId == 0 {
continue
}
contact, err := rpc.GetContact(accId, contactId)
if err != nil {
logger.Error(err)
continue
}
if time.Since(contact.LastSeen.Time).Minutes() < 30 {
// bot is not offline, it will be checked by the online-bots status loop
continue
}
logger.Debug("checking bot status")
if err := pingBot(rpc, accId, contactId, bot.Url); err != nil {
logger.Error(err)
}
}
}
}

func updateStatusLoop(rpc *deltachat.Rpc) {
Expand Down Expand Up @@ -96,33 +149,32 @@ func updateStatusLoop(rpc *deltachat.Rpc) {
logger.Error(err)
continue
}
lastSeen := int(math.Round(time.Since(contact.LastSeen.Time).Hours()))
if lastSeen >= 1 && lastSeen%2 != 0 {
logger.Debug("skipping status check for offline bot")
if time.Since(contact.LastSeen.Time).Minutes() >= 30 {
// offline bot, will be check by the offline-bots status loop
continue
}
}
logger.Debug("checking bot status")
if strings.HasPrefix(strings.ToLower(bot.Url), "openpgp4fpr:") {
_, err := rpc.SecureJoin(accId, bot.Url)
if err != nil {
logger.Error(err)
}
} else {
chatId, err := rpc.CreateChatByContactId(accId, contactId)
if err != nil {
logger.Error(err)
continue
}
_, err = rpc.MiscSendTextMessage(accId, chatId, "/help")
if err != nil {
logger.Error(err)
}
if err := pingBot(rpc, accId, contactId, bot.Url); err != nil {
logger.Error(err)
}
}
}
}

func pingBot(rpc *deltachat.Rpc, accId deltachat.AccountId, contactId deltachat.ContactId, botUrl string) error {
if strings.HasPrefix(strings.ToLower(botUrl), "openpgp4fpr:") {
_, err := rpc.SecureJoin(accId, botUrl)
return err
}
chatId, err := rpc.CreateChatByContactId(accId, contactId)
if err != nil {
return err
}
_, err = rpc.MiscSendTextMessage(accId, chatId, "/help")
return err
}

func updateBotsLoop() {
url := "https://github.com/deltachat-bot/public-bots/raw/main/frontend/data.json"
logger := cli.Logger.With("origin", "metadata-loop")
Expand Down

0 comments on commit 2600a9f

Please sign in to comment.