
Build a WhatsApp Chatbot with Python, Flask and Twilio - makaimc
https://www.twilio.com/blog/build-a-whatsapp-chatbot-with-python-flask-and-twilio
======
namanaggarwal
I really didn't like the WhatsApp approach on this. Basically only partnering
with few big vendors like twilio and then asking people to build apps via them
(by basically paying for it).

Other apps like telegram allows anyone to directly interact with them

~~~
bikamonki
FB paid something like 20bn for Whatsapp almost 6 years ago. Something like
4bn was paid in cash. How do you make that money back if: a) users do not pay
a subscription and b) ads are not printed in the app? You leverage the app's
immense user base asking businesses to pay for API services so they can
implement chat-bots, notifications, in-app tools, etc.

Why using res-sellers, like carriers use re-sellers to sell SMS integration?
Distribution and fair-use control. First, companies like Twilio are the
natural partners to sell one more channel to customers that already integrate
to SMS. Second, partners will help enforce and control anti-spam efforts. If
opening Whatsapp API turns it into a spam jungle like email or sms, it will
certainly lose its appeal and users will start switching to the next new spam-
free chat app.

~~~
cloverich
They don't necessarily need to put ads in the app. They can simply transfer
that personal information to their larger people profiles making their
existing ad networks more valuable. It could additionally be simply a play to
keep someone _else_ from getting said data. (I think your argument still makes
sense, just another angle on that part).

~~~
jeroenhd
The privacy law implications (GDPR, the new California law, etc.) would
probably crush Facebook as a company if they started doing that.

They already paid over 100 million for just making false promises when they
bought WhatsApp, now that the EU has sharpened their blades to take on big
data farms like Google and Facebook, doing something like that would be
suicide for FB. Any additional data farming would probably attract auditors
and lawsuits that Facebook can't get out of so easily anymore.

Facebook would need to put a lot of work into following the GDPR (because
someone using an Asian VPN is still protected, so IP based geoblocking won't
work) to prevent a fine. It would be a very risky move.

I don't think Facebook is going to sell private WhatsApp information any time
soon.

~~~
cloverich
I'm mostly speculating but, they wouldn't be selling the data, they would be
selling access to their ad network that has models built off that data used to
match ads to people. If they have a high(er) conversion rate, they can charge
quite the premium for this information. Similarly, if they could merely keep
people in their platform (WhatsApp), nobody else has that information -- same
effect.

(Also speculating) if that's more or less how it works, and an ads
conglomerate like Google or Facebook is worth hundreds of billions, then even
a fine of a billion dollars wouldn't be enough to deter them from this
activity.

None of this is meant to stir up controversy or be negative or anything, I
just think its an interesting mental exercise to consider this scenario and
these motivations.

------
air7
This is nice but quite expensive. Up to 0.09$ per message inbound or outbound!

Pricing for Template messages

Outbound template messages to WhatsApp destinations have two different
components:

Edit: Here's the price breakdown. [0] > Twilio charges a flat fee of $0.005
per message for all WhatsApp messaging. This applies to all incoming messages,
outbound Template and Session messages. > WhatsApp also charges a per-message
fee to send outbound Template messages. This fee may vary, depending on the
destination country or region.

The second fee varies wildly up to 0.09$ in Germany! [1]

[0] [https://support.twilio.com/hc/en-
us/articles/360037672734-Ho...](https://support.twilio.com/hc/en-
us/articles/360037672734-How-Much-Does-it-Cost-to-Send-and-Receive-WhatsApp-
Messages-with-Twilio-) [1]
[https://developers.facebook.com/docs/whatsapp/pricing/](https://developers.facebook.com/docs/whatsapp/pricing/)

~~~
bouncing
Feel beholden to cell phone carriers? Check out WhatsApp -- it uses the
Internet to eliminate absurd and capricious regional fees.

------
3riverdev
Hey all, I'm the engineering manager for teams at Twilio responsible for
WhatsApp integration, among other channels. We're more than happy to answer
any technical or general questions -- fire away with replies. Will keep an eye
on it today.

~~~
samvher
It seems there is no support for letting a user share their location over
WhatsApp through the API at this moment - I was wondering why that is and if
you're expecting that that will change soon? For the rest I've experimented
with the system and it's been working quite well, thanks for the work!

~~~
3riverdev
Glad to hear it! Location support actually launched just last week! Check out
[https://www.twilio.com/blog/new-rich-features-support-
deeper...](https://www.twilio.com/blog/new-rich-features-support-deeper-
customer-engagement-twilio-api-whatsapp) and
[https://www.twilio.com/docs/sms/whatsapp/api#send-a-
location...](https://www.twilio.com/docs/sms/whatsapp/api#send-a-location-
message-with-whatsapp) for details

~~~
samvher
That's great to hear, thanks!

------
aacook
I'm about to release a bot for Whatsapp for NanaGram
([https://nanagram.co](https://nanagram.co)). Amped!

The user experience of SMS-first products is novel and fun. There's something
magical about snapping a photo and just texting it to a unique NanaGram number
powered by Twilio. It takes just a couple seconds. I'm excited to deliver the
same experience through Whatsapp to customers around the globe and people on
wifi.

Building NanaGram SMS-first has been a challenge. There have been all kinds of
gotchas:

1) AT&T started blocking any number permanently across its network last
January if it contains a hyperlink or email address, even if the customer
interacts with the number. I had to create separate messages just for the
12.5% AT&T users.

2) Twilio only works when you have cell service. If you're indoors with wifi
only, the text won't go through.

3) Images are downsized in an unpredictable way by the carriers. If you're
sending 1 or a couple images, they're pretty good quality. If you send ~5+
photos, they get downsized pretty far and luckily they're still good enough
for 4x6 photo printing. Certain carriers like cricket are super aggressive
with downsizing.

4) When Apple users with iCloud enabled share more than 1 image, it generates
a sharing album which has been a beast to support. Then there's the added
challenge of Apple's recent move to HEIC/HEIF images.

5) Twilio doesn't support group threads (WhatsApp doesn't yet, either)

... I could go on and on.

Getting approval from Whatsapp took a while. I first applied about a year ago
and didn't hear anything, then applied again and was approved a few weeks ago.
I'm guessing the approval layer is to ensure only high-quality bots make the
cut. They also have an approval layer for all of your messages sent outside a
24-hour window of the last user interaction. The approval for that took a few
days.

Integration with WhatsApp has been surprisingly easy. I basically just had to
change "+1{number}" to "whatsapp+1{number}"

~~~
pricechild
> 1) AT&T started blocking any number permanently across its network last
> January if it contains a hyperlink or email address, even if the customer
> interacts with the number. I had to create separate messages just for the
> 12.5% AT&T users.

I don't follow - they blocked any number which replied with a hyperlink or
email?

Can users of AT&T not send them to each other?

~~~
filoleg
I bet that AT&T can detect "non-real-user" phone numbers, so their hyperlink
filter only acts on those numbers.

I have been using Google Voice as my secondary phone number for the past 5
years, and some services manage to detect that it isn't a real phone number
and refuse to work with it. More specifically, it usually happens when I try
to use 2FA over SMS (note: i know that 2FA over SMS is not considered secure,
but some of the services I use only offer this as their 2FA, and it is better
than me not using 2FA at all). It doesn't just fail to deliver the message,
the service straight up tells me to enter a "real" phone number.

~~~
aacook
I've noticed this as well. Services like Twilio provide an endpoint to check
the carrier. For my Google Voice number, they return "Google (Grand Central)
BWI - Bandwidth.com - SVR."

------
ericmarcos
Chatbots on Whatsapp are kind of hard from a UX point of view because the
platform don't support rich elements like buttons or webviews (unlike Facebook
messenger). I hope Whatsapp adds these kind of features soon, otherwise users
will get frustrated when interacting with bots and never use it to talk to
businesses again. And no, AI/NLP is not the answer to this because users are
lazy and prefer to tap on buttons than to write 20 characters... I know this
because I'm the creator of Botonic (a React-based conversational framework)
and we've been building conversational apps on Whatsapp and other platforms
for years. By the way, if anyone is interested in access to the Whatsapp
Business API beta feel free to drop me a line at eric@botonic.io

~~~
pcroh
No thank you. I like whatsapp for its simplicity. If I wanted a copy of
Messenger or Telegram I would use those instead.

~~~
ericmarcos
Simplicity of features != UX Simplicity It's way more simple to tap on a
button than to write complex sentences. We've studied this, when users have
both options available (buttons and input text), 80% go for buttons.

~~~
pcroh
What I also meant is that I don't want bots on whatsapp.

------
sixhobbits
wow this is by Miguel Grinberg who is the author of some of the best Flask
tutorials and books around. Nice to see Twilio is attracting such high quality
writers for their blog

~~~
machbio
Miguel Grinberg was recently hired by Twilio

~~~
miguelgrinberg
Yes, this is true. I will be involved with the Twilio blog as part of my
duties, by writing original articles and also by working with other
contributors to get their posts published.

~~~
sixhobbits
oh nice! Long time fan of your work and I people get us confused sometimes
because PacktPub chose the title 'Flask by Example' for a book I wrote with
them which is too similar to your blog posts.

I nearly wrote an article for Twilio but didn't complete the process. But have
written similar posts e.g. [0] and work with other writers who also do this
form of instructional technical writing. Would love to chat sometime if you
have time - contact info in my profile

[0] [https://www.codementor.io/garethdwyer/building-a-telegram-
bo...](https://www.codementor.io/garethdwyer/building-a-telegram-bot-using-
python-part-1-goi5fncay)

------
mmsimanga
I am divided about this. In my home country WhatsApp is the Internet (along
with Facebook and Twitter). Mobile service providers sell WhatsApp data
bundles which are orders of magnitude more affordable than "normal" data
bundles. So we all communicate via WhatsApp. We use WhatsApp groups and it
works well. News, videos, pictures everything I get by WhatsApp from contacts.
IMHO it works well because there is not advertising and no bots spamming us.
Some of the groups are noisy but you can mute.

I am divided because I like WhatsApp as it is right now. But we are not paying
for it and the money has to come from somewhere. Using third party providers
like Twilio is probably one way to pay for it. I am divided because having to
use Twilio is likely to freeze out developers from my home country. C'est la
vie.

~~~
gcbw3
> we are not paying for it

"With location sharing data, 60 billion messages sent per day and access to
users' entire contact lists, Facebook has access to a ton of personal
information – all uploaded and saved on its servers."

[https://www.investopedia.com/articles/personal-
finance/04091...](https://www.investopedia.com/articles/personal-
finance/040915/how-whatsapp-makes-money.asp)

------
kuu
This is funny because just today I decided to join Telegram to develop a bot.

~~~
Zhyl
Telegram bot writing is silly easy. If you use the python module you can
basically just write the whole thing as a set of functions in a file (with
decorators that watch telegram for triggers) and one function that
automatically polls and does all the responding at the end.

I managed to bang out a few when I had a day off a few months back. Very
satisfying.

~~~
noxer
Writing a Telegram bot is easy yes but writing a good one isn't. There are
some very tricky things involved which you basically can not know and wont
find in the Documentation. It forces you to run into problems first and then
look for solutions. Especially if your bot gets decent amount of traffic
something in the range of 1msg/s and above.

The people at [https://t.me/BotDevelopment](https://t.me/BotDevelopment)
(unofficial) are your best chance for support because Telegram (bot) support
is a joke.

~~~
ddoeth
What are those tricky things?

------
yomansat
The pricing model is:

Template Message: $0.0085 WhatsApp fee + $0.005 Twilio fee = $0.0135 [1].

WhatsApp Session Message, sent or received: $0.005. A session ends 24h after
last user message [1].

Compared to just $0.0075 for sending/receiving SMS [2].

[1]:
[https://www.twilio.com/whatsapp/pricing/us](https://www.twilio.com/whatsapp/pricing/us)

[2]:
[https://www.twilio.com/sms/pricing/us](https://www.twilio.com/sms/pricing/us)

------
kwelstr
In the 90's I built an IRC chatbot, at the time users were naive enough to
think it was a real person, so I kept adding conversation lines. Over time it
grew into thousands of lines and some people came back to the channel just to
chat with it. My friends and I had some good laughs, good times heh.

------
stets
Do you have to pay for Twilio for something like this or does that only come
in for SMS/MMS?

