Exemple #1
0
// NewShell returns a new Shell Adapter.
func NewShell(b bot.Bot) bot.Adapter {
	// Get terminal old state.
	state, err := t.MakeRaw(int(os.Stdin.Fd()))
	if err != nil {
		panic(err)
	}

	// Create a new terminal struct.
	term := t.NewTerminal(os.Stdin, "")
	prompt := fmt.Sprintf("\033[36m%s\033[0m> ", b.Name)
	term.SetPrompt(prompt)

	return Shell{
		bot:         b,
		MessagePool: bot.NewMessagePool(),
		Terminal:    term,
		State:       state,
	}
}
Exemple #2
0
// NewSlack returns a new Slack Adapter.
func NewSlack(b bot.Bot) bot.Adapter {
	// token
	if token = os.Getenv("SLACK_API_TOKEN"); token == "" {
		panic("SLACK_API_TOKEN env is require.")
	}

	// Real Time Messaging Start
	resp, err := http.Get(baseURL + "/rtm.start?token=" + token)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	// Get Web socket URL
	var data struct {
		Ok    bool
		URL   string `json:"url"`
		Error string
	}
	body, _ := ioutil.ReadAll(resp.Body)
	if json.Unmarshal(body, &data); !data.Ok {
		panic(data.Error)
	}

	// Dial to API via websocket
	ws, err := websocket.Dial(data.URL, "", baseURL)
	if err != nil {
		panic(err)
	}

	return Slack{
		bot:         b,
		MessagePool: bot.NewMessagePool(),
		Conn:        ws,
	}
}