コード例 #1
0
ファイル: bot.go プロジェクト: Tigerwhit4/pkg
// New is the constructor for Bot. The Bot will connect to the chat server
func New(host, key, secret, userPublicId string, confs ...Config) (*Bot, error) {
	b := &Bot{
		Room:    commands.NewRoom(userPublicId),
		Key:     key,
		Secret:  secret,
		started: time.Now().UTC(),
		state:   Disconnected,
		subs:    make(map[string]chan interface{}),
	}
	for _, conf := range confs {
		conf(b)
	}
	if b.logCommands {
		b.tcp = tcpclient.New(host, tcpclient.LogCommands)
	} else {
		b.tcp = tcpclient.New(host)
	}

	if err := b.isOnline(); err != nil {
		return nil, err
	}

	return b, nil
}
コード例 #2
0
ファイル: example.go プロジェクト: Tigerwhit4/pkg
// NOTE: for a higher abstraction use the bot package
func main() {
	// command line flags
	publicId := flag.String("publicId", "", "room you want to join to")
	key := flag.String("bot-key", "", "bot key")
	secret := flag.String("bot-secret", "", "bot secret")
	host := flag.String("chat-host", "www.stream.me:2020", "Chat server address")

	flag.Parse()

	// chat client.
	c := tcpclient.New(*host)

	// create room, gives us access to available chat room commands
	room := commands.NewRoom(*publicId)

	// authenticate
	if err := c.Write(room.Pass(*key, *secret), 0); err != nil {
		log.Fatal(err)
	}

	// join room
	if err := c.Write(room.Join(), 0); err != nil {
		log.Fatal(err)
	}

	// Hello, World!
	if err := c.Write(room.Say("Hello, World!"), 0); err != nil {
		log.Println("write error:", err)
	}

	// Leave the chat room
	if err := c.Write(room.Leave(), 0); err != nil {
		log.Println("write error:", err)
	}

	// close the client connection
	c.Close()
}