slack-tictactoe-bot

🤖 A bot to play tic-tac-toe game on Slack is developed.

Stars
6

Slack-tictactoe-bot Awesome GitHub followers

This is a capstone project in the Ruby course at Microverse - @microverseinc . Creating a bot for any platform for any purpose is the purpose of this capstone project.

Built With

  • Ruby
  • Slack API
  • ngrok
  • Sinatra
  • RSpec

Description

The task is to create a bot to play a tic-tac-toe game on Slack.

This project is mainly about

  • Object-Oriented Programming
  • The DRY rule

Live demo

  • Join workspace to test demo.
  • Send a private message to tictactoe near the bottom of the left sidebar.
  • Follow the directive given by tictactoe.

Setup

  • Clone this repository by running git clone https://github.com/ozovalihasan/slack-tictactoe-bot.git.

  • Run cd slack-tictactoe-bot.

  • Install ngrok by running snap install ngrok (I used Snap store. If you don't use Snap store, check the alternatives).

  • Install ruby.

  • Install Bundler by running gem install bundler.

  • Run bundle install.

  • Create a workspace to test this app.

  • Create a Slack App.

  • Open OAuth & Permissions located under the Features title at the left side of page. Go to Scopes, add chat:write, im:write and im:history for Bot Token Scopes.

  • Go to the top of the page. Install your app to your workspace by using Install App to Workspace button. If you change any setting, reinstall it.

  • Go to the top of the page and copy Bot User OAuth Access Token.

  • Open the terminal, and run export SLACK_BOT_TOKEN=xoxb-000000000000-xxxxxxxxxxxxxxxxxxxxxxxx. 'xoxb-000000000000-xxxxxxxxxxxxxxxxxxxxxxxx' part must be changed with token you copied.

  • Open the page of app settings and click Basic Information on the left side of the page. Under the App Credentials, find Verification Token and copy it.

  • Open the terminal, and run export SLACK_VERIFICATION_TOKEN=xxxxxxxxxxxxxxxxxxx. 'xxxxxxxxxxxxxxxxxxx' part must be changed with the token you copied.

Reminder: If you want to share your project, don't add this information to your project because Slack may disable your app if you share this information with the public.

  • Make enable Bot user to be always online. To do it, click App Home under the Features title. Check Always Show My Bot as Online option.

Steps until here are shown in the video.


  • Don't forget to reinstall your app.

  • Run Sinatra by using rackup from the terminal. This terminal shouldn't be closed when the app is used.

  • Open another terminal and run ngrok http -host-header=rewrite 9292. 9292 is default one, so if it is not working correctly, check terminal run rackup. It is seen like Listening on tcp://localhost:9292. Again this terminal must be open when the app is used.

  • When ngrok runs, there should be a link similar to https://1a2b3c4d5e.ngrok.io. Copy this link and click 'Your apps' located at the right top of api.slack.com. Click your app. Click Event Subscriptions on the left side of the page. Paste copied link into Request URL and add /slack/events at the end of the link. It should be seen like https://1a2b3c4d5e.ngrok.io/slack/events. Open Subscribe to bot events part and add message.im. Check Enable Events. Lastly, click Save Changes at the left bottom part of the page.

  • Click Interactivity & Shortcuts on the left side of the page. Check the button of Interactivity. Paste the same link into the box of Request URL. And add /slack/attachments at the end of the link. It should be seen like https://1a2b3c4d5e.ngrok.io/slack/attachments. Lastly, click Save Changes at the left bottom part of the page.

  • If ngrok is restarted, the copied link will be changed automatically. So, you need to follow the last two steps if it is restarted.

  • ngrok is accepting limited requests for a limited time. So, if there will be a lot of interaction between user and bot, the responding time of the bot may increase or it may not respond to the user's interaction. If this happened, ngrok can be restarted, or you may stop using the app for a while.

  • Go to your Workspace. Refresh the page and send a direct message to the bot from Slack. It is located under the Apps title.


Steps until here are shown in the video.


How to Play the Game

  • Send any direct message to the bot.
  • Follow the directions given by the bot.

If you want, check the demo of the app/bot.

Rules of the Game

  • The user is represented 'X' and the bot is represented 'O' as symbol.
  • To win the game, three identical symbols must be next to each other vertically, horizontally, or diagonally. If there is a winner, the winner will be announced as "WINNER".
  • If no one has won by the end of ninth turn, the game will be ended as a "DRAW".

For more information check Wikipedia.

Testing the code

  • All methods are tested.

To test the project.

  • Clone this repository by running git clone https://github.com/ozovalihasan/slack-tictactoe-bot.git.

  • Run cd slack-tictactoe-bot.

  • Install Bundler by running gem install bundler.

  • Run bundle install.

  • Run rspec.

Deploy to Heroku

  • Sign in Heroku.
  • Create an app by running heroku create name-of-app in the terminal. You may use any name for name of your Heroku app. You need to change 'name-of-app' with name you chose in the next steps.
  • Run sudo snap install --classic heroku. If you get any error check.
  • Run heroku login.
  • If you are using master branch,run git push heroku master. If you are using another branch, run git push heroku +HEAD:master.

To configure your Heroku app

  • Run heroku config:set SLACK_BOT_TOKEN=xoxb-000000000000-xxxxxxxxxxxxxxxxxxxxxxxx in terminal. Change bot token with one used before.
  • Run heroku config:set SLACK_VERIFICATION_TOKEN=xxxxxxxxxxxxxxxxxxx. Change verification token with one used before.
  • Run heroku apps:info.
  • Copy 'Web URL'. It is similar to https://name-of-app.herokuapp.com/.
  • Go to 'https://api.slack.com/apps' and open the app created for this project.
  • Go to 'Event Subscriptions' (same as given instruction before) in Slack. Paste https://name-of-app.herokuapp.com/slack/events into the box of 'New Request URL' under 'Enable Events' tab. Push 'Save Changes' button on the right bottom of the screen.
  • Go to 'Interactivity & Shortcuts' (same as given instruction before). Paste https://name-of-app.herokuapp.com/slack/attachments into box of 'Request URL' under 'Interactivity' tab.

To play with your app

  • Go to your workspace that you added app. Send a private message to your app. Play game and share others.

Questions and answers

  • The app/bot is not updated. What do I need to do?

Check Gemfile. If versions of used gems are not updated, update them.

  • The app/bot doesn't respond. What do I need to do?

Check SLACK_VERIFICATION_TOKEN, SLACK_BOT_TOKEN. Check the link provided by ngrok and check the link used for 'Request URL's. Check the terminal run rackup. If it shows any error, solve it.

  • When bundle install run, ERROR: Error installing websocket-driver: is raising. What do I need to do?

Try to run apt-get install ruby-dev.

  • I did everything correctly, but it is not working. What do I need to do?

Run gem list. If you have any gem whose version is greater than one given in Gemfile.lock file, delete newer version of this gem.

Authors

👤 Hasan Özovalı

Contributing 🤝

Contributions, issues, and feature requests are welcome!

Feel free to check the issues page.

Acknowledgements

Show your support

Give a ⭐️ if you like this project!

📝 License

This project is MIT licensed.

Badges
Extracted from project README
Awesome
Related Projects