예제 #1
0
파일: slack.go 프로젝트: pusparajm/linda
// Listen to incoming events
func (adapter *Slack) Listen(events chan *commons.Event) {
	for {
		select {
		case msg := <-adapter.chReceiver:
			switch msg.Data.(type) {
			case wrapper.HelloEvent:
				event := msg.Data.(wrapper.HelloEvent)
				log.WithField("adapter", adapter.cfg.Type).Debugf("Hello: %v", event)

			case *wrapper.MessageEvent:
				event := msg.Data.(*wrapper.MessageEvent)
				channelName := adapter.getChannelName(event.ChannelId)

				// Operate only on selected channel
				if channelName == adapter.cfg.Channel {
					log.WithField("adapter", adapter.cfg.Type).Debugf("Message: %v", event)
					e := commons.NewEvent().FromSlackMessage(event)
					//adapter.syncUsers()
					e.Username = adapter.getUsername(event.UserId)
					events <- e
				}

			case *wrapper.PresenceChangeEvent:
				event := msg.Data.(*wrapper.PresenceChangeEvent)
				log.WithField("adapter", adapter.cfg.Type).Debugf("Presence Change: %v", event)

				// Get username for presence change event
				if event.UserId != adapter.userId {
					isDuplicate := adapter.isDuplicateEvent(event)
					if !isDuplicate {
						e := commons.NewEvent().FromSlackStatus(event)
						//adapter.syncUsers()
						e.Username = adapter.getUsername(event.UserId)
						events <- e
					}

					adapter.lastPcEvent = event
				}

			case wrapper.LatencyReport:
				// Skip latency report

			case *wrapper.SlackWSError:
				err := msg.Data.(*wrapper.SlackWSError)
				log.WithField("adapter", adapter.cfg.Type).Debugf("Error: %d - %s", err.Code, err.Msg)

			default:
				log.WithField("adapter", adapter.cfg.Type).Debugf("Unexpected: %v", msg.Data)
			}
		}
	}
}
예제 #2
0
파일: telegram.go 프로젝트: pusparajm/linda
// Listen to incoming events
func (adapter *Telegram) Listen(events chan *commons.Event) {
	messages := make(chan telebot.Message)

	for {
		adapter.bot.Listen(messages, time.Second)

		for message := range messages {
			log.WithField("adapter", adapter.cfg.Type).Debugf("Message: %v", message)
			events <- commons.NewEvent().FromTelegramMessage(message)
		}

		time.Sleep(time.Second)
	}
}