Skip to content

parthpower/adventures-with-bots

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Adventures with Bots!

This is a tale of adventures writing bots. There is lots of text ahead because it's a story/blog. Maybe not the best tutorial or example out there.

Do you have suggestions? Did I miss something? Do you have better ideas? open an issue!

Why? Why not!

I needed a way to boot up my home server remotely. Expose ssh on an always on raspberry pi? nah that sounds too simple. I also wanted some of my friends to start the server without sharing an ssh user or hacking up an lshell(1) like thing. We already have a discord and a slack channel so, How about a BOT!?

Plan

  • run a bot server on Raspberry Pi (probably in future, something even more tiny like an ESP8266)
  • The user sends a message with a secret token aka password.
  • The bot reads and checks the token.
  • if token matches, send WOL to the server

Discord bot bot-discord.py

Discord bot is extremely simple! Actually easier than a flask server! Well, maybe not.

Googled for "discord bot python" because python is fast to write and I don't care production/extensibility/robustness/speed etc. One can surely achieve those features with python, just that I didn't take those factors into consideration. Looked up here devdungeon (which is a bit outdated), and then more at realpython, then some docs and I wrote this ./bot-discord.py. Not production ready but it works.

Slack bot bot-slack.py

well, this one was a bit tricky. I found a way to send messages which I really didn't care about but reading a message from slack wasn't that simple. They needed me to sping up an HTTP POST handler! on the Internet! A webserver that accessible to the internet! Woah right there!

They deprecated their WebSocket based RTM API. They say they still support it with the "classic" type app but I couldn't get the RTM example connecting, it was just stuck and I was in a hurry! I mean, I just need this hack working! How hard can it be? The famous last words.

I went to try out classic Are there any free web services that allow me to tunnel my local server to the internet? and good old ngrok comes to mind. ngrok is no more an independent small project, probably bought by a company or something and now is a fancy looking thing, the good thing is, it's still simple and it just works. I didn't notice this earlier but official slack docs recommend ngrok to experiment with apps.

Side Note: In the meantime, I also tried to build an ssh tunnel on a GCP instance which should have just worked but then the monthly 2Gig ingress traffic limit on the free tier. Which should be sufficient but I really didn't want to dedicate my GCP instance for that! Since I already have a legit server at home, I was considering running (Cloudflare DDNS)[https://github.com/parthpower/CloudFlare-DDNS-Client], with cert-manager on my home k3s cluster. Which was too much for this project, maybe for another project?

Back to slack, Since slack has this /slash commands, I didn't bother creating a message event listener. Created a POST handler to check for slash command and secret which triggers WOL to my server. Thanks to this doc it was pretty straight forward. Also, DigitalOcean had this example

It took ngrok + flask for the slack slash command handler. Except for the complexity of exposing a local webserver, the slack bot isn't any harder than the discord bot. Actually, easier since it doesn't require a slack specific library, and is literally an HTTP POST handler that responds with some JSON.

Exposing a webserver to public internet safely isn't free or simple or easy. I am not a professional on edge deployment or whatever it is called, let me know if I'm wrong! issues or @parthpower

Free options are,

  1. Open port 80, 443 on your router, add some DDNS, get certs from LetsEncrypt, try to add some security, and hope nobody attacks.
  2. Run-on free tier instances. e.g. GCP/OpenShift/Heroku etc
  3. Trust ngrok with your "secret" slack data.

Having said that, since the discord bot didn't require any open ports, it feels a lot easier than slack bots! I mean, I can even run that form my non-rooted phone!

How to run?

Get Tokens

Get the bot token from Discord and/or verification token from the slack bot.

For discord, check out realpython for step by step guide.

For slack, check out digitalocean for step by step guide

Create .env file

DISCORD_TOKEN="<discord bot token>"
SLACK_VERIFICATION_TOKEN="<slack app verfication token>"

BOT_SECRET="<secret message to trigger WOL>"
SERVER_MAC="<mac address to send WOL magic to>"

Install requirements, hopefully in a virtualenv

virtualenv venv
pip install requirements.txt

Discord Bot

./bot-discord.py

Slack Bot

Install/Setup ngrok https://ngrok.com/download

ngrok http 8080
./bot-slack.py

LICENSE

Copyright © 2020 Parth Parikh <parthpower@gmail.com>
This work is free. You can redistribute it and/or modify it under the
terms of the Do What The Fuck You Want To Public License, Version 2,
as published by Sam Hocevar. See the COPYING.WTFPL file for more details.

About

A tale of adventures making bots

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages