Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Beniamiiin committed Nov 30, 2023
1 parent 52c2ef1 commit 8c0e5a8
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/docker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ jobs:
TELEGRAM_ACCESS_GOVERNANCE_BOT_TOKEN=${{ secrets.TELEGRAM_ACCESS_GOVERNANCE_BOT_TOKEN }}
TELEGRAM_VOTE_BOT_TOKEN=${{ secrets.TELEGRAM_VOTE_BOT_TOKEN }}
MEMBERS_CHAT_ID=${{ vars.MEMBERS_CHAT_ID }}
SEEDERS_CHAT_ID=${{ vars.SEEDERS_CHAT_ID }}
push: true
tags: ghcr.io/beniamiiin/access-governance-system:agb

Expand All @@ -55,6 +56,7 @@ jobs:
YES_VOTES_TO_OVERCOME_NO=${{ vars.YES_VOTES_TO_OVERCOME_NO }}
TELEGRAM_ACCESS_GOVERNANCE_BOT_TOKEN=${{ secrets.TELEGRAM_ACCESS_GOVERNANCE_BOT_TOKEN }}
MEMBERS_CHAT_ID=${{ vars.MEMBERS_CHAT_ID }}
SEEDERS_CHAT_ID=${{ vars.SEEDERS_CHAT_ID }}
push: true
tags: ghcr.io/beniamiiin/access-governance-system:pss

Expand Down
1 change: 1 addition & 0 deletions configs/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ type App struct {
RenominationPeriodDays int `env:"RENOMINATION_PERIOD_DAYS" envDefault:"3"`
InitialSeeders []string `env:"INITIAL_SEEDERS" envSeparator:","`
MembersChatID int64 `env:"MEMBERS_CHAT_ID"`
SeedersChatID int64 `env:"SEEDERS_CHAT_ID"`
}
3 changes: 3 additions & 0 deletions deployments/access_governance_bot/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ ENV TELEGRAM_VOTE_BOT_TOKEN=$TELEGRAM_VOTE_BOT_TOKEN
ARG MEMBERS_CHAT_ID
ENV MEMBERS_CHAT_ID=$MEMBERS_CHAT_ID

ARG SEEDERS_CHAT_ID
ENV SEEDERS_CHAT_ID=$SEEDERS_CHAT_ID

WORKDIR /opt/src

COPY ./go.mod .
Expand Down
87 changes: 82 additions & 5 deletions internal/tg_bot/commands/access_governance_bot/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"access_governance_system/internal/db/models"
"access_governance_system/internal/db/repositories"
"access_governance_system/internal/tg_bot/commands"
tgbot "access_governance_system/internal/tg_bot/extension"
"fmt"
"strings"

Expand All @@ -15,14 +16,15 @@ import (
const startCommandName = "start"

type startCommand struct {
appConfig configs.App
bot *tgbotapi.BotAPI
config configs.AccessGovernanceBotConfig
userRepository repositories.UserRepository
logger *zap.SugaredLogger
}

func NewStartCommand(appConfig configs.App, userRepository repositories.UserRepository, logger *zap.SugaredLogger) commands.Command {
func NewStartCommand(config configs.AccessGovernanceBotConfig, userRepository repositories.UserRepository, logger *zap.SugaredLogger) commands.Command {
return &startCommand{
appConfig: appConfig,
config: config,
userRepository: userRepository,
logger: logger,
}
Expand All @@ -33,6 +35,8 @@ func (c *startCommand) CanHandle(command string) bool {
}

func (c *startCommand) Handle(text, arguments string, user *models.User, chatID int64) []tgbotapi.Chattable {
var messages = []tgbotapi.Chattable{}

parseMode := tgbotapi.ModeMarkdownV2

messageText := tgbotapi.EscapeText(parseMode, fmt.Sprintf(`
Expand All @@ -43,9 +47,82 @@ func (c *startCommand) Handle(text, arguments string, user *models.User, chatID
/approved_proposals - с помощью данной команды, ты можешь посмотреть все принятые/отклоненные предложения.
Для более подробного изучения всех доступных команд, нажми на кнопку Menu.
`, c.appConfig.CommunityName))
`, c.config.App.CommunityName))
messageText = strings.Replace(messageText, "Menu", "*Menu*", -1)
message := tgbotapi.NewMessage(chatID, messageText)
message.ParseMode = parseMode
return []tgbotapi.Chattable{message}
messages = append(messages, message)

if user.Role == models.UserRoleSeeder && user.DiscordID == 0 {
message := c.createInstructionMessageForSeeder(chatID)

if message == nil {
return []tgbotapi.Chattable{tgbot.DefaultErrorMessage(chatID)}
}

messages = append(messages, message)
}

return messages
}

func (c *startCommand) createInstructionMessageForSeeder(chatID int64) tgbotapi.Chattable {
if c.bot == nil {
var err error
c.bot, err = tgbotapi.NewBotAPI(c.config.AccessGovernanceBot.Token)
if err != nil {
c.logger.Fatalf("could not create bot: %v", err)
return nil
}
}

membersChatInviteLink, err := c.createMembersChatInviteLink()
if err != nil {
c.logger.Fatalf("could not create members chat invite link: %v", err)
return nil
}

seedersChatInviteLink, err := c.createSeedersChatInviteLink()
if err != nil {
c.logger.Fatalf("could not create seeders chat invite link: %v", err)
return nil
}

messageText := fmt.Sprintf(`
Я заметил, что ты являешься сидером, но ты еще не полностью авторизован в нашем сообществе.
Для того, чтобы авторизоваться, зайди в нашу группу для members(%s) и для seeders(%s).
И следуй инструкции, которую ты найдешь в закрепленном сообщении в группе для members.
`, membersChatInviteLink, seedersChatInviteLink)
return tgbotapi.NewMessage(chatID, messageText)
}

func (c *startCommand) createMembersChatInviteLink() (string, error) {
inviteLinkConfig := tgbotapi.ChatInviteLinkConfig{
ChatConfig: tgbotapi.ChatConfig{
ChatID: c.config.App.MembersChatID,
},
}

inviteLink, err := c.bot.GetInviteLink(inviteLinkConfig)
if err != nil {
return "", err
}

return inviteLink, nil
}

func (c *startCommand) createSeedersChatInviteLink() (string, error) {
inviteLinkConfig := tgbotapi.ChatInviteLinkConfig{
ChatConfig: tgbotapi.ChatConfig{
ChatID: c.config.App.SeedersChatID,
},
}

inviteLink, err := c.bot.GetInviteLink(inviteLinkConfig)
if err != nil {
return "", err
}

return inviteLink, nil
}
10 changes: 10 additions & 0 deletions internal/tg_bot/commands/authorization_bot/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@ func (c *startCommand) CanHandle(command string) bool {

func (c *startCommand) Handle(text, discordID string, user *models.User, chatID int64) []tgbotapi.Chattable {
if user.Role == models.UserRoleMember || user.Role == models.UserRoleSeeder {
if user.DiscordID == 0 {
user.DiscordID, _ = strconv.Atoi(discordID)
_, err := c.userRepository.Update(user)
if err != nil {
c.logger.Errorw("failed to update user", "user", user, "error", err)
return []tgbotapi.Chattable{extension.DefaultErrorMessage(chatID)}
}
}
return []tgbotapi.Chattable{
tgbotapi.NewMessage(chatID, "Привет, ты уже авторизован."),
}
Expand All @@ -65,6 +73,8 @@ func (c *startCommand) Handle(text, discordID string, user *models.User, chatID
return []tgbotapi.Chattable{extension.DefaultErrorMessage(chatID)}
}

user.Role = models.UserRoleMember

_, err = c.userRepository.Update(user)
if err != nil {
c.logger.Errorw("failed to update user", "user", user, "error", err)
Expand Down

0 comments on commit 8c0e5a8

Please sign in to comment.