Example #1
0
func main() {
	// Connect
	conn, err := connection.Connect(wsAddr, *apiKey, *secret)
	if err != nil {
		log.Fatal(err)
	}

	// Get info about ourself.
	addr := httpAddr + authutil.SignRequest("/api/whoami", *apiKey, *secret)
	// For some reason I can't verify the cert on pratchat.com :\
	transport := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	}
	client := &http.Client{Transport: transport}
	response, err := client.Get(addr)
	if err != nil {
		log.Fatal("Error fetching user info: " + err.Error())
	}
	var buf bytes.Buffer
	io.Copy(&buf, response.Body)
	userInfo := &bot.UserInfo{}
	if err := json.Unmarshal(buf.Bytes(), userInfo); err != nil {
		log.Fatal("Error getting user info: " + err.Error())
	}

	// Leave all current channels.
	for _, channel := range userInfo.User.Channels {
		conn.Leave(channel)
	}

	// Register bots
	for _, f := range bots {
		disp.Register(f(conn, userInfo))
	}

	log.Println("Bots started.")

	// Send 'connected' message
	connectedMsg := &bot.Event{
		Type: bot.EventConnect,
	}
	disp.Send(connectedMsg)

	// Loop, receiving messages, and send them through the dispatcher
	for {
		msg := <-conn.In
		disp.SendRaw(msg)
	}
}
Example #2
0
func (c *Conn) connect() error {
	connectionString := c.creds.addrString + authutil.SignRequest("/eventhub", c.creds.apiKey, c.creds.secret)
	config, err := websocket.NewConfig(connectionString, "http://localhost")
	if err != nil {
		return err
	}
	// Ignore certs for now
	config.TlsConfig = &tls.Config{InsecureSkipVerify: true}
	ws, err := websocket.DialConfig(config)
	if err != nil {
		return err
	}
	c.ws = ws
	return nil
}