100 lines
2.2 KiB
Go
100 lines
2.2 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"git.user-penguin.space/vladimir/all-tg-bot/commands"
|
|
"git.user-penguin.space/vladimir/all-tg-bot/db"
|
|
tgbot "github.com/go-telegram-bot-api/telegram-bot-api"
|
|
"github.com/umputun/go-flags"
|
|
"io/ioutil"
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
)
|
|
|
|
const (
|
|
tag = "@all"
|
|
allUsers = "@vo13crabs @disconecto @v9gel @KeyM_v2 @k_ukolov @iamembarrassed @freel_Fedorov_Kirill @butinmv"
|
|
)
|
|
|
|
type Opts struct {
|
|
Token string `short:"t" long:"token" description:"Telegram api token"`
|
|
Name string `short:"n" long:"name" description:"Telegram bot name" default:"@butler_tg_bot"`
|
|
}
|
|
|
|
var opts Opts
|
|
|
|
func readToken() (string, error) {
|
|
b, err := ioutil.ReadFile("token.txt")
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
str := string(b)
|
|
return str, nil
|
|
}
|
|
|
|
func main() {
|
|
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)
|
|
}
|
|
|
|
if opts.Token == "" {
|
|
token, err := readToken()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
opts.Token = strings.ReplaceAll(token, "\n", "")
|
|
}
|
|
fmt.Println(opts.Token)
|
|
|
|
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)
|
|
}
|
|
|
|
log.Println("Run", opts.Name)
|
|
|
|
for update := range updates {
|
|
|
|
if update.Message == nil {
|
|
continue
|
|
}
|
|
|
|
text := update.Message.Text
|
|
chatID := update.Message.Chat.ID
|
|
|
|
if strings.Contains(strings.ToLower(text), tag) {
|
|
_, _ = bot.Send(tgbot.NewMessage(chatID, allUsers))
|
|
}
|
|
|
|
command := commands.Command(strings.Replace(text, opts.Name, "", 1))
|
|
switch command {
|
|
case commands.Start:
|
|
_, _ = bot.Send(tgbot.NewMessage(chatID, "Здравствуйте, я Волтер (v1.0.1), ваш дворецкий."))
|
|
_ = db.AddChat(chatID)
|
|
case commands.Help:
|
|
_, _ = bot.Send(tgbot.NewMessage(chatID, "Вот что я умею:\n\n1) Позвать всех"))
|
|
case commands.Ping:
|
|
_, _ = bot.Send(tgbot.NewMessage(chatID, "pong"))
|
|
}
|
|
}
|
|
}
|