Compare commits
33 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 9c142cf6c2 | |||
| 0126d0272a | |||
| 9807b624ba | |||
| ec4e1e93cb | |||
| 9a12fd063e | |||
| 6774c2f631 | |||
| eccef756a0 | |||
| 00d33aa41d | |||
| 60746f8824 | |||
| dd358d2c13 | |||
| e4e3859277 | |||
| 8509ec9d33 | |||
| 3c98b8b9b7 | |||
| 8c5698f993 | |||
| 85a8ccb57d | |||
| a74a4d46ee | |||
| e0a3d165e1 | |||
| 7ff184720d | |||
| 858fbb73c1 | |||
| 02ec38e5ef | |||
| 09047de28e | |||
| 72ece0d100 | |||
| fc20b80d7b | |||
| 984b5bbee9 | |||
| b1c34f77ce | |||
| 035045ce90 | |||
| 3ad9a6274d | |||
| d453ab3d64 | |||
| 33d11fc849 | |||
| cb6f1a394a | |||
| 9e38b000ec | |||
| 0a8409e66d | |||
| b76c7dd005 |
@@ -0,0 +1,33 @@
|
||||
name: Check Go code
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
|
||||
permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
check:
|
||||
name: Build and test
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup Go
|
||||
uses: actions/setup-go@v4
|
||||
with:
|
||||
go-version: '1.25.x'
|
||||
cache: false
|
||||
|
||||
- name: Download dependencies
|
||||
run: go mod download
|
||||
|
||||
- name: Run build
|
||||
run: go build ./cmd/anon3anon/
|
||||
|
||||
- name: Run tests
|
||||
run: go test ./...
|
||||
+3
-3
@@ -1,3 +1,3 @@
|
||||
.idea
|
||||
vendor
|
||||
docker-compose.override.yml
|
||||
/.idea/
|
||||
/vendor/
|
||||
/docker-compose.override.yml
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
version: "2"
|
||||
|
||||
run:
|
||||
timeout: 5m
|
||||
issues-exit-code: 1
|
||||
|
||||
linters:
|
||||
enable:
|
||||
- errcheck
|
||||
- govet
|
||||
- ineffassign
|
||||
- staticcheck
|
||||
- unused
|
||||
- bodyclose
|
||||
- importas
|
||||
- dogsled
|
||||
- dupl
|
||||
- gochecknoinits
|
||||
- gocognit
|
||||
- gocritic
|
||||
- gocyclo
|
||||
- revive
|
||||
- gosec
|
||||
- misspell
|
||||
- nakedret
|
||||
- prealloc
|
||||
- unconvert
|
||||
- whitespace
|
||||
- rowserrcheck
|
||||
- goconst
|
||||
- asciicheck
|
||||
- nestif
|
||||
- copyloopvar
|
||||
- sqlclosecheck
|
||||
|
||||
settings:
|
||||
govet:
|
||||
enable:
|
||||
- shadow
|
||||
|
||||
gocritic:
|
||||
disabled-checks:
|
||||
- sloppyReassign
|
||||
- whyNoLint
|
||||
enabled-tags:
|
||||
- experimental
|
||||
- opinionated
|
||||
|
||||
exclusions:
|
||||
generated: lax
|
||||
presets:
|
||||
- comments
|
||||
- common-false-positives
|
||||
- std-error-handling
|
||||
paths:
|
||||
- third_party$
|
||||
- builtin$
|
||||
- examples$
|
||||
|
||||
formatters:
|
||||
enable:
|
||||
- gofmt
|
||||
- goimports
|
||||
|
||||
exclusions:
|
||||
generated: lax
|
||||
paths:
|
||||
- third_party$
|
||||
- builtin$
|
||||
- examples$
|
||||
@@ -1,26 +1,19 @@
|
||||
# anon3anon
|
||||
# :performing_arts: anon3anon
|
||||
|
||||
Telegram bot for anonymous messages 🎭✨
|
||||
Telegram bot for anonymous messages.
|
||||
|
||||
---
|
||||
|
||||
[](https://goreportcard.com/report/github.com/nightnoryu/anon3anon)
|
||||
|
||||
## Building for local development
|
||||
## Local development
|
||||
|
||||
Prerequisites:
|
||||
|
||||
1. Linux
|
||||
2. Git
|
||||
3. Docker
|
||||
4. (optional) [BrewKit](https://github.com/ispringtech/brewkit)
|
||||
1. Git
|
||||
2. Docker
|
||||
3. [brewkit](https://github.com/ispringtech/brewkit)
|
||||
|
||||
Firstly, clone the repository into your `$GOPATH`:
|
||||
Clone the repository:
|
||||
|
||||
```shell
|
||||
mkdir -p $GOPATH/src/github.com/nightnoryu
|
||||
cd $GOPATH/src/github.com/nightnoryu
|
||||
git clone git@github.com:nightnoryu/anon3anon.git
|
||||
git clone ssh://git@gitea.nightnoryu.com:2222/nightnoryu/anon3anon.git
|
||||
cd anon3anon
|
||||
```
|
||||
|
||||
@@ -28,9 +21,6 @@ Then build the project:
|
||||
|
||||
```shell
|
||||
brewkit build
|
||||
|
||||
# Alternatively, if you don't want to use BrewKit, you can do it the old-fashioned way:
|
||||
# go build -o ./bin/anon3anon ./cmd/anon3anon
|
||||
```
|
||||
|
||||
After that, copy the `docker-compose.override.example.yml` to `docker-compose.override.yml` and set the environment variables:
|
||||
@@ -39,10 +29,13 @@ After that, copy the `docker-compose.override.example.yml` to `docker-compose.ov
|
||||
services:
|
||||
anon3anon:
|
||||
environment:
|
||||
TELEGRAM_BOT_TOKEN: 123:ABC # The token for your bot, obtained from t.me/BotFather
|
||||
OWNER_CHAT_ID: 123 # ID of your chat with your bot
|
||||
ANON3ANON_TELEGRAM_BOT_TOKEN: 123:ABC # The token for your bot, obtained from t.me/BotFather
|
||||
ANON3ANON_OWNER_CHAT_ID: 123 # ID of your chat with your bot
|
||||
```
|
||||
|
||||
> [!NOTE]
|
||||
> When launching the bot for the first time, leave `ANON3ANON_OWNER_CHAT_ID` empty and write a message to the bot. It will print the chat ID in the logs and after that you can set it up.
|
||||
|
||||
And you're set! Use `docker compose` to manage the application:
|
||||
|
||||
```shell
|
||||
|
||||
@@ -1,2 +0,0 @@
|
||||
build:
|
||||
dockerfile: docker/Dockerfile-amvera
|
||||
@@ -1,3 +1,4 @@
|
||||
{
|
||||
gobuilder: "golang:1.24.2"
|
||||
gobuilder: "golang:1.25-alpine",
|
||||
golangcilint: "golangci/golangci-lint:v2.4-alpine",
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ local gocache = [
|
||||
apiVersion: "brewkit/v1",
|
||||
|
||||
targets: {
|
||||
all: ["modules", "build"],
|
||||
all: ["modules", "build", "check"],
|
||||
} + {
|
||||
modules: ["gotidy", "modulesvendor"],
|
||||
|
||||
@@ -62,6 +62,30 @@ local gocache = [
|
||||
}
|
||||
}
|
||||
for appID in appIDs
|
||||
} + {
|
||||
check: ["test", "lint"],
|
||||
|
||||
test: {
|
||||
from: "gobase",
|
||||
workdir: "/app",
|
||||
cache: gocache,
|
||||
command: "go test ./..."
|
||||
},
|
||||
|
||||
lint: {
|
||||
from: images.golangcilint,
|
||||
workdir: "/app",
|
||||
cache: gocache,
|
||||
copy: [
|
||||
copyFrom("gosources", "/app", "/app"),
|
||||
copy(".golangci.yml", ".golangci.yml"),
|
||||
],
|
||||
env: {
|
||||
GOCACHE: "/app/cache/go-build",
|
||||
GOLANGCI_LINT_CACHE: "/app/cache/go-build",
|
||||
},
|
||||
command: "golangci-lint run"
|
||||
},
|
||||
} + {
|
||||
gobase: {
|
||||
from: images.gobuilder,
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/kelseyhightower/envconfig"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func parseEnv() (*config, error) {
|
||||
c := new(config)
|
||||
if err := envconfig.Process(appID, c); err != nil {
|
||||
return nil, errors.Wrap(err, "failed to parse env")
|
||||
return nil, fmt.Errorf("failed to parse env: %w", err)
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
+13
-7
@@ -22,13 +22,8 @@ func main() {
|
||||
logger.FatalError(err)
|
||||
}
|
||||
|
||||
opts := []bot.Option{
|
||||
bot.WithMiddlewares(middleware.NewLoggingMiddleware(logger)),
|
||||
bot.WithMessageTextHandler("/start", bot.MatchTypeExact, handler.NewStartCommandHandler(logger)),
|
||||
bot.WithDefaultHandler(handler.NewAnonymousMessagesHandler(logger, conf.OwnerChatID)),
|
||||
}
|
||||
|
||||
b, err := bot.New(conf.TelegramBotToken, opts...)
|
||||
options := initBotOptions(conf, logger)
|
||||
b, err := bot.New(conf.TelegramBotToken, options...)
|
||||
if err != nil {
|
||||
logger.FatalError(err)
|
||||
}
|
||||
@@ -46,3 +41,14 @@ func initLogger() jsonlog.Logger {
|
||||
})
|
||||
return logger
|
||||
}
|
||||
|
||||
func initBotOptions(conf *config, logger jsonlog.Logger) []bot.Option {
|
||||
startCommandHandler := handler.NewStartCommandHandler(logger)
|
||||
anonymousMessagesHandler := handler.NewAnonymousMessagesHandler(logger, conf.OwnerChatID)
|
||||
|
||||
return []bot.Option{
|
||||
bot.WithMiddlewares(middleware.NewLoggingMiddleware(logger)),
|
||||
bot.WithMessageTextHandler("start", bot.MatchTypeCommand, startCommandHandler),
|
||||
bot.WithDefaultHandler(anonymousMessagesHandler),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,5 @@ services:
|
||||
container_name: anon3anon
|
||||
build:
|
||||
context: .
|
||||
dockerfile: docker/Dockerfile-local
|
||||
volumes:
|
||||
- "./bin:/app/bin"
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
FROM golang:1.24.2 AS build
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY . .
|
||||
|
||||
RUN go mod download
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -o /app/bin/anon3anon ./cmd/anon3anon
|
||||
|
||||
FROM alpine:latest AS run
|
||||
|
||||
COPY --from=build /app/bin/anon3anon /app/bin/
|
||||
|
||||
CMD ["/app/bin/anon3anon"]
|
||||
@@ -1,11 +1,10 @@
|
||||
module github.com/nightnoryu/anon3anon
|
||||
|
||||
go 1.24
|
||||
go 1.25
|
||||
|
||||
require (
|
||||
github.com/go-telegram/bot v1.13.3
|
||||
github.com/go-telegram/bot v1.15.0
|
||||
github.com/kelseyhightower/envconfig v1.4.0
|
||||
github.com/pkg/errors v0.9.1
|
||||
github.com/sirupsen/logrus v1.9.3
|
||||
)
|
||||
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
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 v1.13.3 h1:r2erpHI5rMQsR5TFWJ/XVqWHq9R228fcaejLFvXJsmM=
|
||||
github.com/go-telegram/bot v1.13.3/go.mod h1:i2TRs7fXWIeaceF3z7KzsMt/he0TwkVC680mvdTFYeM=
|
||||
github.com/go-telegram/bot v1.15.0 h1:/ba5pp084MUhjR5sQDymQ7JNZ001CQa7QjtxLWcuGpg=
|
||||
github.com/go-telegram/bot v1.15.0/go.mod h1:i2TRs7fXWIeaceF3z7KzsMt/he0TwkVC680mvdTFYeM=
|
||||
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
|
||||
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
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.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
||||
|
||||
@@ -14,11 +14,11 @@ var fieldMap = logrus.FieldMap{
|
||||
}
|
||||
|
||||
type Logger interface {
|
||||
WithField(key string, value interface{}) Logger
|
||||
WithField(key string, value any) Logger
|
||||
|
||||
Info(...interface{})
|
||||
Error(error, ...interface{})
|
||||
FatalError(error, ...interface{})
|
||||
Info(...any)
|
||||
Error(error, ...any)
|
||||
FatalError(error, ...any)
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
@@ -42,14 +42,14 @@ type logger struct {
|
||||
logrus.FieldLogger
|
||||
}
|
||||
|
||||
func (l *logger) WithField(key string, value interface{}) Logger {
|
||||
func (l *logger) WithField(key string, value any) Logger {
|
||||
return &logger{l.FieldLogger.WithField(key, value)}
|
||||
}
|
||||
|
||||
func (l *logger) Error(err error, args ...interface{}) {
|
||||
l.FieldLogger.WithError(err).Error(args)
|
||||
func (l *logger) Error(err error, args ...any) {
|
||||
l.FieldLogger.WithError(err).Error(args...)
|
||||
}
|
||||
|
||||
func (l *logger) FatalError(err error, args ...interface{}) {
|
||||
func (l *logger) FatalError(err error, args ...any) {
|
||||
l.FieldLogger.WithError(err).Fatal(args...)
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/nightnoryu/anon3anon/pkg/infrastructure/jsonlog"
|
||||
|
||||
@@ -9,14 +10,19 @@ import (
|
||||
"github.com/go-telegram/bot/models"
|
||||
)
|
||||
|
||||
func NewAnonymousMessagesHandler(logger jsonlog.Logger, ownerChatId int) bot.HandlerFunc {
|
||||
func NewAnonymousMessagesHandler(logger jsonlog.Logger, ownerChatID int) bot.HandlerFunc {
|
||||
return func(ctx context.Context, b *bot.Bot, update *models.Update) {
|
||||
if update.Message == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if ownerChatID == 0 {
|
||||
logger.Info(fmt.Sprintf("owner chat ID not set. set to %d to use the last chat", update.Message.Chat.ID))
|
||||
return
|
||||
}
|
||||
|
||||
params := &bot.CopyMessageParams{
|
||||
ChatID: ownerChatId,
|
||||
ChatID: ownerChatID,
|
||||
FromChatID: update.Message.Chat.ID,
|
||||
MessageID: update.Message.ID,
|
||||
}
|
||||
@@ -28,7 +34,7 @@ func NewAnonymousMessagesHandler(logger jsonlog.Logger, ownerChatId int) bot.Han
|
||||
|
||||
sendParams := &bot.SendMessageParams{
|
||||
ChatID: update.Message.Chat.ID,
|
||||
Text: "Сообщение отправлено!!",
|
||||
Text: messageSentMessage,
|
||||
}
|
||||
_, err = b.SendMessage(ctx, sendParams)
|
||||
if err != nil {
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package handler
|
||||
|
||||
const (
|
||||
greetingMessage = "Жду твоих сообщений!!"
|
||||
messageSentMessage = "Сообщение отправлено!!"
|
||||
)
|
||||
@@ -11,9 +11,13 @@ import (
|
||||
|
||||
func NewStartCommandHandler(logger jsonlog.Logger) bot.HandlerFunc {
|
||||
return func(ctx context.Context, b *bot.Bot, update *models.Update) {
|
||||
if update.Message == nil {
|
||||
return
|
||||
}
|
||||
|
||||
params := &bot.SendMessageParams{
|
||||
ChatID: update.Message.Chat.ID,
|
||||
Text: "Жду твоих сообщений!!",
|
||||
Text: greetingMessage,
|
||||
}
|
||||
_, err := b.SendMessage(ctx, params)
|
||||
if err != nil {
|
||||
|
||||
@@ -27,7 +27,7 @@ func NewLoggingMiddleware(logger jsonlog.Logger) bot.Middleware {
|
||||
WithField(usernameField, update.Message.From.Username)
|
||||
|
||||
text := update.Message.Text
|
||||
if len(update.Message.Caption) > 0 {
|
||||
if update.Message.Caption != "" {
|
||||
text = update.Message.Caption
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user