示例#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)
		}
	}
}
func setupRTM(api *slack.Slack) {
	chSender := make(chan slack.OutgoingMessage)
	chReceiver := make(chan slack.SlackEvent)

	wsAPI, err := api.StartRTM("", "http://example.com")
	if err != nil {
		fmt.Errorf("%s\n", err)
	}

	go wsAPI.HandleIncomingEvents(chReceiver)
	go wsAPI.Keepalive(20 * time.Second)
	go func(wsAPI *slack.SlackWS, chSender chan slack.OutgoingMessage) {
		for {
			select {
			case msg := <-chSender:
				wsAPI.SendMessage(&msg)
			}
		}
	}(wsAPI, chSender)
	for {
		select {
		case msg := <-chReceiver:
			fmt.Print("Event Received: ")
			switch msg.Data.(type) {
			case slack.HelloEvent:
			// Ignore hello
			case *slack.MessageEvent:
				a := msg.Data.(*slack.MessageEvent)
				fmt.Printf("Message: %v\n", a)
				fmt.Printf("ChanID:", a.ChannelId)
				if strings.Contains(a.Text, "U085QG8D7") {
					createFormForEverybody(a, api)
				}
			case *slack.PresenceChangeEvent:
				a := msg.Data.(*slack.PresenceChangeEvent)
				fmt.Printf("Presence Change: %v\n", a)
			case slack.LatencyReport:
				a := msg.Data.(slack.LatencyReport)
				fmt.Printf("Current latency: %v\n", a.Value)
			case *slack.SlackWSError:
				error := msg.Data.(*slack.SlackWSError)
				fmt.Printf("Error: %d - %s\n", error.Code, error.Msg)
			default:
				fmt.Printf("Unexpected: %v\n", msg.Data)
			}
		}
	}
}
示例#3
0
文件: main.go 项目: dpzaba/cbot
func startStream(c *slack.Slack, flows []string, responders []*MessageResponder) {
	chSender := make(chan slack.OutgoingMessage)
	chReceiver := make(chan slack.SlackEvent)

	wsAPI, err := c.StartRTM("", "https://slack.com/api")
	if err != nil {
		fmt.Errorf("%s\n", err)
	}
	go wsAPI.HandleIncomingEvents(chReceiver)
	go wsAPI.Keepalive(20 * time.Second)
	go func(wsAPI *slack.WS, chSender chan slack.OutgoingMessage) {
		for {
			select {
			case msg := <-chSender:
				wsAPI.SendMessage(&msg)
			}
		}
	}(wsAPI, chSender)
	for {
		select {
		case msg := <-chReceiver:
			fmt.Print("Event Received: ")
			switch msg.Data.(type) {
			case slack.HelloEvent:
				// Ignore hello
			case *slack.MessageEvent:
				a := msg.Data.(*slack.MessageEvent)
				handleMessage(c, a, responders)
				fmt.Printf("Message: %v\n", a)
			case *slack.PresenceChangeEvent:
				a := msg.Data.(*slack.PresenceChangeEvent)
				fmt.Printf("Presence Change: %v\n", a)
			case slack.LatencyReport:
				a := msg.Data.(slack.LatencyReport)
				fmt.Printf("Current latency: %v\n", a.Value)
			case *slack.WSError:
				error := msg.Data.(*slack.WSError)
				fmt.Printf("Error: %d - %s\n", error.Code, error.Msg)
			default:
				fmt.Printf("Unexpected: %v\n", msg.Data)
			}
		}
	}
}