Example #1
0
func addHandlers(r victor.Robot) {
	// Add a typical command that will be displayed using the "help" command
	// if it is enabled.
	r.HandleCommand(&victor.HandlerDoc{
		CmdHandler:     byeFunc,
		CmdName:        "hi",
		CmdDescription: "Says goodbye when the user says hi!",
		CmdUsage:       []string{""},
	})
	// Add a hidden command that isn't displayed in the "help" command unless
	// mentioned by name
	r.HandleCommand(&victor.HandlerDoc{
		CmdHandler:     echoFunc,
		CmdName:        "echo",
		CmdDescription: "Hidden `echo` command!",
		CmdUsage:       []string{"", "`text to echo`"},
		CmdIsHidden:    true,
	})
	// Add a command to show the "Fields" method
	r.HandleCommand(&victor.HandlerDoc{
		CmdHandler:     fieldsFunc,
		CmdName:        "fields",
		CmdDescription: "Show the fields/parameters of a command message!",
		CmdUsage:       []string{"`param0` `param1` `...`"},
	})
	// Add a general pattern which is only checked on "non-command" messages
	// which are described in dispatch.go
	r.HandlePattern("\b(thanks|thank\\s+you)\b", thanksFunc)
	// Add default handler to show "unrecognized command" on "command" messages
	r.SetDefaultHandler(defaultFunc)
}
Example #2
0
// run and recover from panic during bot.Stop()
// the upstream slack adapter has not implemented bot.Stop() yet
func runBot(bot victor.Robot) {
	defer func() {
		if e := recover(); e != nil {
			fmt.Println("bot.Stop() exited with panic: ", e)
			os.Exit(0)
		}
	}()

	bot.Run()
	go monitorErrors(bot.ChatErrors())
	go monitorEvents(bot.ChatEvents())
	// keep the process (and bot) alive
	sigs := make(chan os.Signal, 1)
	signal.Notify(sigs, os.Interrupt)
	<-sigs
	bot.Stop()
}