package main
import (
"fmt"
"log"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
)
func main() {
bot, err := tgbotapi.NewBotAPI("token")
if err != nil {
log.Panic(err)
}
bot.Debug = false
log.Printf("Authorized on account %s", bot.Self.UserName)
updateConfig := tgbotapi.NewUpdate(0)
// Tell Telegram we should wait up to 30 seconds on each request for an
// update. This way we can get information just as quickly as making many
// frequent requests without having to send nearly as many.
updateConfig.Timeout = 60
// Start polling Telegram for updates.
// Let's go through each update that we're getting from Telegram.
for update := range updates {
if update.Message == nil { // ignore any non-Message updates
continue
}
if !update.Message.IsCommand() { // ignore any non-command Messages
continue
}
msg := tgbotapi.NewMessage(update.Message.Chat.ID, "")
switch update.Message.Command() {
case "upload":
file := tgbotapi.FilePath("1.mp4")
msg2 := tgbotapi.NewVideo(update.Message.Chat.ID, file)
sendmsg, err := bot.Send(msg2)
if err != nil {
log.Panic(err)
}
url, _ := bot.GetFileDirectURL(sendmsg.Video.FileID)
fmt.Println(url)//打印出直链
default:
msg.Text = "I don't know that command"
}
}
}
当向机器人发送/upload命令后就会开始上传
使用 Go-Telegram-Bot-Api 向TG机器人发送文件并获取直链
未经允许不得转载:家里蹲的狐狸 » 使用 Go-Telegram-Bot-Api 向TG机器人发送文件并获取直链