Esempio n. 1
0
func (ctx *botContext) responseEvent(ev *slack.MessageEvent) plugins.BotEvent {
	botUser := ctx.RTM.GetInfo().User

	var chName string
	cInfo, err := ctx.RTM.Client.GetChannelInfo(ev.Channel)
	if err == nil {
		chName = cInfo.Name
	}

	return plugins.NewBotEvent(ctx,
		botUser.ID, botUser.Name,
		ev.User, ev.Username, ev.Text, ev.Channel, chName,
	)
}
Esempio n. 2
0
func (ctx *botContext) webSocketRTM(connectedFunc func(event plugins.BotEvent)) {
	if ctx.RTM != nil {
		ctx.RTM.Disconnect()
	}
	ctx.RTM = ctx.Client.NewRTM()

	go ctx.RTM.ManageConnection()

	go func() {
		for {
			select {
			case msg := <-ctx.RTM.IncomingEvents:
				fmt.Print("Event Received: ")
				switch ev := msg.Data.(type) {
				case *slack.ConnectedEvent:
					botUser := ctx.RTM.GetInfo().User
					for _, c := range ev.Info.Channels {
						connectedFunc(plugins.NewBotEvent(ctx,
							botUser.ID, botUser.Name,
							ev.Info.User.ID, ev.Info.User.Name, "connected",
							c.ID, c.Name,
						))
					}
				case *slack.MessageEvent:
					ctx.Plugins.ExecPlugins(ctx.responseEvent(ev))
				case *slack.PresenceChangeEvent:
					fmt.Printf("Presence Change: %v\n", ev)
				case slack.LatencyReport:
					fmt.Printf("Current latency: %v\n", ev.Value)
				case *slack.RTMError:
					fmt.Printf("Error: %d - %s\n", ev.Code, ev.Msg)
				default:
					fmt.Printf("Unexpected: %+v\n", ev)
				}
			}
		}
	}()
}