first commit
This commit is contained in:
59
services/tgbot/bot.go
Normal file
59
services/tgbot/bot.go
Normal file
@@ -0,0 +1,59 @@
|
||||
package tgbot
|
||||
|
||||
import (
|
||||
"log"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"tg-mc/conf"
|
||||
|
||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var (
|
||||
bot *tgbotapi.BotAPI
|
||||
)
|
||||
|
||||
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)}}
|
||||
bot, err = tgbotapi.NewBotAPIWithClient(
|
||||
conf.GetBotSettings().BotToken,
|
||||
tgbotapi.APIEndpoint,
|
||||
client)
|
||||
|
||||
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
|
||||
updates := 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)
|
||||
sendFunc(update.Message.CommandArguments())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func SendMsg(msg string) error {
|
||||
msgT := tgbotapi.NewMessage(conf.GetBotSettings().GroupID, msg)
|
||||
_, err := bot.Send(msgT)
|
||||
return err
|
||||
}
|
Reference in New Issue
Block a user