示例#1
0
文件: core.go 项目: wcharczuk/jarvis
func (c *Core) handleMentionCatchAll(b core.Bot, m *slack.Message) error {
	message := util.TrimWhitespace(core.LessMentions(m.Text))
	if core.IsSalutation(message) {
		return c.handleSalutation(b, m)
	}
	return c.handleUnknown(b, m)
}
示例#2
0
文件: bot.go 项目: wcharczuk/jarvis
func (b *Bot) dispatchResponse(m *slack.Message) error {
	defer func() {
		if r := recover(); r != nil {
			b.Sayf(m.Channel, "there was a panic handling the message:\n> %v", r)
		}
	}()

	//b.LogIncomingMessage(m)
	user := b.FindUser(m.User)
	if user != nil {
		if m.User != "slackbot" && m.User != b.id && !user.IsBot {
			messageText := util.TrimWhitespace(core.LessMentions(m.Text))
			if core.IsUserMention(m.Text, b.id) || core.IsDM(m.Channel) {
				for _, action := range b.mentionActions {
					if core.Like(messageText, action.MessagePattern) && len(action.MessagePattern) != 0 {
						return action.Handler(b, m)
					}
				}
			}
			if b.passivesEnabled() {
				for _, action := range b.passiveActions {
					if core.Like(messageText, action.MessagePattern) && len(action.MessagePattern) != 0 {
						return action.Handler(b, m)
					}
				}
			}
		}
	}
	return nil
}
示例#3
0
文件: jobs.go 项目: wcharczuk/jarvis
func (j *Jobs) handleJobEnable(b core.Bot, m *slack.Message) error {
	messageWithoutMentions := util.TrimWhitespace(core.LessMentions(m.Text))
	pieces := strings.Split(messageWithoutMentions, " ")
	if len(pieces) > 1 {
		taskName := pieces[len(pieces)-1]
		b.JobManager().EnableJob(taskName)
		return b.Sayf(m.Channel, "enabled job `%s`", taskName)
	}
	return exception.New("unhandled response.")
}
示例#4
0
文件: core.go 项目: wcharczuk/jarvis
func (c *Core) handlePassiveCatchAll(b core.Bot, m *slack.Message) error {
	message := util.TrimWhitespace(core.LessMentions(m.Text))
	if optionValue, hasOption := b.Configuration()[ConfigOptionPassiveCatchAll]; hasOption && optionValue == "true" {
		if core.IsAngry(message) {
			user := b.FindUser(m.User)
			response := []string{"slow down %s", "maybe calm down %s", "%s you should really relax", "chill %s", "it's ok %s, let it out"}
			return b.Sayf(m.Channel, core.Random(response), strings.ToLower(user.Profile.FirstName))
		}
	}

	return nil
}
示例#5
0
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)
}
示例#6
0
文件: jira.go 项目: wcharczuk/jarvis
func (j *Jira) handleJira(b core.Bot, m *slack.Message) error {
	text := core.LessMentions(m.Text)

	issueIds := j.extractJiraIssues(text)
	if len(issueIds) == 0 {
		return nil
	}

	issues, err := j.fetchJiraIssues(b, issueIds)
	if err != nil {
		return err
	}
	if len(issues) == 0 {
		return nil
	}

	user := b.FindUser(m.User)

	leadText := fmt.Sprintf("*%s* has mentioned the following jira issues (%d): ", user.Profile.FirstName, len(issues))
	message := slack.NewChatMessage(m.Channel, leadText)
	message.AsUser = slack.OptionalBool(true)
	message.UnfurlLinks = slack.OptionalBool(false)
	for _, issue := range issues {
		if !util.IsEmpty(issue.Key) {
			var itemText string
			if issue.Fields != nil {
				assignee := "Unassigned"
				if issue.Fields.Assignee != nil {
					assignee = issue.Fields.Assignee.DisplayName
				}
				itemText = fmt.Sprintf("%s %s\nAssigned To: %s",
					fmt.Sprintf("https://%s/browse/%s", b.Configuration()[ConfigJiraHost], issue.Key),
					issue.Fields.Summary,
					assignee,
				)
			} else {
				itemText = fmt.Sprintf("%s\n%s", issue.Key, fmt.Sprintf("https://%s/browse/%s", b.Configuration()[ConfigJiraHost], issue.Key))
			}

			item := slack.ChatMessageAttachment{
				Color: slack.OptionalString("#3572b0"),
				Text:  slack.OptionalString(itemText),
			}
			message.Attachments = append(message.Attachments, item)
		}
	}

	_, err = b.Client().ChatPostMessage(message)
	if err != nil {
		fmt.Printf("issue posting message: %v\n", err)
	}
	return err
}
示例#7
0
文件: jobs.go 项目: wcharczuk/jarvis
func (j *Jobs) handleJobRun(b core.Bot, m *slack.Message) error {
	messageWithoutMentions := util.TrimWhitespace(core.LessMentions(m.Text))
	pieces := strings.Split(messageWithoutMentions, " ")
	if len(pieces) > 1 {
		jobName := pieces[len(pieces)-1]
		b.JobManager().RunJob(jobName)
		return b.Sayf(m.Channel, "ran job `%s`", jobName)
	}

	b.JobManager().RunAllJobs()
	return b.Say(m.Channel, "ran all jobs")
}
示例#8
0
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)
}
示例#9
0
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)
}
示例#10
0
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
}
示例#11
0
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)
}