Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
samarets committed Jul 23, 2022
1 parent c1a6eba commit abdb550
Show file tree
Hide file tree
Showing 12 changed files with 1,186 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
BOT_TOKEN=
NOTIFICATIONS_CHAT_ID=
10 changes: 4 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
# Binaries for programs and plugins
.env
.db

*.exe
*.exe~
*.dll
*.so
*.dylib
.DS_Store

.idea

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Env
.env
33 changes: 33 additions & 0 deletions cmd/bot/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package main

import (
"github.com/samarets/support-bot/cmd"
"github.com/samarets/support-bot/internal/bot"
"github.com/samarets/support-bot/internal/db"
"github.com/samarets/support-bot/internal/log"
)

func main() {
cfg, err := cmd.LoadConfig(".", ".env", "env")
if err != nil {
log.Error().Err(err).Send()
panic(err)
}

database, err := db.InitDB()
if err != nil {
panic(err)
}

defer func(database *db.DB) {
err := database.Close()
if err != nil {
panic(err)
}
}(database)

err = bot.InitBot(cfg.TelegramLoggerBotToken, database)
if err != nil {
panic(err)
}
}
35 changes: 35 additions & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package cmd

import (
"fmt"

"github.com/samarets/support-bot/internal/log"
"github.com/spf13/viper"
)

type Config struct {
TelegramLoggerBotToken string `mapstructure:"bot_token"`
TelegramLoggerBotChatID int64 `mapstructure:"notifications_chat_id"`
}

func LoadConfig(path, configFile, configType string) (config Config, err error) {
viper.AddConfigPath(path)
viper.SetConfigName(configFile)
viper.SetConfigType(configType)

viper.SetDefault("bot_token", "")
viper.SetDefault("notifications_chat_id", 0)

viper.AutomaticEnv()

if err = viper.ReadInConfig(); err != nil {
fmt.Println(err)
log.Warn().Msg("used default configs")
} else {
log.Info().Msgf("loaded config from %s", configType)
}

err = viper.Unmarshal(&config)

return config, err
}
Empty file added docker-compose.yml
Empty file.
46 changes: 46 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module github.com/samarets/support-bot

go 1.18

require (
github.com/dgraph-io/badger/v3 v3.2103.2
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
github.com/rs/zerolog v1.27.0
github.com/spf13/viper v1.12.0
)

require (
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cespare/xxhash/v2 v2.1.1 // indirect
github.com/dgraph-io/ristretto v0.1.0 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.3 // indirect
github.com/google/flatbuffers v1.12.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/klauspost/compress v1.12.3 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.3.0 // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0 // indirect
)
561 changes: 561 additions & 0 deletions go.sum

Large diffs are not rendered by default.

167 changes: 167 additions & 0 deletions internal/bot/bot.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package bot

import (
"fmt"

"github.com/dgraph-io/badger/v3"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/samarets/support-bot/internal/db"
"github.com/samarets/support-bot/internal/log"
)

type bot struct {
bot *tgbotapi.BotAPI
db *db.DB
}

func InitBot(token string, db *db.DB) error {
botAPI, err := tgbotapi.NewBotAPI(token)
if err != nil {
return err
}

botState := &bot{
bot: botAPI,
db: db,
}
botState.InitUpdates()

return nil
}

func (b *bot) InitUpdates() {
u := tgbotapi.NewUpdate(0)
u.Timeout = 60

updates := b.bot.GetUpdatesChan(u)

for update := range updates {
if update.Message != nil {
if update.Message.IsCommand() {
switch update.Message.Command() {
case "start":
b.StartCommand(update.Message)
case "connect":
b.ConnectCommand(update)
case "break":
b.BreakCommand(update)

}

continue
}

var fromQueue tgbotapi.User
err := b.db.Get(mergePrefixDB(queue, update.Message.From.ID), &fromQueue)
if err != nil && err != badger.ErrKeyNotFound {
log.Error().Err(err).Send()
continue
}

var roomsID int64
err = b.db.Get(mergePrefixDB(rooms, update.Message.From.ID), &roomsID)
if err != nil && err != badger.ErrKeyNotFound {
log.Error().Err(err).Send()
continue
}

defaultUser := tgbotapi.User{}
if fromQueue == defaultUser && roomsID == 0 {
err = b.db.Set(mergePrefixDB(queue, update.Message.From.ID), update.Message.From)
if err != nil {
log.Error().Err(err).Send()
continue
}

var bufferMessages []tgbotapi.Message
bufferMessages = append(bufferMessages, *update.Message)

err = b.db.Set(mergePrefixDB(buffer, update.Message.From.ID), bufferMessages)
if err != nil {
log.Error().Err(err).Send()
continue
}

msg := tgbotapi.NewMessage(
update.Message.Chat.ID,
"🤖 Ваше повідомлення було отримано, якщо у вас є що додати - напишіть.\n\nСкоро до вас доєднається оператор технічної підтримки",
)
_, err = b.bot.Send(msg)
if err != nil {
log.Error().Err(err).Send()
continue
}

fmt.Println(update.Message.Chat.ID, "create request")
continue
}
if fromQueue != defaultUser && roomsID == 0 {
var bufferMessages []tgbotapi.Message
err = b.db.Get(mergePrefixDB(buffer, update.Message.From.ID), &bufferMessages)
if err != nil && err != badger.ErrKeyNotFound {
log.Error().Err(err).Send()
continue
}

bufferMessages = append(bufferMessages, *update.Message)

err = b.db.Set(mergePrefixDB(buffer, update.Message.From.ID), bufferMessages)
if err != nil {
log.Error().Err(err).Send()
continue
}

continue
}
if roomsID != 0 {
var whoomSend int64
err = b.db.Get(mergePrefixDB(rooms, update.Message.From.ID), &whoomSend)
if err != nil && err != badger.ErrKeyNotFound {
log.Error().Err(err).Send()
continue
}

msg := tgbotapi.NewCopyMessage(whoomSend, update.Message.Chat.ID, update.Message.MessageID)
if update.Message.ReplyToMessage != nil {
var replyToID int
err = b.db.Get(mergePrefixDB(messagesIDs, update.Message.ReplyToMessage.MessageID), &replyToID)
if err != nil && err != badger.ErrKeyNotFound {
log.Error().Err(err).Send()
continue
}

if replyToID != 0 {
fmt.Println(replyToID)
msg.ReplyToMessageID = replyToID
}
}

rMsg, err := b.bot.Send(msg)
if err != nil {
log.Error().Err(err).Send()
continue
}

err = b.db.Set(mergePrefixDB(messagesIDs, update.Message.MessageID), rMsg.MessageID)
if err != nil {
log.Error().Err(err).Send()
continue
}

err = b.db.Set(mergePrefixDB(messagesIDs, rMsg.MessageID), update.Message.MessageID)
if err != nil {
log.Error().Err(err).Send()
continue
}

fmt.Println(update.Message.Chat.ID, "send msg")
continue
}

}
}
}

func mergePrefixDB(prefix string, id interface{}) []byte {
return []byte(fmt.Sprintf("%s-%d", prefix, id))
}
Loading

0 comments on commit abdb550

Please sign in to comment.