Skip to content

Instantly share code, notes, and snippets.

@yagop yagop/config.toml
Last active Dec 9, 2016

Embed
What would you like to do?
Telegram bot torrent to folder
BotToken = "123456:AAAAAAA"
AdminId = 11696011
TorrentDownloadPath = "/tmp"
package main
import (
"gopkg.in/telegram-bot-api.v4"
"github.com/BurntSushi/toml"
"log"
)
type tomlConfig struct {
BotToken string
AdminId int
TorrentDownloadPath string
}
var config tomlConfig
var replyHandler ReplyIdHandler
func main() {
if _, err := toml.DecodeFile("config.toml", &config); err != nil {
log.Fatalln(err)
}
log.Printf("Bot token: %s\n", config.BotToken)
log.Printf("Admin ID: %d\n", config.AdminId)
log.Printf("TorrentDownloadPath: %s\n", config.TorrentDownloadPath)
bot, err := tgbotapi.NewBotAPI(config.BotToken)
bot.Debug = true
if err != nil {
log.Panic(err)
}
log.Printf("Bot name: %s\n", bot.Self.UserName)
replyHandler = ReplyIdHandler{}
botConfig := tgbotapi.UpdateConfig{0, 0, 30}
updates, err := bot.GetUpdatesChan(botConfig)
for update := range updates {
log.Printf("[%s] %s", update.Message.From.UserName, update.Message.Text)
if update.Message != nil {
torrentMessageHandler(&update, bot)
autoKickHandler(&update, bot)
replyHandler.process(&update, bot)
}
}
}
func torrentMessageHandler (update *tgbotapi.Update, bot *tgbotapi.BotAPI) {
fromId := int64(update.Message.From.ID)
if update.Message.From.ID == config.AdminId {
if update.Message.Text == "/loadtorrent" {
msg := tgbotapi.NewMessage(fromId, "Send me the torrent")
msg.ReplyMarkup = tgbotapi.ForceReply{true, true}
resp, err := bot.Send(msg)
if err != nil {
log.Fatal(err)
}
log.Printf("Message sended: %d", resp.MessageID)
replyHandler.add(resp.MessageID, func (update *tgbotapi.Update, bot *tgbotapi.BotAPI) {
if update.Message.From.ID == config.AdminId {
if update.Message.Document.MimeType == "application/x-bittorrent" {
file_name := update.Message.Document.FileName
log.Printf("Bittorrent file: %s\n", file_name)
url, err := bot.GetFileDirectURL(update.Message.Document.FileID)
if err != nil {
log.Fatal(err)
}
log.Printf("File URL: %s\n", url)
download_path := config.TorrentDownloadPath + "/" + file_name
go downloadFile(url, download_path)
}
log.Printf("Callback [%s] %s", update.Message.From.UserName, update.Message.Text)
}
})
}
}
}
package main
import (
"gopkg.in/telegram-bot-api.v4"
"log"
)
// Function callback type declaration
type MessageCallbackFunction func (update *tgbotapi.Update, bot *tgbotapi.BotAPI) ()
// Struct containing the original message Id and a callback to be executed
type MessageReplyCall struct {
messageId int
callback MessageCallbackFunction
}
type ReplyIdHandler struct {
Callbacks []MessageReplyCall
}
// Register a callback to be executed when arrives a message with ID messageId
func (handler *ReplyIdHandler) add (messageId int, callaback MessageCallbackFunction) {
log.Printf("Registered callback for message: %d", messageId)
handler.Callbacks = append(handler.Callbacks, MessageReplyCall{messageId, callaback})
}
// Execute the callbacks registered on messageReplyCallRegister
func (handler *ReplyIdHandler) process (update *tgbotapi.Update, bot *tgbotapi.BotAPI) {
if update.Message.ReplyToMessage != nil {
repliedMessageID := update.Message.ReplyToMessage.MessageID
log.Printf("ReplyToMessage: %d", repliedMessageID)
for _, callback := range handler.Callbacks {
if repliedMessageID == callback.messageId {
log.Printf("Calling callback: %x", callback.callback)
callback.callback(update, bot)
}
}
}
}
package main
import (
"net/http"
"log"
"io"
"os"
)
func downloadFile(url string, download_path string) (int64, error) {
out, err := os.Create(download_path)
defer out.Close()
resp, err := http.Get(url)
if err != nil {
log.Println(err)
return 0, err
}
defer resp.Body.Close()
n, err := io.Copy(out, resp.Body)
if err != nil {
log.Println(err)
return 0, err
}
log.Println("Written bytes", n)
return n, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.