コード例 #1
0
ファイル: main.go プロジェクト: bobbytables/slacker
func slackMethod() cli.Command {
	return cli.Command{
		Name:  "run",
		Usage: "[method]",
		Flags: []cli.Flag{
			cli.StringFlag{
				Name:   "token",
				Usage:  "Your Slack API token",
				EnvVar: "SLACK_TOKEN",
			},
		},
		Description: "Hits the SlackAPI using the format: https://slack.com/api/{method}",
		Action: func(ctx *cli.Context) {
			if len(ctx.Args()) == 0 {
				cli.ShowSubcommandHelp(ctx)
				return
			}

			method := ctx.Args()[0]
			token := ctx.String("token")

			client := slacker.NewAPIClient(token, "")
			b, err := client.RunMethod(method)
			if err != nil {
				fmt.Printf("Error running method: %s", err.Error())
				return
			}

			fmt.Println(string(b))
		},
	}
}
コード例 #2
0
ファイル: slack.go プロジェクト: botyard/botyard
func NewSlackGateway(cfg config.SlackGateway) *SlackGateway {
	client := slacker.NewAPIClient(cfg.Token, "")
	gw := &SlackGateway{
		id:     "slack",
		cfg:    cfg,
		client: client,
		logger: kitlog.NewContext(log.Logger).With("m", "Gateway-Slack"),
	}
	return gw
}
コード例 #3
0
ファイル: slackio.go プロジェクト: crewjam/slackcat
func newIface(url, token, channelName string) (*readerWriterCommon, error) {
	s := &readerWriterCommon{}
	s.client = slacker.NewAPIClient(token, url)

	// fetch the channel ID
	s.channelID = ""
	channels, err := s.client.ChannelsList()
	if err != nil {
		return nil, err
	}
	for _, channel := range channels {
		if channel.Name == channelName {
			s.channelID = channel.ID
			break
		}
	}
	if s.channelID == "" {
		return nil, fmt.Errorf("cannot find channel %s", channelName)
	}

	// figure out what user we are connected as so we can ignore
	// previous messages from that user.
	authBuf, err := s.client.RunMethod("auth.test")
	if err != nil {
		return nil, err
	}
	var auth struct {
		UserID string `json:"user_id"`
	}
	json.Unmarshal(authBuf, &auth)

	rtmStart, err := s.client.RTMStart()
	if err != nil {
		return nil, err
	}
	s.selfUserID = auth.UserID

	s.broker = slacker.NewRTMBroker(rtmStart)
	err = s.broker.Connect()
	if err != nil {
		return nil, err
	}

	return s, nil
}
コード例 #4
0
ファイル: bot.go プロジェクト: crewjam/standupbot
func main() {
	flag.Parse()

	c := slacker.NewAPIClient(*SlackToken, *SlackURL)
	rtmStart, err := c.RTMStart()
	if err != nil {
		panic(err)
	}

	sr := StandupRunner{}

	channels, err := c.ChannelsList()
	if err != nil {
		panic(err)
	}
	for _, channel := range channels {
		if channel.Name == *Channel {
			sr.Channel = channel
		}
	}
	log.Printf("channel is: %s (%s)", sr.Channel.Name, sr.Channel.ID)

	authBuf, err := c.RunMethod("auth.test")
	if err != nil {
		panic(err)
	}
	var auth struct {
		UserID string `json:"user_id"`
	}
	json.Unmarshal(authBuf, &auth)

	var onlyUsers []string
	if *Users != "" {
		onlyUsers = strings.Split(*Users, ",")
	}

	users, err := c.UsersList()
	if err != nil {
		panic(err)
	}

	if onlyUsers != nil {
		for _, user := range users {
			for _, userName := range onlyUsers {
				if userName == user.Name {
					sr.Users = append(sr.Users, user)
				}
			}
		}
	} else {
		for _, user := range users {
			for _, userID := range sr.Channel.Members {
				if userID == auth.UserID {
					continue
				}
				if userID == user.ID {
					sr.Users = append(sr.Users, user)
				}
			}
		}
	}

	broker := slacker.NewRTMBroker(rtmStart)
	broker.Connect()
	defer broker.Close()
	messages := make(chan *slacker.RTMMessage)

	go func() {
		for {
			event := <-broker.Events()
			if event.Type == "message" {
				msg, err := event.Message()
				if err != nil {
					panic(err)
				}

				if msg.Channel != sr.Channel.ID {
					continue
				}

				messages <- msg
			}
		}
	}()

	UserName := func(userID string) string {
		for _, user := range sr.Users {
			if user.ID == userID {
				return user.Name
			}
		}
		return userID
	}

	FriendlyName := func(user *slacker.User) string {
		if user.FirstName != "" {
			return user.FirstName
		}
		if user.LastName != "" {
			return user.LastName
		}
		return user.Name
	}

	Say := func(s string) {
		log.Printf("I said: %s", s)
		err := broker.Publish(slacker.RTMMessage{
			Type:    "message",
			Text:    s,
			Channel: sr.Channel.ID,
		})
		if err != nil {
			panic(err)
		}
	}

	Shuffle(sr.Users)

	log.Printf("ready for standup in #%s", sr.Channel.Name)
	log.Printf("Users will be:")
	for _, user := range sr.Users {
		log.Printf("%s (%s)", user.ID, user.Name)
	}
	typingTime := time.Second * 2

	Say("Hello @channel, it's *standup time*.")
	time.Sleep(typingTime)
	Say("I'll call on each of you one at a time. " +
		"When you are done with your update, send a message containing a " +
		"single period `.` and we'll move on to the next person. " +
		"If I call on someone who is not here, say `.` and I will move on.")

	countUsersResponding := 0
	for _, user := range sr.Users {
		time.Sleep(typingTime)
		Say(fmt.Sprintf("@%s, what have you got for us?", user.Name))

		responseCount := 0
		for {
			msg := <-messages
			log.Printf("%s said: %s", UserName(msg.User), msg.Text)

			if msg.Text == "." {
				if responseCount == 0 && msg.User != user.ID {
					Say(fmt.Sprintf("Got it. %s is not here. If that's wrong, you can chime in at the end.",
						FriendlyName(user)))
				} else {
					Say(fmt.Sprintf("Cool. Thanks, %s.", FriendlyName(user)))
				}
				break
			}

			if msg.User != user.ID {
				continue
			}

			// the user spoke. every time they speak they get another
			// four mins on the deadline
			responseCount++
			if msg.Text == "." {
				Say(fmt.Sprintf("Cool. Thanks, %s.", FriendlyName(user)))
				break
			}
		}
		if responseCount > 0 {
			countUsersResponding++
		}
	}

	time.Sleep(typingTime)
	Say("Thanks everybody! See you next time.")
	log.Printf("done with standup")
}