2016-02-08 13:47:10 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
2016-02-08 17:05:46 +00:00
|
|
|
"log"
|
2016-02-08 13:47:10 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
BindServer string /* Address:Port to bind for Telegram */
|
|
|
|
BindClients string /* Address:Port to bind for clients */
|
|
|
|
Token string /* Telegram bot token */
|
2016-02-08 16:52:13 +00:00
|
|
|
BaseURL string /* Base URL for webhook */
|
2016-02-08 13:47:10 +00:00
|
|
|
WebhookURL string /* Webhook URL */
|
|
|
|
}
|
|
|
|
|
|
|
|
func assert(err error) {
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-08 16:57:27 +00:00
|
|
|
var api *Telegram
|
2016-02-08 16:52:13 +00:00
|
|
|
|
2016-02-08 13:47:10 +00:00
|
|
|
func main() {
|
|
|
|
cfgpath := flag.String("config", "config.json", "Path to configuration file")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
file, err := os.Open(*cfgpath)
|
|
|
|
assert(err)
|
|
|
|
|
|
|
|
var config Config
|
|
|
|
err = json.NewDecoder(file).Decode(&config)
|
|
|
|
assert(err)
|
|
|
|
|
2016-02-08 16:52:13 +00:00
|
|
|
// Create Telegram API object
|
|
|
|
api = mkAPI(config.Token)
|
|
|
|
|
2016-02-08 13:47:10 +00:00
|
|
|
// Setup webhook handler
|
|
|
|
go func() {
|
2016-02-08 17:05:46 +00:00
|
|
|
log.Println("Starting webserver..")
|
2016-02-09 09:25:08 +00:00
|
|
|
http.HandleFunc(config.WebhookURL, webhook)
|
2016-02-08 13:47:10 +00:00
|
|
|
err := http.ListenAndServe(config.BindServer, nil)
|
|
|
|
assert(err)
|
|
|
|
}()
|
|
|
|
|
2016-02-08 16:52:13 +00:00
|
|
|
// Register webhook @ Telegram
|
2016-02-08 17:05:46 +00:00
|
|
|
log.Println("Registering webhook..")
|
2016-02-09 10:33:38 +00:00
|
|
|
api.SetWebhook(config.BaseURL + config.WebhookURL)
|
2016-02-08 16:52:13 +00:00
|
|
|
|
2016-02-08 13:47:10 +00:00
|
|
|
// Create server for clients
|
2016-02-08 17:05:46 +00:00
|
|
|
log.Println("Starting clients server..")
|
2016-02-08 13:47:10 +00:00
|
|
|
startClientsServer(config.BindClients)
|
|
|
|
}
|