loading...
Cover image for Need a hug? There's a bot for that!

Need a hug? There's a bot for that!

yechielk profile image Yechiel Kalmenson ・3 min read

Hey all!

I know I haven't posted in a while, but after a long break I finally had a free weekend and was able to work on a side project (my first one in over a year! Don't believe the gatekeepers, you can have a successful career without side projects!)

It all started a few weeks ago when I was feeling down and felt like I could really use a hug.

Not being close to anyone with whom I'm friends on a hugging basis, I did the next best thing and turned to Twitter:

Sometime later, I was tweeting with a friend who was feeling down, and they mentioned that they could use a hug as well. I immediately sent them a (virtual) hug as well. And the idea came up for a bot that would help people feeling down by sending hugs at them!

a cat hugging her teddy bear

I'm no stranger to Twitter bots, having written one some time ago:

This time I decided to write one in Python.

Python has a great library for interacting with Twitter called tweepy, and even a library around Giphy that makes things like getting a random gif of a hug pretty straightforward!

Between those two libraries, I didn't have to write much code on my own. The whole bot is about 35 lines of code!

I followed this dev.to post by @emcain for instructions on how to get my bot set up on Heroku:

And by the end of the weekend, @ICanHazHugzPlz was born!

a rabbit saying "do you need a hug? Have one! Maybe you do even if you don't think so"

Scope Reduction

Of course, no project comes out the way you expect it.

I had initially envisioned that the bot would reply to people asking it for hugs with a gif of a hug.

It turns out that to implement that, I would have to work out a queue that could keep track of which tweets the bot already replied to. That queue would need to maintain state between restarts; otherwise, the bot would keep spamming anyone who tweeted at it every time the script restarts (multiple times a day on Heroku's free dyno), as my bots earliest followers found out the hard way (sorry!!!)

I wasn't planning on spending more than a weekend on the project, so in the end, I settled on a bot that tweets a hug at the world every few hours.

Show Me The Code!

If you would like to take a look at the code for the bot, you'll find it here (including the failed implementation of the replying version of the bot on a separate branch):

GitHub logo achasveachas / hug-bot

A twitter bot that replies to mentions with a hug.

Hug Bot

Hug Bot is a Twitter bot written in Python that tweets a random hug gif every few hours.

You can follow @ICanHazHugzPlz on Twitter for all your huggy needs.

Two dudes hugging wholesomely

License

This bot is licensed under the MIT license.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/achasveachas/hug-bot/issues or by email to contact@yechiel.me.

This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.

Contact

Feel free to get in touch with me if you have any suggestions or comments at yechiel.me




And of course, if you'd like to bless your Twitter timeline with wholesome hugs, please give my bot a follow at @ICanHazHugzPlz!

Until next time!

Rory Gilmore from Gilmore Girls giving a hug

Discussion

pic
Editor guide
Collapse
iceorfiresite profile image
Ice or Fire

"I just took a DNA tet, turns out I'm 100% hugs!" should be DNA test. Very cool project!

Collapse
yechielk profile image
Yechiel Kalmenson Author

Lol whoops, good catch! I'll fix it tonight.

But you can submit a PR before I get around to it if you'd like 😊

Collapse
vickilanger profile image
Vicki (she/her)

I used @emcain ’s tutorial too! I love your bot! Hugs for making a hugs bot!!! 🤗 🤗

Collapse
yechielk profile image
Yechiel Kalmenson Author

I know! I got it from your README 😂

Hugs back! 🤗

Collapse
vickilanger profile image
Vicki (she/her)

Oh! The benefits of documentation. Glad I could help. 🤗

Thread Thread
yechielk profile image
Yechiel Kalmenson Author

Never underestimate the power of a great README!

Collapse
thecodepixi profile image
Emily A. Pixi

I love this entirely too much!

Collapse
yechielk profile image
Yechiel Kalmenson Author

But is it really too much? 🤗

Collapse
archeelux profile image
Arturs Timofejevs

Haha this is awesome!

Collapse
yechielk profile image
Collapse
emcain profile image
Emily Cain (@data_bae)

I'm so glad I could help 💖 your hug bot is lovely!

Collapse
yechielk profile image
Yechiel Kalmenson Author

Thank you! 🤗