A Chatbot for OhDear!
Tags: chatbot, php, laravel, botman
Published: 2018-11-18 11:09:50

---
title: A Chatbot for OhDear!
published: true
tags: chatbot, php, laravel, botman
canonical_url: https://davidllop.com/chatbot-for-ohdear
---

One day I tried to show a post of mine to a coworker and then I realised my website was down. I had nothing to warn me if that happens at that time and it was kinda embarrassing. Solving it was as easy as login to DigitalOcean and restart the droplet, but it looked very unprofessional.

> For how long was my site down? How many people were unable to read my awesome posts?

That was when I decided that I needed some kind of automatic monitor for my website up time.

I remembered [Freek](https://twitter.com/freekmurze) tweeting about [OhDear!](https://ohdear.app), a new monitoring tool made by him and [Mattias](https://twitter.com/mattiasgeniar), and I decided to take a look.

The documentation looked awesome so I joined as a free 10 days trial to check if it was useful for me. It turned out to be the perfect solution I needed. Taking a look at the docs and learning how to use it (pretty easy indeed) I saw the API section. It ended up being a well structured and simple API. I've worked building API's and consuming them for almost 3 years in the past and I wanted to work on something new. Then this project came to my mind.

> A chatbot to interact with this API. To check your website status and receive messages if something is wrong.

I always create my bots to interact just with Telegram by now, I know how it works and it's easy to test the result. This project could work with Facebook Messenger in a future, I'm not discarding that feature 🤔

Once I've installed a fresh copy of [BotMan](https://botman.io) and prepare the app I realised I'd never tested code that interacts with an API that was not mine. I learned TDD and Unit Testing after that API developer period. BotMan offers a great [Testing Solution](https://botman.io/2.0/testing) to work with. In fact, it's easier to develop a chatbot with tests since you can check the result instantly.

Then I remembered a few lessons in [Adam Wathan](https://twitter.com/adamwathan)'s course [Test Driven Laravel](https://course.testdrivenlaravel.com) regarding this matter. You create a class that will return the same payload as the API you want to integrate. Usually your `Fake` class will extend the current working class to override those methods that perform the actual API call. And that's what [I've done for this test suite](https://github.com/Lloople/bot-mr-dear/tree/master/tests/Fakes). This way I was able to develop the entire project without querying OhDear's app.

OhDear offers a [PHP Open Source package](https://github.com/ohdearapp/ohdear-php-sdk) to interact with their API. I needed to extend a few classes to adapt them into my needs but the rest was pretty easy.

They also have a package to interact with [OhDear WebHooks](https://github.com/ohdearapp/laravel-ohdear-webhooks), but this time I was unable to use this package. It's built to work with a secret key saved in a configuration file. My app, otherwise, it's made to interact with multiple users at a time, and each users has a different secret key. I'm currently storing that information encrypted into the database. When a webhook request is received it already contains the username. For example `https://deployedbot.com/freekmurze`. Then I check the payload with the user's secret key in order to proceed or return an authorization error.

I built this project using the [Single Action Controllers](https://laravel.com/docs/5.7/controllers#single-action-controllers) approach and I'm pretty happy with the result, It's something I'll consider in future projects for sure.

Most of the times there are just one controller per namespace in this project. But the benefit is also visible in the routes file, since it looks neat and you can use your IDE to click on the controllers name and go directly.

When you work on a chatbot that you want to test, you have two options:

1. Write the message twice, in controller and tests, and remember to update both if something is modified.
2. Store your messages somewhere you can call in your controller and tests.

I decided to go with number 2, and also decided to store the messages in Laravel translations files. This project is not multi-lingual (for now 😉) but I think that having the messages there makes it easy to:

- Refactor the message.
- Read code, it's easy to see `ohdear.token.stored` than a whole sentence.
- Test the message is sent to the user.
- Re-use the message in another conversation.
- Translate the whole app in a future, since you have all the texts in a single place.

This bot is currently running and you can talk to it [here](http://t.me/MrDear_bot) or by searching `Mr. Dear` on Telegram. You can also deploy it into your own server since it's Open Source. You would find instructions about how to do that in the [README.md](https://github.com/lloople/bot-mr-dear) of the project.