Example #1
0
File: conn.go Project: Zerak/micro
func (s *slackConn) Recv(event *input.Event) error {
	if event == nil {
		return errors.New("event cannot be nil")
	}

	for {
		select {
		case <-s.exit:
			return errors.New("connection closed")
		case e := <-s.rtm.IncomingEvents:
			switch ev := e.Data.(type) {
			case *slack.MessageEvent:
				// only accept type message
				if ev.Type != "message" {
					continue
				}

				// only accept DMs or messages to me
				switch {
				case strings.HasPrefix(ev.Channel, "D"):
				case strings.HasPrefix(ev.Text, s.auth.User):
				case strings.HasPrefix(ev.Text, fmt.Sprintf("<@%s>", s.auth.UserID)):
				default:
					continue
				}

				// Strip username from text
				switch {
				case strings.HasPrefix(ev.Text, s.auth.User):
					args := strings.Split(ev.Text, " ")[1:]
					ev.Text = strings.Join(args, " ")
					event.To = s.auth.User
				case strings.HasPrefix(ev.Text, fmt.Sprintf("<@%s>", s.auth.UserID)):
					args := strings.Split(ev.Text, " ")[1:]
					ev.Text = strings.Join(args, " ")
					event.To = s.auth.UserID
				}

				if event.Meta == nil {
					event.Meta = make(map[string]interface{})
				}

				// fill in the blanks
				event.From = ev.Channel + ":" + ev.User
				event.Type = input.TextEvent
				event.Data = []byte(ev.Text)
				event.Meta["reply"] = ev
				return nil
			case *slack.InvalidAuthEvent:
				return errors.New("invalid credentials")
			}
		}
	}
}
Example #2
0
File: conn.go Project: Zerak/micro
func (c *hipchatConn) Recv(event *input.Event) error {
	if event == nil {
		return errors.New("event cannot be nil")
	}

	me := c.getName(c.client.Id).MentionName

	for {
		select {
		case <-c.exit:
			return errors.New("connection closed")
		case msg := <-c.recv:
			args := strings.Split(msg.Body, " ")
			switch msg.Type {
			// join the room
			case "join_groupchat":
				c.client.Join(msg.From, strings.Title(me))
				continue
			// this is a Room
			case "groupchat":
				// no args
				if len(args) < 2 {
					continue
				}

				// get first arg and check if its us
				name := strings.ToLower(args[0])
				args = args[1:]

				// is it for us?
				switch {
				case strings.HasPrefix(name, "@"+me), strings.HasPrefix(name, me):
				default:
					continue
				}

				// parse from
				parts := strings.Split(msg.From, "/")
				// from channel
				from := parts[0]

				c.Lock()
				names := c.names
				c.Unlock()

				// from user
				var user string
				if len(parts) > 1 {
					for _, u := range names {
						if u.Name == parts[len(parts)-1] {
							user = "******" + u.MentionName
						}
					}
				}

				event.From = from + ":" + user
				event.Data = []byte(strings.Join(args, " "))
				event.To = c.client.Id
			// this is a DM
			case "chat":
				// no args
				if len(args) < 1 {
					continue
				}

				// get first arg and check if its us
				name := strings.ToLower(args[0])

				// parse from
				parts := strings.Split(msg.From, "|")
				// from channel
				from := strings.Split(parts[0], "/")[0]

				// is it for us?
				switch {
				case strings.HasPrefix(name, "@"+me), strings.HasPrefix(name, me):
					args = args[1:]
				}

				event.From = from
				event.To = c.client.Id
				event.Data = []byte(strings.Join(args, " "))
			}

			if event.Meta == nil {
				event.Meta = make(map[string]interface{})
			}

			event.Type = input.TextEvent
			event.Meta["reply"] = msg
			return nil
		}
	}
}