init telegram
This commit is contained in:
parent
9f56711ea0
commit
844e316b24
@ -6,19 +6,28 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"gopkg.in/telegram-bot-api.v4"
|
||||||
|
|
||||||
"github.com/pyed/transmission"
|
"github.com/pyed/transmission"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
// flags
|
||||||
token string
|
token string
|
||||||
master string
|
master string
|
||||||
url string
|
url string
|
||||||
username string
|
username string
|
||||||
password string
|
password string
|
||||||
|
|
||||||
client *transmission.Client
|
// transmission
|
||||||
|
Client *transmission.Client
|
||||||
|
|
||||||
|
// telegram
|
||||||
|
Bot *tgbotapi.BotAPI
|
||||||
|
Updates <-chan tgbotapi.Update
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// init flags
|
||||||
func init() {
|
func init() {
|
||||||
// define arguments and parse them.
|
// define arguments and parse them.
|
||||||
flag.StringVar(&token, "token", "", "Telegram bot token")
|
flag.StringVar(&token, "token", "", "Telegram bot token")
|
||||||
@ -42,7 +51,10 @@ func init() {
|
|||||||
flag.Usage()
|
flag.Usage()
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// init transmission
|
||||||
|
func init() {
|
||||||
// set transmission.Config, needed to establish a connection with transmission
|
// set transmission.Config, needed to establish a connection with transmission
|
||||||
conf := transmission.Config{
|
conf := transmission.Config{
|
||||||
Address: url,
|
Address: url,
|
||||||
@ -51,19 +63,40 @@ func init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// transmission.New() never returns an error, we will ignore it and test with client.Session.Update()
|
// transmission.New() never returns an error, we will ignore it and test with client.Session.Update()
|
||||||
client, _ = transmission.New(conf)
|
Client, _ = transmission.New(conf)
|
||||||
if err := client.Session.Update(); err != nil {
|
if err := Client.Session.Update(); err != nil {
|
||||||
|
|
||||||
// try to predict the error message, as it vague coming from pyed/transmission
|
// 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.
|
if strings.HasPrefix(err.Error(), "invalid character") { // means the user or the pass is wrong.
|
||||||
fmt.Fprintln(os.Stderr, "Transmission's Username or Password is wrong.\n")
|
fmt.Fprintf(os.Stderr, "Transmission's Username or Password is wrong.\n\n")
|
||||||
|
|
||||||
} else { // any other error is probaby because of the URL
|
} else { // any other error is probaby because of the URL
|
||||||
fmt.Fprintf(os.Stderr, "Error: Couldn't connect to: %s\n", url)
|
fmt.Fprintf(os.Stderr, "Error: Couldn't connect to: %s\n", url)
|
||||||
fmt.Fprintln(os.Stderr, "Make sure to pass the right full RPC URL e.g. http://localhost:9091/transmission/rpc\n")
|
fmt.Fprintf(os.Stderr, "Make sure to pass the right full RPC URL e.g. http://localhost:9091/transmission/rpc\n\n")
|
||||||
}
|
}
|
||||||
// send the vague error message too
|
// send the vague error message too
|
||||||
fmt.Fprintln(os.Stderr, "RawError: "+err.Error())
|
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)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user