This repository has been archived on 2023-07-05. You can view files and clone it, but cannot push or open issues or pull requests.
clessy/broker/main.go

53 lines
1,023 B
Go
Raw Normal View History

2016-02-08 13:47:10 +00:00
package main
import (
"encoding/json"
"flag"
"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:52:13 +00:00
var api Telegram
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() {
http.HandlerFunc(config.Token, webhook)
err := http.ListenAndServe(config.BindServer, nil)
assert(err)
}()
2016-02-08 16:52:13 +00:00
// Register webhook @ Telegram
api.setWebhook(config.BaseURL + config.WebhookURL)
2016-02-08 13:47:10 +00:00
// Create server for clients
startClientsServer(config.BindClients)
}