func (c *Config) handleConfigGet(b core.Bot, m *slack.Message) error { messageWithoutMentions := util.TrimWhitespace(core.LessMentions(m.Text)) parts := core.ExtractSubMatches(messageWithoutMentions, "^config:(.+)") if len(parts) < 2 { return exception.Newf("malformed message for `%s`", ActionConfigGet) } key := parts[1] value := b.Configuration()[key] return b.Sayf(m.Channel, "> %s: `%s` = %s", ActionConfigGet, key, value) }
func (c *Config) handleUnloadModule(b core.Bot, m *slack.Message) error { messageWithoutMentions := util.TrimWhitespace(core.LessMentions(m.Text)) parts := core.ExtractSubMatches(messageWithoutMentions, "^module:unload (.+)") if len(parts) < 2 { return exception.Newf("malformed message for `%s`", ActionModuleUnload) } key := parts[1] if !b.LoadedModules().Contains(key) { return b.Sayf(m.Channel, "Module `%s` isn't loaded.", key) } if !b.RegisteredModules().Contains(key) { return b.Sayf(m.Channel, "Module `%s` isn't registered.", key) } b.UnloadModule(key) return b.Sayf(m.Channel, "Unloaded Module `%s`.", key) }
func (c *Config) handleConfigSet(b core.Bot, m *slack.Message) error { messageWithoutMentions := util.TrimWhitespace(core.LessMentions(m.Text)) parts := core.ExtractSubMatches(messageWithoutMentions, "^config:(.+) (.+)") if len(parts) < 3 { return exception.Newf("malformed message for `%s`", ActionConfigSet) } key := parts[1] value := parts[2] setting := value if core.LikeAny(value, "true", "yes", "on", "1") { setting = "true" } else if core.LikeAny(value, "false", "off", "0") { setting = "false" } b.Configuration()[key] = setting return b.Sayf(m.Channel, "> %s: `%s` = %s", ActionConfigSet, key, setting) }
func (s *Stocks) handleStockChart(b core.Bot, m *slack.Message) error { messageWithoutMentions := util.TrimWhitespace(core.LessMentions(m.Text)) args := core.ExtractSubMatches(messageWithoutMentions, "^stock:chart (.*)") if len(args) < 2 { return exception.Newf("invalid input for %s", ActionStockPrice) } pieces := strings.Split(args[1], " ") ticker := pieces[0] timeframe := "1M" if len(pieces) > 1 { timeframe = pieces[1] } var imageURL string if strings.Contains(ticker, "+") { tickerPieces := strings.Split(ticker, "+") if len(tickerPieces) < 2 { return errors.New("invalid combination ticker") } imageURL = fmt.Sprintf("https://chart-service.charczuk.com/stock/chart/%s/%s?width=768&height=280&use_pct=true&add_sma=true&format=png&compare=%s", tickerPieces[0], timeframe, tickerPieces[1]) } else { imageURL = fmt.Sprintf("https://chart-service.charczuk.com/stock/chart/%s/%s?width=768&height=280&add_sma=true&format=png", ticker, timeframe) } leadText := fmt.Sprintf("Historical Chart for `%s`", ticker) message := slack.NewChatMessage(m.Channel, leadText) message.AsUser = slack.OptionalBool(true) message.UnfurlLinks = slack.OptionalBool(false) message.Parse = util.OptionalString("full") message.Attachments = []slack.ChatMessageAttachment{ slack.ChatMessageAttachment{ Title: util.OptionalString("Chart"), ImageURL: util.OptionalString(imageURL), }, } _, err := b.Client().ChatPostMessage(message) return err }
func (s *Stocks) handleStockPrice(b core.Bot, m *slack.Message) error { messageWithoutMentions := util.TrimWhitespace(core.LessMentions(m.Text)) pieces := core.ExtractSubMatches(messageWithoutMentions, "^stock:price (.*)") if len(pieces) < 2 { return exception.Newf("invalid input for %s", ActionStockPrice) } rawTicker := pieces[1] tickers := []string{} if strings.Contains(rawTicker, ",") { tickers = strings.Split(rawTicker, ",") } else { tickers = []string{rawTicker} } stockInfo, err := external.StockPrice(tickers) if err != nil { return err } if len(stockInfo) == 0 { return b.Sayf(m.Channel, "No stock information returned for: `%s`", strings.Join(tickers, ", ")) } return s.announceStocks(b, m.Channel, stockInfo) }