コード例 #1
0
ファイル: bot.go プロジェクト: timblair/slack-archivebot
func archiveChannels(api *slack.Slack, c []slack.Channel, reason string) {
	var wg sync.WaitGroup

	for _, channel := range c {
		fmt.Printf("Archiving #%s (%s) due to %s\n", channel.Name, channel.Id, reason)
		wg.Add(1)

		go func(c slack.Channel) {
			defer wg.Done()
			if err := api.ArchiveChannel(c.Id); err != nil {
				message := fmt.Sprintf(
					"Error archiving channel #%s (%s): %s\n", c.Name, c.Id, err)
				log.Printf(message)
				// send error message in a DM to onErrorNotify user/channel
				onErrorNotify := os.Getenv("ARCHIVEBOT_NOTIFY")
				if onErrorNotify != "" {
					params := slack.PostMessageParameters{}
					if _, _, postMessageError := api.PostMessage(
						onErrorNotify, message, params); postMessageError != nil {
						postMessageErrorMessage := fmt.Sprintf(
							"Error posting error message to Slack: %s\n", postMessageError)
						log.Printf(postMessageErrorMessage)
					}
				}
			}
		}(channel)
	}

	wg.Wait()
}
コード例 #2
0
func createFormForEverybody(formMessage *slack.MessageEvent, api *slack.Slack) {
	users, err := api.GetUsers()
	if err != nil {
		fmt.Println("Channel err ", err)
	}

	for _, user := range users {
		//create user form DONE
		//send dm to fill out form DONE

		formResp := createFormForUser(formMessage, user.Id)
		if user.Name == "typebot" || user.Id == "U085QG8D7" {
			continue
		}
		_, _, chanId, err := api.OpenIMChannel(user.Id)
		if err != nil {
			fmt.Println("IM err ", err)
		}

		fmt.Println("formResp ", formResp)
		formDM := fmt.Sprintf("A form has been created, go fill it out! %v", formResp.Links[1].Href)

		api.PostMessage(chanId, formDM, slack.PostMessageParameters{
			AsUser: true,
		})
	}
}
コード例 #3
0
ファイル: main.go プロジェクト: thecalvinchan/linkr
func SendIRCToSlack(event *irc.Event, slackAPI *slack.Slack) {
	params := slack.PostMessageParameters{
		Username: fmt.Sprintf("(IRC) %s", event.Nick),
		AsUser:   true,
	}
	_, _, err := slackAPI.PostMessage(SlackChannel, event.Message(), params)
	if err != nil {
		log.Println("SendIRCToSlack:", err)
	} else {
		fmt.Println("IRC -> Slack:", event.Message())
	}
}
コード例 #4
0
func dmPerson(user *slack.User, message string, api *slack.Slack) {
	if user.Name == "typebot" {
		return
	}

	_, _, chanId, err := api.OpenIMChannel(user.Id)
	if err != nil {
		fmt.Println("IM err ", err)
	}
	api.PostMessage(chanId, message, slack.PostMessageParameters{
		AsUser: true,
	})
}
コード例 #5
0
func dmAll(message string, api *slack.Slack) {

	users, err := api.GetUsers()
	if err != nil {
		fmt.Println("Channel err ", err)
	}

	for _, user := range users {
		if user.Name == "typebot" {
			continue
		}

		_, _, chanId, err := api.OpenIMChannel(user.Id)
		if err != nil {
			fmt.Println("IM err ", err)
		}
		api.PostMessage(chanId, message, slack.PostMessageParameters{
			AsUser: true,
		})
	}
}
コード例 #6
0
ファイル: main.go プロジェクト: dpzaba/cbot
// handleMessage receives an event and passes it to the MessageResponders
func handleMessage(c *slack.Slack, e *slack.MessageEvent, responders []*MessageResponder) {
	if e.Username == "cbot" {
		return
	}

	content, args, err := parseMessageContent(e)
	if err != nil {
		log.Printf("Error parsing message: %v", err)
		return
	}

	if len(content) == 0 {
		return
	}
	// determine if this is a direct message (prefixed with bots name)
	direct := len(args) > 0 && args[0] == *prefix

	// handle 'help' command
	if direct && (len(args) == 1 || (len(args) > 1 && args[1] == "help")) {

		helpTxt := bytes.NewBufferString("I understand:\n")
		for _, r := range responders {
			if r.Name[0] == '_' {
				continue
			}
			helpTxt.WriteString(fmt.Sprintf("    %s %s\n", *prefix, r.Name))
		}
		params := slack.PostMessageParameters{Username: *prefix}
		if _, _, err := c.PostMessage(e.Channel, helpTxt.String(), params); err != nil {
			log.Println(err)
		}
		return
	}

	directHandled := !direct

	user, err := c.GetUserInfo(e.Msg.User)

	os.Setenv("CURRENT_FLOW", e.Channel)
	os.Setenv("CURRENT_USER_AVATAR", user.Profile.ImageOriginal)
	os.Setenv("CURRENT_USER_EMAIL", user.Profile.Email)
	os.Setenv("CURRENT_USER_NICK", user.Name)
	os.Setenv("CURRENT_USER_NAME", user.Profile.RealName)
	os.Setenv("CURRENT_USER_ID", string(user.ID))

	for _, responder := range responders {

		caught, err := responder.Handle(direct, content, args[1:], func(response string) error {
			// handle the output of the command by replying to the message
			params := slack.PostMessageParameters{Username: *prefix}
			_, _, error := c.PostMessage(e.Channel, response, params)
			return error
		})
		if err != nil {
			log.Println(err)
			continue
		}
		if caught && direct {
			directHandled = true
		}
	}
	// handle case when a direct message wasn't handled
	if !directHandled {
		log.Printf("Unhandled direct message: %s", content)
		resp := "Sorry, didn't recognize that command. Try 'cbot help'"
		params := slack.PostMessageParameters{Username: *prefix}
		if _, _, err := c.PostMessage(e.Channel, resp, params); err != nil {
			log.Println(err)
		}
	}
}