transmission-telegram/transmission-telegram.go

105 lines
2.6 KiB
Go
Raw Normal View History

2016-06-19 08:07:00 +03:00
package main
import (
"flag"
"fmt"
"os"
"strings"
2016-06-19 22:02:15 +03:00
"gopkg.in/telegram-bot-api.v4"
2016-06-19 08:07:00 +03:00
"github.com/pyed/transmission"
)
var (
2016-06-19 22:02:15 +03:00
// flags
2016-06-19 08:07:00 +03:00
token string
master string
url string
username string
password string
2016-06-19 22:02:15 +03:00
// transmission
Client *transmission.Client
// telegram
Bot *tgbotapi.BotAPI
Updates <-chan tgbotapi.Update
2016-06-19 08:07:00 +03:00
)
2016-06-19 22:02:15 +03:00
// init flags
2016-06-19 08:07:00 +03:00
func init() {
// define arguments and parse them.
flag.StringVar(&token, "token", "", "Telegram bot token")
flag.StringVar(&master, "master", "", "Your telegram handler, So the bot will only respond to you")
flag.StringVar(&url, "url", "http://localhost:9091/transmission/rpc", "Transmission RPC URL")
flag.StringVar(&username, "username", "", "Transmission username")
flag.StringVar(&password, "password", "", "Transmission password")
// set the usage message
flag.Usage = func() {
fmt.Fprintln(os.Stderr, "Usage: transmission-bot -token=<TOKEN> -master=<@tuser> -url=[http://] -username=[user] -password=[pass]\n")
flag.PrintDefaults()
}
flag.Parse()
// make sure that we have the two madatory arguments: telegram token & master's handler.
if token == "" ||
master == "" {
fmt.Fprintf(os.Stderr, "Error: Mandatory argument missing!\n\n")
flag.Usage()
os.Exit(1)
}
2016-06-19 22:02:15 +03:00
}
2016-06-19 08:07:00 +03:00
2016-06-19 22:02:15 +03:00
// init transmission
func init() {
2016-06-19 08:07:00 +03:00
// set transmission.Config, needed to establish a connection with transmission
conf := transmission.Config{
Address: url,
User: username,
Password: password,
}
// transmission.New() never returns an error, we will ignore it and test with client.Session.Update()
2016-06-19 22:02:15 +03:00
Client, _ = transmission.New(conf)
if err := Client.Session.Update(); err != nil {
2016-06-19 08:07:00 +03:00
// try to predict the error message, as it vague coming from pyed/transmission
if strings.HasPrefix(err.Error(), "invalid character") { // means the user or the pass is wrong.
2016-06-19 22:02:15 +03:00
fmt.Fprintf(os.Stderr, "Transmission's Username or Password is wrong.\n\n")
2016-06-19 08:07:00 +03:00
} else { // any other error is probaby because of the URL
fmt.Fprintf(os.Stderr, "Error: Couldn't connect to: %s\n", url)
2016-06-19 22:02:15 +03:00
fmt.Fprintf(os.Stderr, "Make sure to pass the right full RPC URL e.g. http://localhost:9091/transmission/rpc\n\n")
2016-06-19 08:07:00 +03:00
}
// send the vague error message too
2016-06-19 22:02:15 +03:00
fmt.Fprintf(os.Stderr, "JSONError: %s\n", err)
os.Exit(1)
}
}
// init telegram
func init() {
// authorize using the token
var err error
Bot, err = tgbotapi.NewBotAPI(token)
if err != nil {
fmt.Fprintf(os.Stderr, "Telegram Error: %s\n", err)
os.Exit(1)
}
// get a channel and sign it to 'Updates'
u := tgbotapi.NewUpdate(0)
u.Timeout = 60
Updates, err = Bot.GetUpdatesChan(u)
if err != nil {
fmt.Fprintf(os.Stderr, "Telegram Error: %s\n", err)
2016-06-19 08:07:00 +03:00
os.Exit(1)
}
}
func main() {}