Пример #1
0
func PipeSlackToIRC(slackAPI *slack.Slack, ircLink *irc.Connection) {
	//sender := make(chan slack.OutgoingMessage)
	receiver := make(chan slack.SlackEvent)
	wsAPI, err := slackAPI.StartRTM("", "http://example.com")
	if err != nil {
		log.Fatalf("StartRTM() error: %s", err)
	}
	go wsAPI.HandleIncomingEvents(receiver)
	go wsAPI.Keepalive(10 * time.Second)
	for {
		msg := <-receiver
		switch msg.Data.(type) {
		case *slack.MessageEvent:
			msgEvent := msg.Data.(*slack.MessageEvent)
			// Ignore bot messages, including our own
			if msgEvent.BotId != "" {
				break
			}

			fmt.Printf("Message: %s\n", msgEvent)
			user, err := slackAPI.GetUserInfo(msgEvent.UserId)
			if err != nil {
				log.Printf("GetUserInfo(): %s\n", err)
				break
			}
			msg := fmt.Sprintf("(Slack) <%s> %s", user.Profile.RealName, unescapeMessage(msgEvent.Text))
			ircLink.Privmsg(IRCChannel, msg)
			fmt.Println("Slack -> IRC:", msg)
		}
	}
}
Пример #2
0
// 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)
		}
	}
}