Ejemplo n.º 1
0
func lastMessageTimestamp(api *slack.Slack, channel slack.Channel) (int64, error) {
	var latest string

	for {
		historyParams := slack.HistoryParameters{Count: 5}
		if latest != "" {
			historyParams.Latest = latest
		}

		history, err := api.GetChannelHistory(channel.Id, historyParams)

		if err != nil {
			return -1, err
		}

		if len(history.Messages) == 0 {
			return -1, nil
		}

		for _, msg := range history.Messages {
			latest = msg.Msg.Timestamp

			if msg.SubType != "channel_join" && msg.SubType != "channel_leave" {
				msgStamp := strings.Split(msg.Msg.Timestamp, ".")
				if timestamp, err := strconv.ParseInt(msgStamp[0], 10, 32); err == nil {
					return timestamp, nil
				}
			}
		}
	}
}
Ejemplo n.º 2
0
func (ms *Messages) GetAllWithHistory(channelID string, timestamp string) string {
	params := slackApi.HistoryParameters{
		Count:     30,
		Inclusive: true,
	}
	if timestamp != "" {
		params.Latest = timestamp
	}

	h, err := API.GetChannelHistory(channelID, params)
	if err != nil {
		errorLn(err.Error())
		return ""
	}

	var tmpMs []Message
	for i := len(h.Messages) - 1; i > 0; i-- {
		msg := Message{}
		msg.transformFromBackend(&h.Messages[i].Msg)
		msg.Channel = channelID
		tmpMs = append(tmpMs, msg)
	}
	ms.list = append(tmpMs, ms.list...)
	ms.Len = len(ms.list)

	return ms.GetAll(channelID)
}
Ejemplo n.º 3
0
func (s *Slack) handleIM(action string, params map[string]string) (interface{}, error) {
	var v interface{}
	var err error

	switch action {
	case "close":
		noop, closed, err := s.s.CloseIMChannel(params["channel"])
		if err != nil {
			return nil, err
		}

		v = map[string]bool{
			"no_op":          noop,
			"already_closed": closed,
		}
	case "history":
		historyParms := slack.HistoryParameters{}
		historyParms.Latest = getStringParam(params, "latest", slack.DEFAULT_HISTORY_LATEST)

		historyParms.Oldest = getStringParam(params, "oldest", slack.DEFAULT_HISTORY_OLDEST)

		historyParms.Count = getIntParam(params, "count", slack.DEFAULT_HISTORY_COUNT)

		v, err = s.s.GetIMHistory(params["channel"], historyParms)

	case "list":
		ims, err := s.s.GetIMChannels()
		if err != nil {
			return nil, err
		}

		v = map[string]interface{}{
			"ims": ims,
		}
	case "mark":
		err = s.s.MarkIMChannel(params["channel"], params["ts"])
	case "open":
		noop, opened, ch, err := s.s.OpenIMChannel(params["user"])
		if err != nil {
			return nil, err
		}

		v = map[string]interface{}{
			"no_op":        noop,
			"already_open": opened,
			"channel": map[string]string{
				"id": ch,
			},
		}

	default:
		return nil, fmt.Errorf("invalid im action %s", action)
	}

	return v, err
}
Ejemplo n.º 4
0
func (s *Slack) handleGroups(action string, params map[string]string) (interface{}, error) {
	var v interface{}
	var err error

	switch action {
	case "archive":
		err = s.s.ArchiveGroup(params["channel"])
	case "close":
		noop, closed, err := s.s.CloseGroup(params["channel"])
		if err != nil {
			return nil, err
		}
		v = map[string]bool{
			"no_op":          noop,
			"already_closed": closed,
		}
	case "create":
		group, err := s.s.CreateGroup(params["name"])
		if err != nil {
			return nil, err
		}
		v = map[string]interface{}{
			"group": group,
		}
	case "createchild":
		group, err := s.s.CreateChildGroup(params["channel"])
		if err != nil {
			return nil, err
		}
		v = map[string]interface{}{
			"group": group,
		}
	case "history":
		historyParam := slack.HistoryParameters{}
		historyParam.Latest = getStringParam(params, "latest", slack.DEFAULT_HISTORY_LATEST)
		historyParam.Oldest = getStringParam(params, "latest", slack.DEFAULT_HISTORY_OLDEST)
		historyParam.Count = getIntParam(params, "count", slack.DEFAULT_HISTORY_COUNT)
		v, err = s.s.GetGroupHistory(params["channel"], historyParam)
	case "invite":
		group, in, err := s.s.InviteUserToGroup(params["channel"], params["user"])
		if err != nil {
			return nil, err
		}
		v = map[string]interface{}{
			"already_in_group": in,
			"group":            group,
		}
	case "kick":
		err = s.s.KickUserFromGroup(params["channel"], params["user"])
	case "leave":
		err = s.s.LeaveGroup(params["channel"])
	case "list":
		exclude := getIntParam(params, "exclude_archived", 0)
		groups, err := s.s.GetGroups(exclude == 1)
		if err != nil {
			return nil, err
		}
		v = map[string]interface{}{
			"groups": groups,
		}
	case "open":
		noop, opened, err := s.s.OpenGroup(params["channel"])
		if err != nil {
			return nil, err
		}
		v = map[string]bool{
			"no_op":        noop,
			"already_open": opened,
		}
	case "mark":
		err = s.s.SetGroupReadMark(params["channel"], params["ts"])
	case "rename":
		group, err := s.s.RenameGroup(params["channel"], params["name"])
		if err != nil {
			return nil, err
		}
		v = map[string]interface{}{
			"group": group,
		}
	case "setpurpose":
		purpose, err := s.s.SetGroupPurpose(params["channel"], params["purpose"])
		if err != nil {
			return nil, err
		}
		v = map[string]interface{}{
			"purpose": purpose,
		}
	case "settopic":
		topic, err := s.s.SetGroupTopic(params["channel"], params["topic"])
		if err != nil {
			return nil, err
		}
		v = map[string]interface{}{
			"topic": topic,
		}
	case "unarchive":
		err = s.s.UnarchiveGroup(params["channel"])
	default:
		return nil, fmt.Errorf("invalid groups action %s", action)
	}

	return v, err
}
Ejemplo n.º 5
0
func (s *Slack) handleChannels(action string, params map[string]string) (interface{}, error) {
	var v interface{}
	var err error

	switch action {
	case "archive":
		err = s.s.ArchiveChannel(params["channel"])
	case "create":
		ch, err := s.s.CreateChannel(params["name"])
		if err != nil {
			return nil, err
		}
		v = map[string]interface{}{
			"channel": ch,
		}
	case "history":
		historyParam := slack.HistoryParameters{}
		historyParam.Latest = getStringParam(params, "latest", slack.DEFAULT_HISTORY_LATEST)
		historyParam.Oldest = getStringParam(params, "latest", slack.DEFAULT_HISTORY_OLDEST)
		historyParam.Count = getIntParam(params, "count", slack.DEFAULT_HISTORY_COUNT)
		v, err = s.s.GetChannelHistory(params["channel"], historyParam)
	case "info":
		ch, err := s.s.GetChannelInfo(params["channel"])
		if err != nil {
			return nil, err
		}
		v = map[string]interface{}{
			"channel": ch,
		}
	case "invite":
		ch, err := s.s.InviteUserToChannel(params["channel"], params["user"])
		if err != nil {
			return nil, err
		}
		v = map[string]interface{}{
			"channel": ch,
		}
	case "join":
		ch, err := s.s.JoinChannel(params["name"])
		if err != nil {
			return nil, err
		}
		v = map[string]interface{}{
			"channel": ch,
		}
	case "kick":
		err = s.s.KickUserFromChannel(params["channel"], params["user"])
	case "leave":
		_, err = s.s.LeaveChannel(params["channel"])
	case "list":
		exclude := getIntParam(params, "exclude_archived", 0)
		chs, err := s.s.GetChannels(exclude == 1)
		if err != nil {
			return nil, err
		}
		v = map[string]interface{}{
			"channels": chs,
		}
	case "mark":
		err = s.s.SetChannelReadMark(params["channel"], params["ts"])
	case "rename":
		ch, err := s.s.RenameChannel(params["channel"], params["name"])
		if err != nil {
			return nil, err
		}
		v = map[string]interface{}{
			"channel": ch,
		}
	case "setpurpose":
		purpose, err := s.s.SetChannelPurpose(params["channel"], params["purpose"])
		if err != nil {
			return nil, err
		}
		v = map[string]interface{}{
			"purpose": purpose,
		}
	case "settopic":
		topic, err := s.s.SetChannelTopic(params["channel"], params["topic"])
		if err != nil {
			return nil, err
		}
		v = map[string]interface{}{
			"topic": topic,
		}
	case "unarchive":
		err = s.s.UnarchiveChannel(params["channel"])
	default:
		return nil, fmt.Errorf("invalid files action %s", action)
	}

	return v, err
}