11🤖 Demo Bots
22============
33
4- This page contains some examples of bots you can create using pywa.
5- Every example is a complete working bot that you can run on your own server.
4+ This page contains complete, working examples of bots you can create using pywa.
65
76👋 Hello Bot
87--------------
@@ -12,15 +11,11 @@ This is a simple bot that welcomes the user when they send a message.
1211.. code-block :: python
1312 :linenos:
1413
15- import flask # pip3 install flask
1614 from pywa import WhatsApp, types
1715
18- flask_app = flask.Flask(__name__ )
19-
2016 wa = WhatsApp(
2117 phone_id = ' your_phone_number' ,
2218 token = ' your_token' ,
23- server = flask_app,
2419 verify_token = ' xyzxyz' ,
2520 )
2621
@@ -29,8 +24,7 @@ This is a simple bot that welcomes the user when they send a message.
2924 msg.react(' 👋' )
3025 msg.reply(f ' Hello { msg.from_user.name} ! ' )
3126
32- # Run the server
33- flask_app.run()
27+ # Run the server with `pywa dev`
3428
3529
3630 📝 Echo Bot
@@ -42,15 +36,11 @@ This is a simple bot that echoes back the user's message.
4236.. code-block :: python
4337 :linenos:
4438
45- import flask # pip3 install flask
4639 from pywa import WhatsApp, types
4740
48- flask_app = flask.Flask(__name__ )
49-
5041 wa = WhatsApp(
5142 phone_id = ' your_phone_number' ,
5243 token = ' your_token' ,
53- server = flask_app,
5444 verify_token = ' xyzxyz' ,
5545 )
5646
@@ -59,11 +49,9 @@ This is a simple bot that echoes back the user's message.
5949 try :
6050 msg.copy(to = msg.sender, reply_to_message_id = msg.message_id_to_reply)
6151 except ValueError :
62- msg.reply_text(" I can't echo this message" )
63-
64- # Run the server
65- flask_app.run()
52+ msg.reply(" I can't echo this message" )
6653
54+ # Run the server with `pywa dev`
6755
6856
6957 ⬆️ Url Uploader Bot
@@ -74,16 +62,11 @@ This is a simple bot that uploads files from URLs.
7462.. code-block :: python
7563 :linenos:
7664
77- import flask # pip3 install flask
7865 from pywa import WhatsApp, types, filters, errors
79- from pywa.types import Message, MessageStatus
80-
81- flask_app = flask.Flask(__name__ )
8266
8367 wa = WhatsApp(
8468 phone_id = ' your_phone_number' ,
8569 token = ' your_token' ,
86- server = flask_app,
8770 verify_token = ' xyzxyz' ,
8871 )
8972
@@ -94,10 +77,9 @@ This is a simple bot that uploads files from URLs.
9477 # When a file fails to download/upload, the bot will reply with an error message.
9578 @wa.on_message_status (filters.failed_with(errors.MediaDownloadError, errors.MediaUploadError))
9679 def on_media_download_error (_ : WhatsApp, status : types.MessageStatus):
97- status.reply_text (f " I can't download/upload this file: { status.error.details} " )
80+ status.reply (f " I can't download/upload this file: { status.error.details} " )
9881
99- # Run the server
100- flask_app.run()
82+ # Run the server with `pywa dev`
10183
10284
10385 🔢 Calculator WhatsApp Bot
@@ -115,15 +97,11 @@ Usage:
11597.. code-block :: python
11698
11799 import re
118- import flask # pip3 install flask
119100 from pywa import WhatsApp, types, filters
120101
121- flask_app = flask.Flask(__name__ )
122-
123102 wa = WhatsApp(
124103 phone_id = ' your_phone_number' ,
125104 token = ' your_token' ,
126- server = flask_app,
127105 verify_token = ' xyzxyz' ,
128106 )
129107
@@ -153,9 +131,7 @@ Usage:
153131 return
154132 msg.reply(f ' { a} { op} { b} = * { result} * ' )
155133
156- # Run the server
157- flask_app.run()
158-
134+ # Run the server with `pywa dev`
159135
160136 🌐 Translator Bot
161137-----------------
@@ -166,17 +142,14 @@ A simple WhatsApp bot that translates text messages to other languages.
166142 :linenos:
167143
168144 import logging
169- import flask # pip3 install flask
170145 import googletrans # pip3 install googletrans==4.0.0-rc1
171146 from pywa import WhatsApp, types, filters
172147
173- flask_app = flask.Flask(__name__ )
174148 translator = googletrans.Translator()
175149
176150 wa = WhatsApp(
177151 phone_id = ' your_phone_number' ,
178152 token = ' your_token' ,
179- server = flask_app,
180153 verify_token = ' xyzxyz' ,
181154 )
182155
@@ -199,7 +172,7 @@ A simple WhatsApp bot that translates text messages to other languages.
199172
200173 @wa.on_message (filters.text)
201174 def offer_translation (_ : WhatsApp, msg : types.Message):
202- msg_id = msg.reply_text (
175+ msg_id = msg.reply (
203176 text = ' Choose language to translate to:' ,
204177 buttons = types.SectionList(
205178 button_title = ' 🌐 Choose Language' ,
@@ -238,27 +211,26 @@ A simple WhatsApp bot that translates text messages to other languages.
238211 original_text = MESSAGE_ID_TO_TEXT [sel.reply_to_message.message_id]
239212 except KeyError : # If the bot was restarted, the message ID is no longer valid.
240213 sel.react(' ❌' )
241- sel.reply_text (
214+ sel.reply (
242215 text = ' Original message not found. Please send a new message.'
243216 )
244217 return
245218 try :
246219 translated = translator.translate(original_text, dest = lang_code)
247220 except Exception as e:
248221 sel.react(' ❌' )
249- sel.reply_text (
222+ sel.reply (
250223 text = ' An error occurred. Please try again.'
251224 )
252225 logging.exception(e)
253226 return
254227
255- sel.reply_text (
228+ sel.reply (
256229 text = f " Translated to { translated.dest} : \n { translated.text} "
257230 )
258231
259232
260- # Run the server
261- flask_app.run()
233+ # Run the server with `pywa dev`
262234
263235
264236 🖼 Random image bot
@@ -270,16 +242,11 @@ This example shows how to create a simple bot that replies with a random image f
270242.. code-block :: python
271243 :linenos:
272244
273- import requests
274- import flask
275245 from pywa import WhatsApp, types
276246
277- flask_app = flask.Flask(__name__ )
278-
279247 wa = WhatsApp(
280248 phone_id = ' your_phone_number' ,
281249 token = ' your_token' ,
282- server = flask_app,
283250 verify_token = ' xyzxyz' ,
284251 )
285252
@@ -291,8 +258,7 @@ This example shows how to create a simple bot that replies with a random image f
291258 buttons = types.ButtonUrl(title = ' Unsplash' , url = ' https://unsplash.com' )
292259 )
293260
294- # Run the server
295- flask_app.run()
261+ # Run the server with `pywa dev`
296262
297263
298264 📸 Remove background from image
@@ -303,16 +269,13 @@ This example shows how to create a bot that removes the background from an image
303269.. code-block :: python
304270 :linenos:
305271
306- import requests
307- import flask
272+ import logging
273+ import httpx
308274 from pywa import WhatsApp, types
309275
310- flask_app = flask.Flask(__name__ )
311-
312276 wa = WhatsApp(
313277 phone_id = ' your_phone_number' ,
314278 token = ' your_token' ,
315- server = flask_app,
316279 verify_token = ' xyzxyz' ,
317280 )
318281
@@ -324,7 +287,7 @@ This example shows how to create a bot that removes the background from an image
324287 files = {' image_file' : original_img}
325288 data = {' size' : ' auto' }
326289 headers = {' X-Api-Key' : REMOVEBG_API_KEY }
327- response = requests .post(url, files = files, data = data, headers = headers)
290+ response = httpx .post(url, files = files, data = data, headers = headers)
328291 response.raise_for_status()
329292 return response.content
330293
@@ -334,8 +297,8 @@ This example shows how to create a bot that removes the background from an image
334297 try :
335298 original_img = msg.image.download(in_memory = True )
336299 image = get_removed_bg_image(original_img)
337- except requests .HTTPError as e:
338- msg.reply_text( f " A error occurred" )
300+ except httpx .HTTPError as e:
301+ msg.reply( " An error occurred" )
339302 logging.exception(e)
340303 return
341304 msg.reply_image(
@@ -344,5 +307,4 @@ This example shows how to create a bot that removes the background from an image
344307 mime_type = ' image/png' , # when sending bytes, you must specify the mime type
345308 )
346309
347- # Run the server
348- flask_app.run()
310+ # Run the server with `pywa dev`
0 commit comments