You are browsing as a guest. Sign up (or log in) to start making projects!

Ship #1

I made a Python Slack bot with three commands: /clock, /remindme, and /whatstheweather. The hardest part was getting each command to work properly and making sure the bot could respond to users through DMs. I also had to work out how to keep the bot running when my laptop isn’t on, which meant setting it up on a cloud host.

I’m most proud of having three different useful commands working together in one bot. I also added error handling for things like missing commands, invalid reminder times and weather requests that don’t work.

To test it, use /clock, /remindme 1 test reminder, or /whatstheweather Sydney in Slack. The bot will respond through DMs, which can be found under Activities.

Try project → See source code →
Open comments for this post

53m 28s logged

Devlog 2

Once /clock was working, I started adding the other two commands. I wanted each command to do something different so the bot would meet the requirement of having at least three functions rather than having three commands that were basically the same.

The first one I added was /remindme. The command takes a number of minutes followed by some text, for example /remindme 5 Finish my homework. I used split(maxsplit=1) so that the first part can be treated as the time while everything after it stays as the reminder message.

For the actual delay, I used Python’s threading.Timer. This lets the bot continue running normally while it waits for the reminder. When the timer finishes, the bot sends a DM to the person who created the reminder.

I then added /weather. Instead of using an API that needs an account/API key, I used the free wttr.in service. The city is taken directly from what the user types after the command and the response is sent back to the person as a DM.

After adding both commands, I tested different inputs, including leaving out the city for the weather command and entering something that wasn’t a number for the reminder. This helped me add some basic messages for incorrect input instead of letting the program crash.

0
0
19
Open comments for this post

51m 49s logged

Devlog 1

I started by getting the basic Slack Bolt app running through Socket Mode. I chose Socket Mode because I wanted to be able to test the bot directly from my computer without having to set up a public server first.

For my first actual command, I made /clock. I wanted this to be slightly more useful than just displaying the computer’s time, so I used Slack’s user information to find the timezone of whoever ran the command. This means the command doesn’t need a timezone written into the code and should work for different users.

I had to use users_info to get the user’s profile and then use the tz value from it. After getting the timezone, Python’s zoneinfo module can convert the current time to the correct local time.

I tested the command in Slack and checked that the time it returned matched my timezone.

The ‘hard’ part was getting the bot to actually give the time, but it turns out it was giving it to me after all, I was just looking in the wrong spot 😆.

0
0
16

Delete project?

Are you sure you want to permanently delete this project? This action cannot be undone.

All devlogs, followers, and associated data will be removed.

Followers

Loading…