59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
package tgbot
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"net/url"
|
|
"tg-mc/conf"
|
|
"tg-mc/services/utils"
|
|
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
func Run(sendFunc func(string) error) {
|
|
var err error
|
|
|
|
HttpProxy := conf.GetBotSettings().HTTPProxy
|
|
proxyUrl, err := url.Parse(HttpProxy)
|
|
if err != nil {
|
|
log.Panic(err, "HTTP_PROXY environment variable is not set correctly")
|
|
}
|
|
|
|
client := &http.Client{Transport: &http.Transport{Proxy: http.ProxyURL(proxyUrl)}}
|
|
conf.Bot, err = tgbotapi.NewBotAPIWithClient(
|
|
conf.GetBotSettings().BotToken,
|
|
tgbotapi.APIEndpoint,
|
|
client)
|
|
|
|
if err != nil {
|
|
log.Panic(err)
|
|
}
|
|
|
|
conf.Bot.Debug = false
|
|
|
|
log.Printf("Authorized on account %s", conf.Bot.Self.UserName)
|
|
|
|
u := tgbotapi.NewUpdate(0)
|
|
u.Timeout = 60
|
|
updates := conf.Bot.GetUpdatesChan(u)
|
|
|
|
for update := range updates {
|
|
if update.Message != nil {
|
|
logrus.Infof("[%s] %s", update.Message.From.UserName, update.Message.Text)
|
|
if update.Message.Command() == "talk" {
|
|
logrus.Infof("id is %d", update.Message.Chat.ID)
|
|
m := fmt.Sprintf("%v: %v", update.Message.From.UserName, update.Message.CommandArguments())
|
|
err := sendFunc(m)
|
|
logrus.WithError(err).Error("send message error")
|
|
}
|
|
if update.Message.Command() == "list" {
|
|
logrus.Infof("id is %d", update.Message.Chat.ID)
|
|
m := tgbotapi.NewMessage(update.Message.Chat.ID, utils.GetAlivePlayer())
|
|
conf.Bot.Send(m)
|
|
}
|
|
}
|
|
}
|
|
}
|