Using a Raspberry Pi to create a Groupme Bot

I recently had a good friend who is in a group chat forget to tell us something. So to remind him. I created a groupme bot that sends him the same message every hour, on the hour. Here’s a breakdown of what I did.

 

For this tutorial, I’m going to assume you know how to SSH into your pi and use some of the basic command lines.

Create Bot in Groupme

The first step to create a chatbot in groupme is to register it and get an API key. To do this, you visit their dev site. Once you log in, navigate to the bot page on the top, and the click “Create Bot”.

On the creation page. select the chat you want the bot in. In my case, i set up a separate chat so that it wasn’t constantly pinging the main chat. After selecting which chat you want the bot in, you can name it and give it an avatar.

Don’t worry about assigning a callback URL, we won’t be needing one for this basic bot.

Setting up the Script

Once your bot is create, the page should list our your bots ID, and a test CURL command if you want. Here’s an example of what the test command looks like:

curl -d '{"text" : "Your message here", "bot_id" : "BOT_ID_GOES_HERE"}' https://api.groupme.com/v3/bots/post

Copy the test script for now, we’ll use it. Next, SSH into your pi and travel to the folder you want. For me, I created a folder on my desktop. Inside the folder of your choosing. Create a shell file:

touch reminder.sh

With your file created. We’ll use the nano command to edit it. If you haven’t used nano before, its a built in command tool for editing files in the command line.

nano reminder.sh

Now that nano is open, we’ll start the file out by declaring that bash will be used. Then we’ll past the example code that groupme gave us. When it’s done. it should look like this:

#!/bin/bash

curl -d '{"text" : "Reminder: Dont forget to pick up laundry", "bot_id" : "BOT_ID_GOES_HERE"}' https://api.groupme.com/v3/bots/post

The First line declares that this script will be using bash to execute the commands inside the file. The 2nd line uses the CURL command, is used to send our message (in the form of JSON) to the Groupme API. The “-d” after curl is what is referred to as a flag, or option. Flags are denoted with a minus/hyphen in front of it.

In this case, the -d flag means that we’ll be using a regular POST method to send everything inside the quotation marks, which is JSON, to the address we provide (the Groupme API).

Our script is finished! We can now exit Nano and set up the job that runs our script every hour.

Setting up an Executable Script

We’ll be using a cronjob to execute our script every hour. However, in-order for that to work, we need to make our script executable. To check if your script is executable, make sure you’re in the folder with your script in it and run

ls -l

This command will list out all files in the folder, and there permissions. Our file permissions will need to be -rwxr-xr-x. To change your file to be executable you can run the following command to update it.

sudo chmod +x reminder.sh

Your reminder file should be executable now! To test if it’s working, run sh reminder.sh in the folder with the script to test it. If your formatting is correct, you should be able get the message in chat. Any formatting or syntax error should show up in your command line.

Scheduling the Script

To run our script, we’ll be setting up a cronjob to run the script every hour, on the hour. If you’ve never set up a cronjob before, it’s a simple way to runĀ sh without you having to be there. To edit your cronjob run:

crontab -e

This command will bring up the nano editor for the crontab file. The first few lines explain the basics of how it all works, with an example. On the last line of the file, we’ll paste the following:

0 * * * * /home/pi/Desktop/reminder-example/reminder.sh

The 0 at the beginning means that we’ll run our script at the top of the 0. Putting “15” here would run the script every hour at 15 past the hour (ie: 5:15, 6:15, 7:15).

Once you save your changes in the cronjob file, your script should run every hour, on the hour. When it runs it should send the message you typed out!

Extra Credit

Once i got this script running I updated it a few times. I modified the JSON that it sends to include a mention, that way, he would get a notification every time the message was sent (A jerk move, but effective). To dive more into how its possible, export messages from one of your chats (I suggest a small chat with few messages, long chats could take a long time). Use a JSON reader to explore how messages are arranged.

I also updated the cronjob a few times, to change the frequency of the time. instead of 0 or 15 at the beginning, you can use a comma. So to run a script every 30 minutes you can use: 0,30 * * * * or 15,45 * * * *, to run a script at those respective times. You can also use a shortened version to run it every 10 minutes, instead of specifying a time, you use */ before the numbers to specify intervals. */5 * * * * would get you every 5 minutes.