init
This commit is contained in:
commit
973c19f148
7 changed files with 95 additions and 0 deletions
1
.env
Normal file
1
.env
Normal file
|
|
@ -0,0 +1 @@
|
|||
BOT_TOKEN=8978257540:AAEzAXLT1ylVlC-y0AtoM_FXtTMntbpYg60
|
||||
8
go.mod
Normal file
8
go.mod
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
module spambot
|
||||
|
||||
go 1.26.5
|
||||
|
||||
require (
|
||||
github.com/go-telegram/bot v1.23.0 // indirect
|
||||
github.com/joho/godotenv v1.5.1
|
||||
)
|
||||
4
go.sum
Normal file
4
go.sum
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
github.com/go-telegram/bot v1.23.0 h1:CKKQq115G/GUGBG8uuWl5uXbiBHyVjZBp/qqOLWZjJk=
|
||||
github.com/go-telegram/bot v1.23.0/go.mod h1:i2TRs7fXWIeaceF3z7KzsMt/he0TwkVC680mvdTFYeM=
|
||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
35
internal/bot/bot.go
Normal file
35
internal/bot/bot.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package bot
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"spambot/internal/commands"
|
||||
"spambot/internal/config"
|
||||
|
||||
"github.com/go-telegram/bot"
|
||||
)
|
||||
|
||||
type TelegramBot struct {
|
||||
config *config.Config
|
||||
bot *bot.Bot
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func NewTelegramBot(config *config.Config) *TelegramBot {
|
||||
b, err := bot.New(config.Token)
|
||||
|
||||
b.RegisterHandler(bot.HandlerTypeMessageText, "/start", bot.MatchTypeExact, commands.StartCommand)
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("Failed to create bot: %v", err)
|
||||
}
|
||||
return &TelegramBot{
|
||||
config: config,
|
||||
bot: b,
|
||||
ctx: context.Background(),
|
||||
}
|
||||
}
|
||||
|
||||
func (b *TelegramBot) Start() {
|
||||
b.bot.Start(b.ctx)
|
||||
}
|
||||
15
internal/commands/start.go
Normal file
15
internal/commands/start.go
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
package commands
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/go-telegram/bot"
|
||||
"github.com/go-telegram/bot/models"
|
||||
)
|
||||
|
||||
func StartCommand(ctx context.Context, b *bot.Bot, update *models.Update) {
|
||||
b.SendMessage(ctx, &bot.SendMessageParams{
|
||||
ChatID: update.Message.Chat.ID,
|
||||
Text: "Hello, world!",
|
||||
})
|
||||
}
|
||||
20
internal/config/config.go
Normal file
20
internal/config/config.go
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Token string
|
||||
}
|
||||
|
||||
func NewConfig() *Config {
|
||||
config := &Config{}
|
||||
_ = godotenv.Load()
|
||||
|
||||
config.Token = os.Getenv("BOT_TOKEN")
|
||||
|
||||
return config
|
||||
}
|
||||
12
main.go
Normal file
12
main.go
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"spambot/internal/bot"
|
||||
"spambot/internal/config"
|
||||
)
|
||||
|
||||
func main() {
|
||||
config := config.NewConfig()
|
||||
bot := bot.NewTelegramBot(config)
|
||||
bot.Start()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue