Add separate questions service and implement simple bot behavior

This commit is contained in:
2024-07-28 19:32:09 +03:00
parent 4dc77effd6
commit a0f9a6347a
4 changed files with 106 additions and 18 deletions
+34 -17
View File
@@ -3,36 +3,53 @@ package main
import (
"log"
"os"
"strconv"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
"github.com/nightnoryu/anon3anon/pkg/app"
)
const telegramBotTokenEnvKey = "TELEGRAM_BOT_TOKEN"
const (
telegramBotTokenEnvKey = "TELEGRAM_BOT_TOKEN"
ownerChatIDEnvKey = "OWNER_CHAT_ID"
)
type config struct {
Token string
OwnerChatID int64
}
func getConfig() (config, error) {
token := os.Getenv(telegramBotTokenEnvKey)
ownerChatID, err := strconv.ParseInt(os.Getenv(ownerChatIDEnvKey), 10, 64)
if err != nil {
return config{}, err
}
return config{
Token: token,
OwnerChatID: ownerChatID,
}, nil
}
func main() {
token := os.Getenv(telegramBotTokenEnvKey)
bot, err := tgbotapi.NewBotAPI(token)
config, err := getConfig()
if err != nil {
log.Panic(err)
}
bot.Debug = true
bot, err := tgbotapi.NewBotAPI(config.Token)
if err != nil {
log.Panic(err)
}
//bot.Debug = true
log.Printf("Authorized on account %s", bot.Self.UserName)
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
service := app.NewAnonymousQuestionsService(bot, config.OwnerChatID)
updates := bot.GetUpdatesChan(u)
for update := range updates {
if update.Message != nil { // If we got a message
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
msg := tgbotapi.NewMessage(update.Message.Chat.ID, update.Message.Text)
msg.ReplyToMessageID = update.Message.MessageID
bot.Send(msg)
}
if err := service.ListenForMessages(); err != nil {
log.Panic(err)
}
}
+4 -1
View File
@@ -2,4 +2,7 @@ module github.com/nightnoryu/anon3anon
go 1.22.5
require github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 // indirect
require (
github.com/go-telegram-bot-api/telegram-bot-api/v5 v5.5.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
)
+2
View File
@@ -1,2 +1,4 @@
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/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
+66
View File
@@ -0,0 +1,66 @@
package app
import (
"log"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
func NewAnonymousQuestionsService(bot *tgbotapi.BotAPI, ownerChatID int64) AnonymousMessagesService {
return &anonymousMessagesService{
bot: bot,
ownerChatID: ownerChatID,
}
}
type AnonymousMessagesService interface {
ListenForMessages() error
}
type anonymousMessagesService struct {
bot *tgbotapi.BotAPI
ownerChatID int64
}
func (s *anonymousMessagesService) ListenForMessages() error {
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
updates := s.bot.GetUpdatesChan(u)
for update := range updates {
if update.Message == nil {
continue
}
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
if update.Message.Text == "/start" {
reply := tgbotapi.NewMessage(update.Message.Chat.ID, "Жду твоих вопросов!")
s.bot.Send(reply)
continue
}
if len(update.Message.Photo) > 0 {
var photos []interface{}
photo := tgbotapi.NewInputMediaPhoto(tgbotapi.FileID(update.Message.Photo[0].FileID))
photo.Caption = "*Новое анонимное сообщение!*"
photo.ParseMode = tgbotapi.ModeMarkdown
photos = append(photos, photo)
mediaMsg := tgbotapi.NewMediaGroup(s.ownerChatID, photos)
s.bot.Send(mediaMsg)
} else {
msg := tgbotapi.NewMessage(s.ownerChatID, "*Новое анонимное сообщение!*\n\n"+update.Message.Text)
msg.ParseMode = tgbotapi.ModeMarkdown
s.bot.Send(msg)
}
reply := tgbotapi.NewMessage(update.Message.Chat.ID, "Сообщение отправлено!")
s.bot.Send(reply)
}
return nil
}