Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM golang:1.18.3 as builder
LABEL stage=tgbotbuilder
WORKDIR /src

COPY go.mod .
COPY go.sum .
RUN go mod download

COPY . .

RUN make build
RUN mkdir -p /etc/tgbot && mkdir /data

FROM scratch
COPY --from=builder /app /app
ADD folders.tar.gz /

ENTRYPOINT [ "/app" ]
13 changes: 13 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
.PHONY: default build image run

default: build

build:
CGO_ENABLED=1 GOOS=linux go build -o /app -a -ldflags '-linkmode external -extldflags "-static"' cmd/main.go

image:
docker build -t skillbot:latest .
docker image prune -f --filter label=stage=tgbotbuilder

run:
go run cmd/main.go
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
# tg-bot-users 🤖

# Список команд:

89 changes: 89 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package main

import (
"flag"
"log"
"skbot/internal/callbackmsg"
"skbot/internal/chatmembers"
"skbot/internal/comandmsg"
"skbot/internal/config"
"skbot/internal/functions"
"skbot/internal/socialmsg"
"skbot/internal/textmsg"
"skbot/pkg/client/telegram"
"skbot/pkg/logging"
)

var cfgPath string

func init() {
flag.StringVar(&cfgPath, "config", "tg-bot-users/conf.yml", "config file path")
}

func main() {

log.Print("config initializing")
cfg := config.GetConfig(cfgPath)

log.Print("logger initializing")
logger := logging.GetLogger(cfg.AppConfig.LogLevel)

modGroupId := cfg.ModersGroupID.ModeratorsGroup
_, _, err := functions.AddModeratorsGroup(cfg.ModersGroupID.ModeratorsGroup)
_, _, err = functions.AddModeratorsGroup(cfg.ModersGroupID.ModeratorsGroupGolang)
_, _, err = functions.AddModeratorsGroup(cfg.ModersGroupID.ModeratorsGroupJava)
_, _, err = functions.AddModeratorsGroup(cfg.ModersGroupID.ModeratorsGroupPython)
_, _, err = functions.AddModeratorsGroup(cfg.ModersGroupID.ModeratorsGroup1S)
_, _, err = functions.AddModeratorsGroup(cfg.ModersGroupID.ModeratorsGroupCSharp)
if err != nil {
logger.Info(err)
}

updChan, bot, err := telegram.StartBotByChan(cfg, logger)
if err != nil {
logger.Fatal(err)
}
defer bot.StopReceivingUpdates()

for {

update := <-updChan

// make slice from text message
//if update.Message != nil {
// command := strings.Split(update.Message.Text, " ")
//} else {
// continue
//}
if update.Message != nil {

// text messages operations
textmsg.WithTextQueryDo(update, bot, logger, modGroupId)

// social messages from bot in chat
socialmsg.WithSocialTextQueryDo(update, bot, logger)
} else if update.CallbackQuery != nil {

callbackmsg.WithCallBackDo(update, bot, logger, modGroupId)

} else if update.Message.Command() != "" {

//com menu (only moderator's chats)
comandmsg.CommandQueryDo(update, bot, logger)
} else if update.Message.NewChatMembers != nil {

chatmembers.WithChatMembersDo(update, bot, logger)
}

// TODO inline help
if update.InlineQuery != nil {
log.Println("response from Inline query")
query := update.InlineQuery.Query

log.Println(query)

}

}

}
23 changes: 23 additions & 0 deletions conf.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---


telegram:
tg_token:
tg_sert:



app:
log_level: trace

chat_count_config:
test_chat_id_count:
chat_id_count:

moderators:
moderators_group:
moderators_group_golang:
moderators_group_java:
moderators_group_python:
moderators_group_1s:
moderators_group_csharp:
Binary file added folders.tar.gz
Binary file not shown.
18 changes: 18 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module skbot

go 1.18

require (
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1
github.com/ilyakaznacheev/cleanenv v1.3.0
github.com/mattn/go-sqlite3 v1.14.14
github.com/sirupsen/logrus v1.8.1
)

require (
github.com/BurntSushi/toml v1.1.0 // indirect
github.com/joho/godotenv v1.4.0 // indirect
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 // indirect
)
26 changes: 26 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
github.com/BurntSushi/toml v1.1.0 h1:ksErzDEI1khOiGPgpwuI7x2ebx/uXQNw7xJpn9Eq1+I=
github.com/BurntSushi/toml v1.1.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 h1:wG8n/XJQ07TmjbITcGiUaOtXxdrINDz1b0J1w0SzqDc=
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1/go.mod h1:A2S0CWkNylc2phvKXWBBdD3K0iGnDBGbzRpISP2zBl8=
github.com/ilyakaznacheev/cleanenv v1.3.0 h1:RapuLclPPUbmdd5Bi5UXScwMEZA6+ZNLU5OW9itPjj0=
github.com/ilyakaznacheev/cleanenv v1.3.0/go.mod h1:i0owW+HDxeGKE0/JPREJOdSCPIyOnmh6C0xhWAkF/xA=
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/mattn/go-sqlite3 v1.14.14 h1:qZgc/Rwetq+MtyE18WhzjokPD93dNqLGNT3QJuLvBGw=
github.com/mattn/go-sqlite3 v1.14.14/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 h1:slmdOY3vp8a7KQbHkL+FLbvbkgMqmXojpFUO/jENuqQ=
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3/go.mod h1:oVgVk4OWVDi43qWBEyGhXgYxt7+ED4iYNpTngSLX2Iw=
83 changes: 83 additions & 0 deletions internal/callbackmsg/callbackmsg.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package callbackmsg

import (
"fmt"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"skbot/internal/config"
"skbot/internal/functions"
"skbot/internal/menu"
"skbot/pkg/logging"
"time"
)

func WithCallBackDo(update tgbotapi.Update, bot *tgbotapi.BotAPI, logger *logging.Logger, modGroupId int64) {

data := update.CallbackQuery.Data

switch data {

case "com_list":

msg := tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID, menu.ComMenu)
msg.ParseMode = "markdown"
delMsg, _ := bot.Send(msg)

callback := tgbotapi.NewCallback(update.CallbackQuery.ID, "✅")
if _, err := bot.Request(callback); err != nil {
logger.Error(err)
}

go func() {
time.Sleep(30 * time.Second)
_, _ = bot.Send(tgbotapi.NewDeleteMessage(update.CallbackQuery.Message.Chat.ID, delMsg.MessageID))
}()

case "jubilee_list":

var list string
var count = 1
users, err := functions.GetJubileeUsers()
chatId := update.CallbackQuery.Message.Chat.ID
if err != nil {
logger.Info(err)
}

moderGroupList, err := functions.GetModeratorsGroup()
if err != nil {
logger.Error(err)
}

for _, group := range moderGroupList {

if group.GroupID == chatId {

for _, user := range users {
// TODO take id from base to number users in list
text := fmt.Sprintf("*№%d* \nГруппа: *%s*\nИмя: *%s* Ник: *@%s*\nНомер: *%d* "+
"Время: *%s* ", count, user.GroupName, user.UserName, user.UserNick,
user.Serial, user.Time.Format(config.StructDateTimeFormat))

list = list + text + "\n\n"
count++

}
}
}
msg := tgbotapi.NewMessage(update.CallbackQuery.Message.Chat.ID, "Список юбилейный:\n"+list)
msg.ParseMode = "markdown"
_, _ = bot.Send(msg)

callback := tgbotapi.NewCallback(update.CallbackQuery.ID, "✅")
if _, err := bot.Request(callback); err != nil {
logger.Error(err)
}

case "add_mod":

callback := tgbotapi.NewCallback(update.CallbackQuery.ID, "✅")
if _, err := bot.Request(callback); err != nil {
logger.Error(err)
}

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

import (
"fmt"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"skbot/internal/config"
"skbot/internal/functions"
"skbot/pkg/logging"
"time"
)

func WithChatMembersDo(update tgbotapi.Update, bot *tgbotapi.BotAPI, logger *logging.Logger) {

newUser := update.Message.NewChatMembers[0]
chatId := update.Message.Chat.ID
groupName := update.Message.Chat.Title

if !newUser.IsBot {
count, err := bot.GetChatMembersCount(tgbotapi.ChatMemberCountConfig{
ChatConfig: tgbotapi.ChatConfig{
ChatID: chatId,
SuperGroupUsername: groupName,
},
})

if count%500 == 0 || count%500 == 1 || count%500 == 2 || count == 4 {

err = functions.AddNewJubileeUser(&newUser, count, groupName)
if err != nil {
logger.Error(err)
}

moderGroupList, err := functions.GetModeratorsGroup()
if err != nil {
logger.Error(err)
}

for _, group := range moderGroupList {

text := fmt.Sprintf("🎉 В группу: %s вступил юбилейный пользователь\n%s "+
"(@%s), %d. \nВремя вступления %s",
groupName, newUser.FirstName, newUser.UserName, count,
time.Now().Format(config.StructDateTimeFormat))

_, _ = bot.Send(tgbotapi.NewMessage(group.GroupID, text))
}

}

msg := tgbotapi.NewMessage(chatId, fmt.Sprintf("_Рады вас приветствовать_ "+
"*%s*, _расскажите нам о себе пожалуйста._", newUser.FirstName))

msg.ParseMode = "markdown"
ans, _ := bot.Send(msg)

go func() {

time.Sleep(20 * time.Second)
_, _ = bot.Send(tgbotapi.NewDeleteMessage(chatId, ans.MessageID))
}()
}

}
Loading