Skip to content

Commit

Permalink
update sraketa command, introduce yaml config
Browse files Browse the repository at this point in the history
  • Loading branch information
mcahriman committed Aug 27, 2022
1 parent 14a55a4 commit 87c99fc
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 53 deletions.
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# srakabot2

Running:
it requires API Key to be set in the config file or env variable.
TODO: Add docs
## Configuring

Bot congiration goes in yaml file of the next format

telegram:
api_token: YOUR:TELEGRAM_BOT_APIKEY_HERE
couchdb:
host: http://your.couchdb.host:5984/
user: username
password: password
database: database_name

put this as .srakabot.yml to the home directory or use

``` -config /path/to/yaml ``` command line argument to specify location
32 changes: 32 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package main

import (
"os"

"gopkg.in/yaml.v3"
)

type Config struct {
Telegram struct {
ApiToken string `yaml:"api_token"`
}
CouchDB struct {
Host string
User string
Password string
Database string
}
}

func getConfig(filename string) (config *Config, err error) {
data, err := os.ReadFile(filename)
if err != nil {
return
}
config = new(Config)
err = yaml.Unmarshal(data, config)
if err != nil {
return
}
return
}
6 changes: 3 additions & 3 deletions database.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@ import (
var cbInstance *kivik.Client

func cbConnect() {
client, err := kivik.New("couch", "http://localhost:5984/")
client, err := kivik.New("couch", appConfig.CouchDB.Host)
if err != nil {
panic("Could not connect to database")
}
err = client.Authenticate(context.TODO(), couchdb.BasicAuth("admin", "admin"))
err = client.Authenticate(context.TODO(), couchdb.BasicAuth(appConfig.CouchDB.User, appConfig.CouchDB.Password))
if err != nil {
panic(err)
}
cbInstance = client
}

func getUpdatesDB() *kivik.DB {
return cbInstance.DB("srakabot_db")
return cbInstance.DB(appConfig.CouchDB.Database)
}

// func getUserMetadataDB() *kivik.DB {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ require (
gopkg.in/couchbaselabs/gocbconnstr.v1 v1.0.4 // indirect
gopkg.in/couchbaselabs/jsonx.v1 v1.0.0 // indirect
gopkg.in/square/go-jose.v2 v2.6.0 // indirect
gopkg.in/yaml.v3 v3.0.1
)
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,10 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
17 changes: 7 additions & 10 deletions messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
_ "embed"
"fmt"
"log"
"regexp"
"strconv"
"strings"
"time"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
Expand All @@ -23,13 +23,6 @@ type ChatMessage struct {
Message tgbotapi.Message `json:"messageDetails"`
}

var AllowedCommands = []string{
"/stats",
"/showRanksTest",
"/rankDiag",
"/bledina",
}

func processUpdate(bot *tgbotapi.BotAPI, update tgbotapi.Update) {

db := getUpdatesDB()
Expand Down Expand Up @@ -62,7 +55,8 @@ func processUpdate(bot *tgbotapi.BotAPI, update tgbotapi.Update) {

func processMessage(bot *tgbotapi.BotAPI, message tgbotapi.Message) {

messageChunks := strings.Split(message.Text, " ")
re := regexp.MustCompile(`[\s@]+`)
messageChunks := re.Split(message.Text, 5)

if len(messageChunks) >= 1 && stringInSlice(messageChunks[0], AllowedCommands) {
switch messageChunks[0] {
Expand Down Expand Up @@ -141,7 +135,10 @@ func processMessage(bot *tgbotapi.BotAPI, message tgbotapi.Message) {
}
case "/bledina":
go func() {
sraketa()
if !sraketa() {
log.Printf("Could not take screenshot")
return
}
responseConfig := tgbotapi.NewPhoto(message.Chat.ID, tgbotapi.FilePath("sraketa_current.png"))
bot.Send(responseConfig)
}()
Expand Down
38 changes: 21 additions & 17 deletions srakabot.go
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
package main

import (
"flag"
"log"
"os"
"strings"

tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/playwright-community/playwright-go"
)

var appConfig *Config

var AllowedCommands = []string{
"/stats",
"/showRanksTest",
"/rankDiag",
"/bledina",
}

func main() {
//connect to touchbase
cbConnect()
err := playwright.Install()

var err error
configFileLocationPtr := flag.String("config", os.Getenv("HOME")+"/.srakabot.yml", "Config file location")

appConfig, err = getConfig(*configFileLocationPtr)
if err != nil {
log.Printf("Could not install playwright")
os.Exit(1)
log.Fatalf("Could not load configuration")
}

token := os.Getenv("TGBOT_API_TOKEN")
file_location := os.Getenv("HOME") + "/.srakabot_token"
if token == "" {
contents, err := os.ReadFile(file_location)
if err == nil {
token = strings.Trim(string(contents), "\n")
} else {
log.Printf("%s could not be read and environment variable TG_BOT_API_TOKEN is not defined", file_location)
}
//connect to touchbase
cbConnect()
if !sraketa_init() {
log.Printf("Could not initialize alerts")
}

bot, err := tgbotapi.NewBotAPI(token)
bot, err := tgbotapi.NewBotAPI(appConfig.Telegram.ApiToken)
if err != nil {
os.Exit(1)
}
Expand Down
69 changes: 49 additions & 20 deletions sraketa.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,64 @@ import (
"github.com/playwright-community/playwright-go"
)

func sraketa() {
pw, err := playwright.Run()
if err != nil {
log.Fatalf("could not start playwright: %v", err)
var loadedPage playwright.Page
var playwrightError error
var playwrightInstance *playwright.Playwright
var playwrightBrowser playwright.Browser

func sraketa_init() bool {
playwrightError = playwright.Install()
if playwrightError != nil {
log.Printf("Could not install playwright")
return false
}

playwrightInstance, playwrightError = playwright.Run()
if playwrightError != nil {
return false
}
browser, err := pw.Firefox.Launch()
if err != nil {
log.Fatalf("could not launch browser: %v", err)

playwrightBrowser, playwrightError = playwrightInstance.Firefox.Launch()
if playwrightError != nil {
return false
}
page, err := browser.NewPage()
if err != nil {
log.Fatalf("could not create page: %v", err)

loadedPage, playwrightError = playwrightBrowser.NewPage()

if playwrightError != nil {
return false
}
if _, err = page.Goto("https://alerts.in.ua", playwright.PageGotoOptions{

if _, playwrightError = loadedPage.Goto("https://alerts.in.ua", playwright.PageGotoOptions{
WaitUntil: playwright.WaitUntilStateNetworkidle,
}); err != nil {
log.Fatalf("could not goto: %v", err)
}); playwrightError != nil {
return false
}
return true

}

if _, err = page.Screenshot(playwright.PageScreenshotOptions{
Path: playwright.String("sraketa_current.png"),
}); err != nil {
log.Fatalf("could not create screenshot: %v", err)
func sraketa() bool {
if playwrightError == nil {

if _, err := loadedPage.Screenshot(playwright.PageScreenshotOptions{
Path: playwright.String("sraketa_current.png"),
}); err != nil {
log.Printf("could not create screenshot: %v", err)
return false
}
return true
} else {
return false
}

if err = browser.Close(); err != nil {
log.Fatalf("could not close browser: %v", err)
}

func sraketa_shutdown() {
if err := playwrightBrowser.Close(); err != nil {
log.Printf("could not close browser: %v", err)
}
if err = pw.Stop(); err != nil {
if err := playwrightInstance.Stop(); err != nil {
log.Fatalf("could not stop Playwright: %v", err)
}

Expand Down

0 comments on commit 87c99fc

Please sign in to comment.