Skip to content

Commit

Permalink
refactored flags invocation & updated README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
RSheremeta committed Apr 12, 2023
1 parent c48b86e commit 1bf0a0d
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 34 deletions.
11 changes: 9 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@

token := $(shell echo $(TG_TOKEN))

.PHONY start:
go build && ./read-adviser-bot -tg-bot-token '${token}'
start:
go build && ./read-adviser-bot -tg_bot_token '${token}'

start_sqlite:
go build && ./read-adviser-bot -tg_bot_token '${token}' -storage_type 'sqlite'

start_files:
go build && ./read-adviser-bot -tg_bot_token '${token}' -storage_type 'files'

.PHONY: start start_sqlite start_files
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ If you make the Bot to pick any link for you, this link will be removed from the
### Usage
1. Get (or retrieve created) the Telegram Bot token value.
2. Set it as an env variable called `TG_TOKEN`.
3. In order to run the Telegram Bot service, just do the ```make start``` command in terminal
3. There are 3 ways in order to run the Telegram Bot service:
- ```make start-sqlite``` command in terminal (uses sqlite as a storage source)
- ```make start-files``` command in terminal (uses Gob encoded in files as a storage source)
- just ```make start``` (uses **sqlite** by default)

Hint: the `make` builds the binary and runs the service whenever you invoke it, but you can also use a pre-built one [here](https://github.com/RSheremeta/read-adviser-bot/tags)
Hint: the `make` commands do build the binary and run the service whenever you invoke any of it, but you can also use a pre-built one [here](https://github.com/RSheremeta/read-adviser-bot/tags)
78 changes: 48 additions & 30 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,76 @@ package main
import (
"context"
"flag"
"fmt"
"log"

client "github.com/RSheremeta/read-adviser-bot/clients/telegram"
"github.com/RSheremeta/read-adviser-bot/consumer/eventconsumer"
"github.com/RSheremeta/read-adviser-bot/events/telegram"
"github.com/RSheremeta/read-adviser-bot/storage"

// "github.com/RSheremeta/read-adviser-bot/storage/files"
"github.com/RSheremeta/read-adviser-bot/storage/files"
"github.com/RSheremeta/read-adviser-bot/storage/sqlite"
)

const (
tokenFlagName = "tg-bot-token"
tokenFlagName = "tg_bot_token"
tgBotHost = "api.telegram.org"
batchSize = 100
)

const (
storagePath = "files_storage"
storageSqlPath = "data/sqlite/storage.db"
filesStoragePath = "files_storage"
sqlStoragePath = "data/sqlite/storage.db"
storageTypeFlagName = "storage_type"
sqliteStorageOpt = "sqlite"
filesStorageOpt = "files"
)

func main() {
token := mustToken()
var token, storageType string

tgClient := client.New(tgBotHost, token)
flag.StringVar(
&token,
tokenFlagName,
"",
"token for the Telegram bot accessing",
)

// an alternative option to use - a file storage via Gob - if use this, change storage variable passing into tg.new() func
// storage := files.New(storagePath)
// or
// sqlite storage
storage, err := sqlite.New(storageSqlPath)
if err != nil {
log.Fatal("cannot connect to the storage: ", err)
flag.StringVar(
&storageType,
storageTypeFlagName,
"",
fmt.Sprintf("type of storage for the links usage. the options are %q or %q. and %q is by default",
sqliteStorageOpt, filesStorageOpt, sqliteStorageOpt),
)

if storageType == "" {
storageType = sqliteStorageOpt
}
flag.Parse()

if err = storage.Init(context.TODO()); err != nil {
log.Fatal("cannot init the storage: ", err)
if token == "" {
log.Fatal("the token value is not specified")
}

tgClient := client.New(tgBotHost, token)

var storage storage.Storage

if storageType == filesStorageOpt {
storage = files.New(filesStoragePath)
log.Println("initializing storage of files type")
} else if storageType == sqliteStorageOpt {
storage, err := sqlite.New(sqlStoragePath)
if err != nil {
log.Fatal("cannot connect to the sqlite storage: ", err)
}

if err = storage.Init(context.TODO()); err != nil {
log.Fatal("cannot init the sqlite storage: ", err)
}
log.Println("initializing storage of sqlite type")
}

eventsProcessor := telegram.New(tgClient, storage)
Expand All @@ -52,18 +85,3 @@ func main() {
log.Fatalf("service stopped due to the error: %s", err.Error())
}
}

func mustToken() string {
t := flag.String(
tokenFlagName,
"",
"token for the Telegram bot accessing",
)
flag.Parse()

if *t == "" {
log.Fatal("the token value is not specified")
}

return *t
}
Binary file modified read-adviser-bot
Binary file not shown.

0 comments on commit 1bf0a0d

Please sign in to comment.