64 lines
1.2 KiB
Go
64 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"strconv"
|
|
|
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
|
|
)
|
|
|
|
var bot *tgbotapi.BotAPI
|
|
var groupID int64
|
|
var botAPIKey string
|
|
|
|
func botInit() {
|
|
var err error
|
|
botAPIKey = os.Getenv("TELEGRAM_APIKEY")
|
|
groupID, err = strconv.ParseInt(os.Getenv("TELEGRAM_GROUPID"), 10, 64)
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
|
|
bot, err = tgbotapi.NewBotAPI(botAPIKey)
|
|
if err != nil {
|
|
log.Println(err)
|
|
} else {
|
|
log.Printf("Bot 名称: %s", bot.Self.UserName)
|
|
}
|
|
}
|
|
|
|
func telegramBridge() {
|
|
u := tgbotapi.NewUpdate(0)
|
|
u.Timeout = 60
|
|
updates, err := bot.GetUpdatesChan(u)
|
|
if err != nil {
|
|
log.Println(err)
|
|
}
|
|
|
|
for update := range updates {
|
|
if update.Message == nil {
|
|
continue
|
|
}
|
|
|
|
log.Printf("<%s> %s", update.Message.From.UserName, update.Message.Text)
|
|
|
|
if update.Message.IsCommand() {
|
|
//handleCommands(bot, update)
|
|
continue
|
|
}
|
|
|
|
if update.Message.Chat.ID == groupID {
|
|
sendToArma(update.Message.From.UserName, update.Message.Text)
|
|
}
|
|
}
|
|
}
|
|
|
|
func sendToTelegram(player, message string) {
|
|
msgText := fmt.Sprintf("[Arma3] <%s> %s\n", player, message)
|
|
log.Println(msgText)
|
|
msg := tgbotapi.NewMessage(groupID, msgText)
|
|
bot.Send(msg)
|
|
}
|