logo

drewdevault.com

[mirror] blog and personal website of Drew DeVault git clone https://hacktivis.me/git/mirror/drewdevault.com.git

How-to-write-an-IRC-bot.md (4644B)


  1. ---
  2. date: 2018-03-10
  3. layout: post
  4. title: How to write an IRC bot
  5. tags: [irc, slack]
  6. ---
  7. My disdain for Slack and many other Silicon Valley chat clients is [well
  8. known](/2015/11/01/Please-stop-using-slack.html), as is my undying love for IRC.
  9. With Slack making the news lately after their recent decision to disable the IRC
  10. and XMPP gateways in a classic [Embrace Extend
  11. Extinguish](https://en.wikipedia.org/wiki/Embrace%2C_extend%2C_and_extinguish)
  12. move, they've been on my mind and I feel like writing about them more. Let's
  13. compare writing a bot for Slack with writing an IRC bot.
  14. First of all, let's summarize the process for making a Slack bot. Full details
  15. are available in [their documentation](https://api.slack.com/slack-apps). The
  16. basic steps are:
  17. 1. Create a Slack account and "workspace" to host the bot (you may have already
  18. done this step). On the free plan you can have up to 10 "integrations" (aka
  19. bots). This includes all of the plug-n-play bots Slack can set up for you, so
  20. make sure you factor that into your count. Otherwise you'll be heading to the
  21. pricing page and making a case to whoever runs your budget.
  22. 2. Create a "Slack app" through their web portal. The app will be tied to the
  23. company you work with now, and if you get fired you will lose the app. Make
  24. sure you make a separate organization if this is a concern!
  25. 3. The recommended approach from here is to set up subscriptions to the "Event
  26. API", which involves standing up a web server (with working SSL) on a
  27. consistent IP address (and don't forget to open up the firewall) to receive
  28. incoming notifications from Slack. You'll need to handle a proprietary
  29. challenge to verify your messages via some HTTP requests coming from Slack
  30. which gives you info to put into HTTP headers of your outgoing requests. The
  31. Slack docs refer to this completion of this process as "triumphant success".
  32. 4. Receive some JSON in a proprietary format via your HTTP server and use some
  33. more proprietary HTTP APIs to respond to it.
  34. Alternatively, instead of steps 3 and 4 you can use the "Real Time Messaging"
  35. API, which is a websocket-based protocol that starts with an HTTP request to
  36. Slack's authentication endpoint, then a follow-up HTTP request to open the
  37. WebSocket connection. Then you set up events in a similar fashion. Refer to the
  38. complicated table in the documentation breaking down which events work through
  39. which API.
  40. Alright, so that's the Slack way. How does the IRC way compare? IRC is an open
  41. standard, so to learn about it I can just read RFC 1459, which on my system is
  42. conveniently waiting to be read at `/usr/share/doc/rfc/txt/rfc1459.txt`. This
  43. means I can just read it locally, offline, in the text editor of my choice,
  44. rather than on some annoying website that calls authentication a "triumphant
  45. success" and complains about JavaScript being disabled.
  46. **Note**: This blog post pre-dates the commercial take-over of and subsequent
  47. obsolescence of Freenode. The network this connects to, and channel it mentions,
  48. no longer exist. Running these commands will not work, though the principles
  49. remain correct.
  50. You don't have to read it right now, though. I can give you a summary here, like
  51. I gave for Slack. Let's start by not writing a bot at all - let's just manually
  52. throw some bits in the general direction of Freenode. Install netcat and run
  53. `nc irc.freenode.net 6667`, then type this into your terminal:
  54. ```
  55. NICK joebloe
  56. USER joebloe 0.0.0.0 joe :Joe Bloe
  57. ```
  58. Hey, presto, you're connected to IRC! Type this in to join a channel:
  59. ```
  60. JOIN #cmpwn
  61. ```
  62. Then type this to say hello:
  63. ```
  64. PRIVMSG #cmpwn :Hi SirCmpwn, I'm here from your blog!
  65. ```
  66. IRC is one of the simplest protocols out there, and it's dead easy to write a
  67. bot for it. If your programming language can open a TCP socket (it can), then
  68. you can use it to write an IRC bot in 2 minutes, flat. That's not even to
  69. mention that there are IRC client libraries available for every programming
  70. language on every platform ever - I even [wrote one
  71. myself!](https://github.com/SirCmpwn/ChatSharp) In fact, that guy is probably
  72. the fifth or sixth IRC library I've written. They're so easy to write that I've
  73. lost count.
  74. Slack is a walled garden. Their proprietary API is defined by them and only
  75. implemented by them. They can and will shut off parts you depend on (like the
  76. IRC+XMPP gateways that were just shut down). IRC is over 20 years old and
  77. software written for it then still works now. It's implemented by hundreds of
  78. clients, servers, and bots. Your CI supports it and GitHub can send commit
  79. notifications to it. It's ubiquitous and free. Use it!