-
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
Showing
5 changed files
with
145 additions
and
1 deletion.
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,4 @@ | ||
TELEGRAM_BOT_TOKEN=<YOUR_BOT_TOKEN> | ||
TELEGRAM_CHANNEL=@viz_news | ||
VIZ_NODE=https://node.viz.cx | ||
DB_PATH=database |
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 |
---|---|---|
@@ -1,3 +1,9 @@ | ||
.env | ||
.vscode/ | ||
database/ | ||
__debug_bin | ||
viz-news-bot | ||
|
||
# Binaries for programs and plugins | ||
*.exe | ||
*.exe~ | ||
|
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 |
---|---|---|
@@ -1 +1,3 @@ | ||
# viz-news | ||
# VIZ News bot | ||
|
||
Script for tracking telegram channels which added VIZ social bot, then publishing this list to telegram channel. |
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,2 @@ | ||
#!/bin/bash | ||
GOOS=linux GOARCH=amd64 go build |
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,130 @@ | ||
package main | ||
|
||
import ( | ||
"html" | ||
"log" | ||
"math/rand" | ||
"os" | ||
"regexp" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/VIZ-Blockchain/viz-go-lib" | ||
"github.com/VIZ-Blockchain/viz-go-lib/operations" | ||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api" | ||
_ "github.com/joho/godotenv/autoload" | ||
"github.com/syndtr/goleveldb/leveldb" | ||
) | ||
|
||
func main() { | ||
db, err := leveldb.OpenFile(os.Getenv("DB_PATH"), nil) | ||
defer db.Close() | ||
|
||
bot, err := tgbotapi.NewBotAPI(os.Getenv("TELEGRAM_BOT_TOKEN")) | ||
if err != nil { | ||
log.Panic(err) | ||
} | ||
|
||
err = start(db, bot) | ||
if err != nil { | ||
log.Panic(err) | ||
} | ||
} | ||
|
||
func start(db *leveldb.DB, bot *tgbotapi.BotAPI) error { | ||
cls, _ := viz.NewClient(os.Getenv("VIZ_NODE")) | ||
defer cls.Close() | ||
|
||
config, err := cls.API.GetConfig() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
props, err := cls.API.GetDynamicGlobalProperties() | ||
if err != nil { | ||
return err | ||
} | ||
lastBlock := props.LastIrreversibleBlockNum | ||
|
||
log.Printf("---> Entering the block processing loop (last block = %v)\n", lastBlock) | ||
for { | ||
props, err := cls.API.GetDynamicGlobalProperties() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
for props.LastIrreversibleBlockNum-lastBlock > 0 { | ||
block, err := cls.API.GetBlock(lastBlock) | ||
if err != nil { | ||
return err | ||
} | ||
log.Printf("Received block %v", block.Number) | ||
|
||
for _, tx := range block.Transactions { | ||
for _, operation := range tx.Operations { | ||
switch op := operation.Data().(type) { | ||
case *operations.AwardOperation: | ||
channel := getChannel(op.Memo) | ||
if channel != "" { | ||
err = saveChannel(db, bot, channel) | ||
if err != nil { | ||
log.Println(err) | ||
} | ||
} | ||
// case *operations.UnknownOperation: | ||
// log.Printf("Unkowned operation receivded: %v+\n", op) | ||
} | ||
} | ||
} | ||
|
||
lastBlock++ | ||
} | ||
|
||
time.Sleep(time.Duration(config.BlockInterval) * time.Second) | ||
} | ||
} | ||
|
||
func getChannel(str string) string { | ||
var re = regexp.MustCompile(`(?m)channel:(@[a-z0-9_]+):`) | ||
var arr = re.FindStringSubmatch(str) | ||
if len(arr) > 1 { | ||
return arr[1] | ||
} | ||
return "" | ||
} | ||
|
||
func saveChannel(db *leveldb.DB, bot *tgbotapi.BotAPI, channel string) error { | ||
data, _ := db.Get([]byte(channel), nil) | ||
if data != nil { | ||
return nil // already saved | ||
} | ||
err := db.Put([]byte(channel), []byte("true"), nil) | ||
if err != nil { | ||
return err | ||
} | ||
c, err := bot.GetChat(tgbotapi.ChatConfig{SuperGroupUsername: channel}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
text := randomEmoji() + " Новый #канал с ботом: \n\n" + c.Title + " — @" + c.UserName + "\n\n *** \n\n" + c.Description + "\n\n ***" | ||
msg := tgbotapi.NewMessageToChannel(os.Getenv("TELEGRAM_CHANNEL"), text) | ||
_, err = bot.Send(msg) | ||
return err | ||
} | ||
|
||
func randomEmoji() string { | ||
rand.Seed(time.Now().UnixNano()) | ||
// http://apps.timwhitlock.info/emoji/tables/unicode | ||
emoji := [][]int{ | ||
// Emoticons icons | ||
{128513, 128591}, | ||
// Transport and map symbols | ||
{128640, 128704}, | ||
} | ||
r := emoji[rand.Int()%len(emoji)] | ||
min := r[0] | ||
max := r[1] | ||
n := rand.Intn(max-min+1) + min | ||
return html.UnescapeString("&#" + strconv.Itoa(n) + ";") | ||
} |