神刀安全网

Building Rock Paper Scissors with Botkit

SETUP

Disclaimer, I’m using node 6 to take advantage of ES6!

Before we get into the code, go make a Slack bot and grab its API token .

Got it? Now make a new folder, npm init and:

npm i -S botkit

Copy and paste this Botkit starter into a new file called bot.js and run:

token=YOUR_SLACK_API_TOKEN node bot.js

The script will fire up a server and put your bot online. Now, invite your Slack bot into your channel with “/invite @YOUR_BOT_NAME”. Try saying “@YOUR_BOT_NAME hello”, you’ll should get a “YO!”. You’re bot is happy to hear from you.

1. DUEL DECLARED

I want to challenge someone by saying “ @gamebot play @michell ”, so instead of listening for “hello” let’s keep our bot tuned to “play”.

Botkit will hand the phrase back as “ play <@ USER_ID > ”. Using a little regex we can parse out the ID of player two. If no user ID is found, don’t start the game.

Let’s structure info about the new game. We’ll need the channel ID, where the game started, and data on both players. Player data can be accessed in the “players” object using their respective IDs as the key.

Botkit has built in storage handy for small apps like this. Back up top where we destructure our controller, grab the channels storage.

const { hears, storage: { channels } } = controller;

Botkit’s storage system provides a “save” method where we’ll pass the game data and a callback. In the callback, let player two know they’ve been challenged and how they can join.

2. CHALLENGE ACCEPTED

Our bot will need to listen to any mention of “accept”. Once our bot detects the word, we’ll query our storage to see if we already have game data.

Confirm that it is player two responding…

3. CONFIDE IN PRIVATE

In the real world Rock Paper Scissors players have to “shoot” at the same time. That won’t work on Slack, so instead use “startPrivateConversation” to get each player’s response in a private channel. Once we have both we’ll reveal the results.

bot.startPrivateConversation(message, callback);

A conversation can only be in response to a message initiated by the user . So each player must engage the bot for us to start a private conversation. In our case, player one engages when they say “play” and player two when they say “accept”. Those two moments allow us to pass the message object to start the private conversation.

player one:

player two:

Inside the callback function we need to do a few things. First, capture each player’s response (rock/paper/scissors), then add the response to the game data.

To accomplish this, the callback will need access to the message data and bot, but the callback’s only parameters are “error” and “conversation”. We can remedy this by making a higher order function that returns our callback with context.

A conversation starts with a bot asking a question and listening for a response. As arguments, the ask method takes a question, an array of callbacks, and capture options. Each item in the array of callbacks has a regex pattern to check the user’s response against. If there’s a match, the bot calls the pattern’s callback. In our case, we’re listening for “rock”, “paper”, or “scissors”.

If the bot can’t find a match, the last callback repeats the question.

The third parameter lets you to capture the user’s response. By passing an object with “key” set to a string of our choice, we’ll be able to retrieve the user’s response.

convo.ask('...', [...], {key: 'rockPaperScissors'});

After the bot captures the response, there are no other messages queued so the conversation status is set to “completed”.

4. DECLARE THE RESULTS

Botkit’s conversation has an “on” method we’ll use to detect when it ends. Upon completion, update the game data and broadcast the results.

Check to make sure things ended correctly. If so, extract the rockPaperScissors data.

Grab the channel data from our store and check if there are any players who haven’t gone yet.

If only one person has responded, save the info. In the original channel let the other player know their opponent has played.

Otherwise, if both players have played broadcast the results! Then clear them so they can play again. (“how about best out of three??”)

(Bonus points, you can take this tutorial one step further and actually tell the players who won 😉

Here’s the final code , give it a whirl.

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Building Rock Paper Scissors with Botkit

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址