all-tg-bot/main.go

101 lines
2.2 KiB
Go
Raw Normal View History

2022-04-01 15:33:10 +00:00
package main
2022-04-01 16:17:55 +00:00
import (
"errors"
2022-04-03 11:06:45 +00:00
"fmt"
2022-04-04 14:40:47 +00:00
"git.user-penguin.space/vladimir/all-tg-bot/commands"
2022-04-24 19:35:18 +00:00
"git.user-penguin.space/vladimir/all-tg-bot/db"
2022-04-01 16:17:55 +00:00
tgbot "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/umputun/go-flags"
2022-04-03 11:06:45 +00:00
"io/ioutil"
2022-04-01 16:17:55 +00:00
"log"
"os"
"strings"
)
2022-04-24 20:07:52 +00:00
const tag = "@all"
2022-04-01 16:17:55 +00:00
type Opts struct {
Token string `short:"t" long:"token" description:"Telegram api token"`
2022-04-03 10:19:32 +00:00
Name string `short:"n" long:"name" description:"Telegram bot name" default:"@butler_tg_bot"`
2022-04-01 16:17:55 +00:00
}
var opts Opts
2022-04-01 15:33:10 +00:00
2022-04-03 11:06:45 +00:00
func readToken() (string, error) {
b, err := ioutil.ReadFile("token.txt")
if err != nil {
return "", err
}
str := string(b)
return str, nil
}
2022-04-01 15:33:10 +00:00
func main() {
2022-04-01 16:17:55 +00:00
run()
}
func run() {
p := flags.NewParser(&opts, flags.PrintErrors|flags.PassDoubleDash|flags.HelpFlag)
p.SubcommandsOptional = true
if _, err := p.Parse(); err != nil {
if err.(*flags.Error).Type != flags.ErrHelp {
log.Println(errors.New("[ERROR] cli error: " + err.Error()))
}
os.Exit(2)
}
2022-04-03 11:06:45 +00:00
if opts.Token == "" {
token, err := readToken()
if err != nil {
panic(err)
}
2022-04-03 11:15:09 +00:00
opts.Token = strings.ReplaceAll(token, "\n", "")
2022-04-03 11:06:45 +00:00
}
fmt.Println(opts.Token)
2022-04-01 16:17:55 +00:00
bot, err := tgbot.NewBotAPI(opts.Token)
if err != nil {
panic(err)
}
u := tgbot.NewUpdate(0)
u.Timeout = 60
updates, err := bot.GetUpdatesChan(u)
if err != nil {
panic(err)
}
2022-04-03 11:06:45 +00:00
log.Println("Run", opts.Name)
2022-04-01 16:17:55 +00:00
for update := range updates {
if update.Message == nil {
continue
}
text := update.Message.Text
chatID := update.Message.Chat.ID
2022-04-24 19:54:43 +00:00
username := update.Message.From.UserName
2022-04-24 20:07:52 +00:00
_ = db.AddUserInChat(chatID, username)
2022-04-01 16:17:55 +00:00
if strings.Contains(strings.ToLower(text), tag) {
2022-04-24 20:07:52 +00:00
users, _ := db.GetUsers(chatID)
_, _ = bot.Send(tgbot.NewMessage(chatID, "@"+strings.Join(users, " @")))
2022-04-01 16:17:55 +00:00
}
2022-04-03 10:19:32 +00:00
command := commands.Command(strings.Replace(text, opts.Name, "", 1))
switch command {
case commands.Start:
2022-04-24 20:11:23 +00:00
_, _ = bot.Send(tgbot.NewMessage(chatID, fmt.Sprintf("Здравствуйте, я Волтер (v1.2.1), ваш дворецкий (%d).", chatID)))
2022-04-24 19:35:18 +00:00
_ = db.AddChat(chatID)
2022-04-03 10:35:07 +00:00
case commands.Help:
_, _ = bot.Send(tgbot.NewMessage(chatID, "Вот что я умею:\n\n1) Позвать всех"))
2022-04-03 10:19:32 +00:00
case commands.Ping:
2022-04-03 11:36:08 +00:00
_, _ = bot.Send(tgbot.NewMessage(chatID, "pong"))
2022-04-03 10:19:32 +00:00
}
2022-04-01 16:17:55 +00:00
}
2022-04-01 15:33:10 +00:00
}