DEV Community

Cover image for I Built a Python WhatsApp Bot to Keep Me Sane During Quarantine

I Built a Python WhatsApp Bot to Keep Me Sane During Quarantine

Zhang Zeyu on May 02, 2020

This pandemic has taken a huge toll on my mental and emotional health. In order to keep me occupied and brighten up the lives of those around me, I...
Collapse
 
mirdotbhatia profile image
Mir Bhatia

This is a really nice post! I tried to implement my own version of it, but have run into errors while sending requests to Twilio. The logs say that my ngrok URL is returning a 404. I've done exactly what you have and thus this is very annoying.

Do you have any ideas as to what the issue may be?

Collapse
 
zeyu2001 profile image
Zhang Zeyu

Hi there, thanks for reading :)

Can you check whether the URL you set in the Twilio sandbox configuration includes the /bot/ at the end? In our urls.py, we have configured the application on the /bot/ URL. So if the nrgok URL is c21d2af6.ngrok.io, you would have to use c21d2af6.ngrok.io/bot/.

Alternatively, you could change this by editing this line in urls.py

path('bot/', include('bot_app.urls')),

to

path('', include('bot_app.urls'))

Let me know if this helps!

Collapse
 
mirdotbhatia profile image
Mir Bhatia

Hi, thanks for the quick response.

It still doesn't work, unfortunately. The URLs change every time the server is started, yes? I tried to play around with that and added /bot/ at the end, but it still doesn't seem to work for me.

/bot/ is added only in the Twilio config, yes? Or does that go in the python file too. It throws an error rn, but I just want to be sure.

Thread Thread
 
zeyu2001 profile image
Zhang Zeyu

Yup, the ngrok URLs change every time you start ngrok, and /bot/ is added only in the Twilio config.

Have you added the ngrok URL to your ALLOWED_HOSTS in settings.py?

Is Python raising any errors or is it just a 404? If it's just a 404, you might want to check your urls.py files in both the bot and the bot_app folder.

The source code is available at my GitHub, so you can check against that.

Thread Thread
 
mirdotbhatia profile image
Mir Bhatia

Perfect, I'll compare my code against your github repo and then let you know if something's up.

Thanks!

Collapse
 
tatianapg profile image
Tatiana Guanangui • Edited

Hi Zhang Zeyu,
Congrats! Great tutorial, I am learning a lot. I have an error:
"requests.exceptions.ConnectionError: HTTPSConnectionPool(host='api.apify', port=443): Max retries exceeded with url:..."
I think it is related to a restriction about apify.com.
Please could you tell how to solve this? Could you suggest a tutorial to start with Apify?

Thanks, nice job! Greetings from Ecuador!

Collapse
 
artgoblin profile image
artgoblin

hey great post,I just want to know what these part of codes do:
1>data['data']['status'] == "SUCCEEDED"
what value does ['data'] and ['status'] contains.
2>memes = data['data']['children']:
same for this one also what values do ['children'] contain.

Collapse
 
tatianapg profile image
Tatiana Guanangui

Thanks Zeyu, my bot is working! I did it!

Collapse
 
zeyu2001 profile image
Zhang Zeyu

Congrats! Sorry for the late reply, but to answer your previous question, the old Python requests library has an unintuitive error message. There was actually an issue raised on the GitHub repo of the requests library about this (check out github.com/psf/requests/issues/1198).

The error says "Max retries exceeded" when it has nothing to do with retrying. What happens is that the requests library "wraps" around urllib3's HTTPConnectionPool function, and the original exception message raised by urllib3 was shown as part of the error raised by the requests library.

Indeed this can be very confusing for end-users who don't know the source code of the requests library.

This seems to be an old issue and has since been fixed, so I suggest updating your requests library to the newest version for more meaningful error messages.

(In short, it's not an issue with Apify. Rather, it's an unintuitive error message telling you that your connection was refused - this could happen for any number of reasons, including a typo in your URL. Cheers!)

Collapse
 
tatianapg profile image
Tatiana Guanangui

Thansk Zeyu. I fixed my error creating my own urls in apify. You are right because the links worked well in isolation. Indeed, the errors are really hard to debug because the message is not clear.

Have a nice weekend!
Tatiana

Collapse
 
rafaacioly profile image
Rafael Acioly

Nice post! :)

isn't twilio too expensive for whatsapp API?

Collapse
 
zeyu2001 profile image
Zhang Zeyu

Thank you! Perhaps, but I think it's good enough for a fun personal side project without having to register for a WhatsApp business account :)

Collapse
 
sergejacko8 profile image
Sergio Toledo • Edited

Hey! Nice post! It's very complete
I have only one question: Is it normal to get a screen like in the pictures that I am linking with this comment? It's because I donĀ“t know if it is normal to have the server running correctly and getting no response in my smartphone when I type "hello" but in the ngrok I got an "OK" and in the console I got "POST / HTTP/1.1" 200 16351

I let the links from my screenshots showing this. Thank you

dev-to-uploads.s3.amazonaws.com/i/...
dev-to-uploads.s3.amazonaws.com/i/...

Collapse
 
zeyu2001 profile image
Zhang Zeyu • Edited

Hey there! It looks like your server is running correctly, since it is receiving POST requests from Twilio and responding with a 200 OK status code. Let's narrow down the problem here. Perhaps your server is responding as intended to Twilio, but the HttpResponse is not in the correct format accepted by Twilio? Make sure you are using the Twilio MessagingResponse object correctly. Can you also check your Twilio debugger (twilio.com/console/debugger/) for any error messages? I'm fairly certain the error has to do with how Twilio interprets your server's response, so you might find some useful information in the Twilio debugger. Keep me posted!

Collapse
 
delta456 profile image
Swastik Baranwal

Nice post! It is really informative!

Collapse
 
zeyu2001 profile image
Zhang Zeyu

Thank you!

Collapse
 
dlainfiesta profile image
Diego Lainfiesta

Great post!

Collapse
 
skeith profile image
Yukirin

I have been trying to try building a Whatsapp Bot.

Will this bot work on a group chat?

Collapse
 
zeankundev profile image
zeankun.dev

So I found a meme API, but I want to implement it on your code. How to do it?

Collapse
 
abhishekkumar98 profile image
abhishek-kumar98

Is there a way for me to message someone else and not my number through this API?

Collapse
 
micaelamaria profile image
Mica

Hi! Awesome post, thank you! There is one thing I do not get yet: How do I connect it to a whatsapp group, or I associate it to a whatsapp number?

Collapse
 
joshthecodingaddict profile image
Joshua Hassan

Can this bot be used in a WhatsApp group chat