Pocket Screw
- 3 Devlogs
- 2 Total hours
Helping you stay productive, 24/7!
Helping you stay productive, 24/7!
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.
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 😆.