2021-05-07 00:08:12 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
tg "github.com/go-telegram-bot-api/telegram-bot-api"
|
|
|
|
)
|
|
|
|
|
2021-05-17 22:42:33 +00:00
|
|
|
type TelegramVerifiedToken struct {
|
2021-05-07 13:32:51 +00:00
|
|
|
Token string
|
|
|
|
ChatID int64
|
|
|
|
Username string
|
|
|
|
}
|
|
|
|
|
2021-05-07 00:08:12 +00:00
|
|
|
type TelegramDaemon struct {
|
|
|
|
Stopped bool
|
|
|
|
ShutdownChannel chan string
|
|
|
|
bot *tg.BotAPI
|
|
|
|
username string
|
|
|
|
tokens []string
|
2021-05-17 22:42:33 +00:00
|
|
|
verifiedTokens []TelegramVerifiedToken
|
2021-05-07 13:32:51 +00:00
|
|
|
languages map[int64]string // Store of languages for chatIDs. Added to on first interaction, and loaded from app.storage.telegram on start.
|
2021-05-07 00:08:12 +00:00
|
|
|
link string
|
|
|
|
app *appContext
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTelegramDaemon(app *appContext) (*TelegramDaemon, error) {
|
|
|
|
token := app.config.Section("telegram").Key("token").String()
|
|
|
|
if token == "" {
|
|
|
|
return nil, fmt.Errorf("token was blank")
|
|
|
|
}
|
|
|
|
bot, err := tg.NewBotAPI(token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-07 13:32:51 +00:00
|
|
|
td := &TelegramDaemon{
|
2021-05-07 00:08:12 +00:00
|
|
|
ShutdownChannel: make(chan string),
|
|
|
|
bot: bot,
|
|
|
|
username: bot.Self.UserName,
|
|
|
|
tokens: []string{},
|
2021-05-17 22:42:33 +00:00
|
|
|
verifiedTokens: []TelegramVerifiedToken{},
|
2021-05-07 13:32:51 +00:00
|
|
|
languages: map[int64]string{},
|
2021-05-07 00:08:12 +00:00
|
|
|
link: "https://t.me/" + bot.Self.UserName,
|
|
|
|
app: app,
|
2021-05-07 13:32:51 +00:00
|
|
|
}
|
|
|
|
for _, user := range app.storage.telegram {
|
|
|
|
if user.Lang != "" {
|
|
|
|
td.languages[user.ChatID] = user.Lang
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return td, nil
|
2021-05-07 00:08:12 +00:00
|
|
|
}
|
|
|
|
|
2021-05-17 22:42:33 +00:00
|
|
|
func genAuthToken() string {
|
2021-05-07 00:08:12 +00:00
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
pin := make([]rune, 8)
|
|
|
|
for i := range pin {
|
2021-05-29 16:43:11 +00:00
|
|
|
if (i+1)%3 == 0 {
|
2021-05-07 00:08:12 +00:00
|
|
|
pin[i] = '-'
|
|
|
|
} else {
|
|
|
|
pin[i] = runes[rand.Intn(len(runes))]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return string(pin)
|
|
|
|
}
|
|
|
|
|
2021-05-17 22:42:33 +00:00
|
|
|
var runes = []rune("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
|
|
|
|
|
|
|
|
// NewAuthToken generates an 8-character pin in the form "A1-2B-CD".
|
|
|
|
func (t *TelegramDaemon) NewAuthToken() string {
|
|
|
|
pin := genAuthToken()
|
|
|
|
t.tokens = append(t.tokens, pin)
|
|
|
|
return pin
|
|
|
|
}
|
|
|
|
|
2021-05-07 00:08:12 +00:00
|
|
|
func (t *TelegramDaemon) run() {
|
|
|
|
t.app.info.Println("Starting Telegram bot daemon")
|
|
|
|
u := tg.NewUpdate(0)
|
|
|
|
u.Timeout = 60
|
|
|
|
updates, err := t.bot.GetUpdatesChan(u)
|
|
|
|
if err != nil {
|
|
|
|
t.app.err.Printf("Failed to start Telegram daemon: %v", err)
|
2021-05-23 13:48:36 +00:00
|
|
|
telegramEnabled = false
|
2021-05-07 00:08:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
var upd tg.Update
|
|
|
|
select {
|
|
|
|
case upd = <-updates:
|
|
|
|
if upd.Message == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
sects := strings.Split(upd.Message.Text, " ")
|
|
|
|
if len(sects) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
lang := t.app.storage.lang.chosenTelegramLang
|
2021-05-07 13:32:51 +00:00
|
|
|
storedLang, ok := t.languages[upd.Message.Chat.ID]
|
2021-05-07 00:08:12 +00:00
|
|
|
if !ok {
|
2021-05-07 13:32:51 +00:00
|
|
|
found := false
|
2021-05-07 00:08:12 +00:00
|
|
|
for code := range t.app.storage.lang.Telegram {
|
|
|
|
if code[:2] == upd.Message.From.LanguageCode {
|
|
|
|
lang = code
|
2021-05-07 13:32:51 +00:00
|
|
|
found = true
|
2021-05-07 00:08:12 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2021-05-07 13:32:51 +00:00
|
|
|
if found {
|
|
|
|
t.languages[upd.Message.Chat.ID] = lang
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lang = storedLang
|
2021-05-07 00:08:12 +00:00
|
|
|
}
|
|
|
|
switch msg := sects[0]; msg {
|
|
|
|
case "/start":
|
2021-05-16 14:00:13 +00:00
|
|
|
t.commandStart(&upd, sects, lang)
|
2021-05-07 00:08:12 +00:00
|
|
|
continue
|
|
|
|
case "/lang":
|
2021-05-16 14:00:13 +00:00
|
|
|
t.commandLang(&upd, sects, lang)
|
2021-05-07 00:08:12 +00:00
|
|
|
continue
|
|
|
|
default:
|
2021-05-16 14:00:13 +00:00
|
|
|
t.commandPIN(&upd, sects, lang)
|
2021-05-07 00:08:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
case <-t.ShutdownChannel:
|
2021-05-07 17:29:56 +00:00
|
|
|
t.bot.StopReceivingUpdates()
|
2021-05-07 00:08:12 +00:00
|
|
|
t.ShutdownChannel <- "Down"
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TelegramDaemon) Reply(upd *tg.Update, content string) error {
|
|
|
|
msg := tg.NewMessage((*upd).Message.Chat.ID, content)
|
|
|
|
_, err := t.bot.Send(msg)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TelegramDaemon) QuoteReply(upd *tg.Update, content string) error {
|
|
|
|
msg := tg.NewMessage((*upd).Message.Chat.ID, content)
|
|
|
|
msg.ReplyToMessageID = (*upd).Message.MessageID
|
|
|
|
_, err := t.bot.Send(msg)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-25 22:03:13 +00:00
|
|
|
var escapedChars = []string{"_", "\\_", "*", "\\*", "[", "\\[", "]", "\\]", "(", "\\(", ")", "\\)", "~", "\\~", "`", "\\`", ">", "\\>", "#", "\\#", "+", "\\+", "-", "\\-", "=", "\\=", "|", "\\|", "{", "\\{", "}", "\\}", ".", "\\.", "!", "\\!"}
|
|
|
|
var escaper = strings.NewReplacer(escapedChars...)
|
|
|
|
|
2021-05-07 15:33:44 +00:00
|
|
|
// Send will send a telegram message to a list of chat IDs. message.text is used if no markdown is given.
|
2021-05-07 15:06:47 +00:00
|
|
|
func (t *TelegramDaemon) Send(message *Message, ID ...int64) error {
|
|
|
|
for _, id := range ID {
|
2021-05-07 15:33:44 +00:00
|
|
|
var msg tg.MessageConfig
|
|
|
|
if message.Markdown == "" {
|
|
|
|
msg = tg.NewMessage(id, message.Text)
|
|
|
|
} else {
|
2021-05-25 22:03:13 +00:00
|
|
|
text := escaper.Replace(message.Markdown)
|
2021-05-22 22:32:30 +00:00
|
|
|
msg = tg.NewMessage(id, text)
|
2021-05-07 15:33:44 +00:00
|
|
|
msg.ParseMode = "MarkdownV2"
|
|
|
|
}
|
2021-05-07 15:06:47 +00:00
|
|
|
_, err := t.bot.Send(msg)
|
2021-05-07 00:08:12 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TelegramDaemon) Shutdown() {
|
|
|
|
t.Stopped = true
|
|
|
|
t.ShutdownChannel <- "Down"
|
|
|
|
<-t.ShutdownChannel
|
|
|
|
close(t.ShutdownChannel)
|
|
|
|
}
|
2021-05-16 14:00:13 +00:00
|
|
|
|
|
|
|
func (t *TelegramDaemon) commandStart(upd *tg.Update, sects []string, lang string) {
|
|
|
|
content := t.app.storage.lang.Telegram[lang].Strings.get("startMessage") + "\n"
|
2021-05-18 17:41:42 +00:00
|
|
|
content += t.app.storage.lang.Telegram[lang].Strings.template("languageMessage", tmpl{"command": "/lang"})
|
2021-05-16 14:00:13 +00:00
|
|
|
err := t.Reply(upd, content)
|
|
|
|
if err != nil {
|
|
|
|
t.app.err.Printf("Telegram: Failed to send message to \"%s\": %v", upd.Message.From.UserName, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TelegramDaemon) commandLang(upd *tg.Update, sects []string, lang string) {
|
|
|
|
if len(sects) == 1 {
|
2021-05-18 17:41:42 +00:00
|
|
|
list := "/lang `<lang>`\n"
|
2021-05-16 14:00:13 +00:00
|
|
|
for code := range t.app.storage.lang.Telegram {
|
2021-05-18 17:41:42 +00:00
|
|
|
list += fmt.Sprintf("`%s`: %s\n", code, t.app.storage.lang.Telegram[code].Meta.Name)
|
2021-05-16 14:00:13 +00:00
|
|
|
}
|
|
|
|
err := t.Reply(upd, list)
|
|
|
|
if err != nil {
|
|
|
|
t.app.err.Printf("Telegram: Failed to send message to \"%s\": %v", upd.Message.From.UserName, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, ok := t.app.storage.lang.Telegram[sects[1]]; ok {
|
|
|
|
t.languages[upd.Message.Chat.ID] = sects[1]
|
|
|
|
for jfID, user := range t.app.storage.telegram {
|
|
|
|
if user.ChatID == upd.Message.Chat.ID {
|
|
|
|
user.Lang = sects[1]
|
|
|
|
t.app.storage.telegram[jfID] = user
|
2021-05-18 17:41:42 +00:00
|
|
|
if err := t.app.storage.storeTelegramUsers(); err != nil {
|
2021-05-16 14:00:13 +00:00
|
|
|
t.app.err.Printf("Failed to store Telegram users: %v", err)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TelegramDaemon) commandPIN(upd *tg.Update, sects []string, lang string) {
|
|
|
|
tokenIndex := -1
|
|
|
|
for i, token := range t.tokens {
|
|
|
|
if upd.Message.Text == token {
|
|
|
|
tokenIndex = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if tokenIndex == -1 {
|
|
|
|
err := t.QuoteReply(upd, t.app.storage.lang.Telegram[lang].Strings.get("invalidPIN"))
|
|
|
|
if err != nil {
|
|
|
|
t.app.err.Printf("Telegram: Failed to send message to \"%s\": %v", upd.Message.From.UserName, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err := t.QuoteReply(upd, t.app.storage.lang.Telegram[lang].Strings.get("pinSuccess"))
|
|
|
|
if err != nil {
|
|
|
|
t.app.err.Printf("Telegram: Failed to send message to \"%s\": %v", upd.Message.From.UserName, err)
|
|
|
|
}
|
2021-05-17 22:42:33 +00:00
|
|
|
t.verifiedTokens = append(t.verifiedTokens, TelegramVerifiedToken{
|
2021-05-16 14:00:13 +00:00
|
|
|
Token: upd.Message.Text,
|
|
|
|
ChatID: upd.Message.Chat.ID,
|
|
|
|
Username: upd.Message.Chat.UserName,
|
|
|
|
})
|
|
|
|
t.tokens[len(t.tokens)-1], t.tokens[tokenIndex] = t.tokens[tokenIndex], t.tokens[len(t.tokens)-1]
|
|
|
|
t.tokens = t.tokens[:len(t.tokens)-1]
|
|
|
|
}
|