-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0be57c5
commit 36b5b30
Showing
23 changed files
with
271 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package main | ||
|
||
import ( | ||
"access_governance_system/configs" | ||
"access_governance_system/internal/di" | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
|
||
"github.com/bwmarrin/discordgo" | ||
"go.uber.org/zap" | ||
) | ||
|
||
var ( | ||
config configs.DiscordAuthrozationBotConfig | ||
logger *zap.SugaredLogger | ||
) | ||
|
||
func main() { | ||
config, err := configs.LoadDiscordAuthrozationBotConfig() | ||
logger := di.NewLogger(config.Logger.AppName, config.App.Environment, config.Logger.URL) | ||
|
||
if err != nil { | ||
logger.Fatalw("failed to load config", "error", err) | ||
} | ||
logger.Info("config loaded") | ||
|
||
go func() { | ||
logger.Info("setting up health check server") | ||
settingUpHealthCheckServer(logger) | ||
}() | ||
|
||
logger.Info("starting bot") | ||
|
||
discord, err := discordgo.New("Bot " + config.AuthrozationBot.Token) | ||
if err != nil { | ||
logger.Fatalw("failed to create discord session", "error", err) | ||
} | ||
|
||
discord.AddHandler(authorization) | ||
|
||
discord.Identify.Intents = discordgo.IntentsGuildMessages | ||
|
||
err = discord.Open() | ||
if err != nil { | ||
logger.Fatalw("error opening connection", "error", err) | ||
return | ||
} | ||
|
||
stop := make(chan os.Signal, 1) | ||
signal.Notify(stop, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) | ||
<-stop | ||
|
||
discord.Close() | ||
} | ||
|
||
func authorization(s *discordgo.Session, m *discordgo.MessageCreate) { | ||
if m.Author.ID == s.State.User.ID { | ||
return | ||
} | ||
|
||
if m.Content == "!authorize" { | ||
tgBotLink := fmt.Sprintf("https://t.me/S16AuthorizationBot?start=%s", m.Author.ID) | ||
message := fmt.Sprintf("Привет, для авторизации в сообществе %s перейди по ссылке %s", config.App.CommunityName, tgBotLink) | ||
|
||
channel, err := s.UserChannelCreate(m.Author.ID) | ||
if err != nil { | ||
fmt.Println("failed to create author channel", "error", err) | ||
} | ||
|
||
_, err = s.ChannelMessageSend(channel.ID, message) | ||
if err != nil { | ||
fmt.Println("failed to send message", "error", err) | ||
} | ||
} | ||
} | ||
|
||
func settingUpHealthCheckServer(logger *zap.SugaredLogger) { | ||
mux := http.NewServeMux() | ||
mux.HandleFunc("/authorization-bot-discord/healthcheck", healthCheckHandler) | ||
|
||
server := &http.Server{Addr: ":8080", Handler: mux} | ||
|
||
if err := server.ListenAndServe(); !errors.Is(err, http.ErrServerClosed) { | ||
logger.Errorw("failed to start http server", "error", err) | ||
} | ||
|
||
stop := make(chan os.Signal, 1) | ||
signal.Notify(stop, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) | ||
<-stop | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
defer cancel() | ||
|
||
err := server.Shutdown(ctx) | ||
if err != nil { | ||
logger.Errorw("failed to shutdown http server", "error", err) | ||
return | ||
} | ||
|
||
logger.Info("shutting down") | ||
} | ||
|
||
func healthCheckHandler(w http.ResponseWriter, r *http.Request) { | ||
_, _ = w.Write([]byte("I'm alive")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
package configs | ||
|
||
type Telegram struct { | ||
type Bot struct { | ||
Token string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package configs | ||
|
||
type Discord struct { | ||
Token string `env:"DISCORD_AUTHORIZATION_BOT_TOKEN,notEmpty"` | ||
AuthorizationChannelID string `env:"DISCORD_AUTHORIZATION_CHANNEL_ID,notEmpty"` | ||
Token string `env:"DISCORD_AUTHORIZATION_BOT_TOKEN"` | ||
ChannelID string `env:"DISCORD_SERVER_ID"` | ||
MemberRoleID string `env:"DISCORD_MEMBER_ROLE_ID"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
FROM golang:1.21 | ||
|
||
ARG ENVIRONMENT | ||
ENV ENVIRONMENT=$ENVIRONMENT | ||
|
||
ARG LOKI_URL | ||
ENV LOKI_URL=$LOKI_URL | ||
|
||
ARG DISCORD_AUTHORIZATION_BOT_TOKEN | ||
ENV DISCORD_AUTHORIZATION_BOT_TOKEN=$DISCORD_AUTHORIZATION_BOT_TOKEN | ||
|
||
ARG DISCORD_SERVER_ID | ||
ENV DISCORD_SERVER_ID=$DISCORD_SERVER_ID | ||
|
||
ARG DISCORD_MEMBER_ROLE_ID | ||
ENV DISCORD_MEMBER_ROLE_ID=$DISCORD_MEMBER_ROLE_ID | ||
|
||
WORKDIR /opt/src | ||
|
||
COPY ./go.mod . | ||
COPY ./go.sum . | ||
RUN go mod download | ||
|
||
ADD . . | ||
|
||
RUN go build -o /go/bin/app ./cmd/authorization_bot/discord | ||
|
||
CMD ["/go/bin/app"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.