Skip to content

Commit

Permalink
Add Message Handler
Browse files Browse the repository at this point in the history
  • Loading branch information
taras-by committed Dec 25, 2019
1 parent 2e422c1 commit c060609
Show file tree
Hide file tree
Showing 6 changed files with 398 additions and 378 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ The last example is the removal of the third participant

## Run manually

sudo -u tbot env $(sudo cat /var/lib/tbot/environment | xargs) tbot server
sudo -u tbot env $(sudo cat /var/lib/tbot/environment | xargs) tbot run

## Run as service
Create config file:
Expand All @@ -51,7 +51,7 @@ tbotd.service file:
User = tbot
Group = tbot
EnvironmentFile = -/var/lib/tbot/environment
ExecStart = /usr/bin/tbot server
ExecStart = /usr/bin/tbot run
PIDFile = /var/run/tbotd.pid
Restart = always
RestartSec = 60
Expand Down
30 changes: 19 additions & 11 deletions app.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import (
"fmt"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
srv "github.com/taras-by/tbot/server"
"github.com/taras-by/tbot/store"
tlg "github.com/taras-by/tbot/telegram"
"log"
"runtime"
)
Expand All @@ -19,7 +19,6 @@ type app struct {
commit string
date string
version string
bot *tgbotapi.BotAPI
storage *store.Storage
}

Expand All @@ -31,15 +30,10 @@ func newApp() (a *app) {
date: Date,
version: Version,
}
a.printVersion()

var err error

a.bot, err = tgbotapi.NewBotAPI(a.options.TelegramToken)
if err != nil {
log.Printf("Telegram connection Error. Token: %s", a.options.TelegramToken)
log.Panic(err.Error())
}

a.storage, err = store.NewStorage(a.options.StorePath)
if err != nil {
log.Printf("storage creating error. Path: %s", a.options.StorePath)
Expand All @@ -49,12 +43,26 @@ func newApp() (a *app) {
return a
}

func (a *app) newServer() (s *srv.Server) {
return &srv.Server{
Bot: a.bot,
func (a *app) makeBotService() (s *tlg.BotService) {

bot, err := tgbotapi.NewBotAPI(a.options.TelegramToken)
if err != nil {
log.Printf("Telegram connection Error. Token: %s", a.options.TelegramToken)
log.Panic(err.Error())
}
log.Printf("Authorized on account %s", bot.Self.UserName)

handler := &tlg.MessageHandler{
Bot: bot,
Storage: a.storage,
Version: a.version,
}
handler.Init()

return &tlg.BotService{
Bot: bot,
Handler: handler,
}
}

func (a app) printVersion() {
Expand Down
Loading

0 comments on commit c060609

Please sign in to comment.