Skip to content

Commit

Permalink
add telegram reward notification bot
Browse files Browse the repository at this point in the history
  • Loading branch information
0pcom committed May 24, 2024
1 parent d825311 commit 7067771
Show file tree
Hide file tree
Showing 38 changed files with 8,728 additions and 1 deletion.
117 changes: 117 additions & 0 deletions cmd/skywire-cli/commands/rewards/tgbot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Package clirewards cmd/skywire-cli/commands/rewards/tgbot.go
package clirewards

import (
"fmt"
"log"
"os"
"strconv"
"strings"
"time"

"github.com/bitfield/script"
"github.com/spf13/cobra"
tele "gopkg.in/telebot.v3"
)

var filePath string

func init() {
RootCmd.AddCommand(
tgbotCmd,
)
tgbotCmd.Flags().StringVarP(&filePath, "watch", "w", "../reward/rewards/transactions0.txt", "File to watch - file where reward transaction IDs are recorded")

}

var tgbotCmd = &cobra.Command{
Use: "bot",
Short: "reward notification telegram bot",
Long: "reward notification telegram bot",
Run: func(_ *cobra.Command, _ []string) {
chatIDStr := os.Getenv("TG_CHAT_ID")
chatID, err := strconv.ParseInt(chatIDStr, 10, 64)
if err != nil {
log.Fatalf("failed to parse chat ID: %v", err)
}
pref := tele.Settings{
Token: os.Getenv("TG_BOT_TOKEN"),
Poller: &tele.LongPoller{Timeout: 10 * time.Second},
}

b, err := tele.NewBot(pref)
if err != nil {
log.Fatal(err)
return
}

tgbotscript := `#!/bin/bash
_stats() {
# [[ $1 == "" ]] && return
cat ` + strings.TrimSuffix(filePath, "/transactions0.txt") + `/hist/$(find ` + strings.TrimSuffix(filePath, "/transactions0.txt") + `/hist/ -name "*.txt" -type f -exec grep -l "$1" {} + | xargs -I{} basename {} | tr -d ".txt")_stats.txt
}
`

// var lastModTime time.Time
lastModTime, err := os.Stat(filePath)
if err != nil {
log.Fatal(err)
return
}
// Use a goroutine to periodically check the file for changes
go func() {
for {
time.Sleep(2 * time.Second)

fileInfo, err := os.Stat(filePath)
if err != nil {
log.Printf("Error checking file info: %s", err)
continue
}

if fileInfo.ModTime().After(lastModTime.ModTime()) {
// The file has been modified since the last check, get the last line which is the most recent txid
lastLine, err := script.File(filePath).Last(1).String()
if err != nil {
log.Printf("Error getting last line of file: %v", err)
continue
}
if lastLine != "" {
tmpFile, err := os.CreateTemp(os.TempDir(), "*.sh")
if err != nil {
return
}
if err := tmpFile.Close(); err != nil {
return
}
_, _ = script.Exec(`chmod +x ` + tmpFile.Name()).String() //nolint
_, _ = script.Echo(tgbotscript).WriteFile(tmpFile.Name()) //nolint
stats, err := script.Exec(`bash -c 'source ` + tmpFile.Name() + ` ; _stats ` + lastLine + `'`).String() //nolint
if err != nil {
log.Printf("Error getting statistics: %v", err)
continue
}
os.Remove(tmpFile.Name()) //nolint

dateforlink, err := script.Echo(stats).First(1).Replace("date: ", "").String()
if err != nil {
log.Printf("Error getting date for link: %v", err)
continue
}
msg := fmt.Sprintf("Rewards have been distributed!\n\nhttps://explorer.skycoin.com/app/transaction/%s\n\n%s\n\nhttps://fiber.skywire.dev/skycoin-rewards/hist/%s", lastLine, stats, dateforlink)
// Send the last line to the Telegram chat
_, err = b.Send(&tele.Chat{ID: chatID}, msg)
if err != nil {
log.Printf("Error sending message to Telegram chat: %s", err)
continue
}
}

lastModTime = fileInfo
}
}
}()

b.Start()
},
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ require (
golang.org/x/sync v0.3.0
golang.org/x/sys v0.17.0
golang.zx2c4.com/wireguard v0.0.0-20230223181233-21636207a675
gopkg.in/telebot.v3 v3.2.1
)

require (
Expand Down
640 changes: 639 additions & 1 deletion go.sum

Large diffs are not rendered by default.

34 changes: 34 additions & 0 deletions vendor/gopkg.in/telebot.v3/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions vendor/gopkg.in/telebot.v3/LICENSE

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7067771

Please sign in to comment.