💡 Examples#

Note

WORK IN PROGRESS

Send a text message#

Text message#
 1from pywa import WhatsApp
 2
 3wa = WhatsApp(phone_id='972123456789', token='xxxxx')
 4
 5recipient = '972987654321'
 6wa.send_message(to=recipient, text='Hello world!')
 7
 8# Message with link preview
 9wa.send_message(
10    to=recipient,
11    text='PyWa Documentation: https://pywa.readthedocs.io',
12    preview_url=True
13)

Send a message with a media#

Media message#
 1from pywa import WhatsApp
 2
 3wa = WhatsApp(phone_id='972123456789', token='xxxxx')
 4recipient = '972987654321'
 5
 6# - From URL
 7wa.send_image(to=recipient, image='https://cdn.pixabay.com/photo/2014/10/01/10/44/animal-468228_1280.jpg')
 8
 9# - From local file
10wa.send_video(to=recipient, video='/path/to/video.mp4')
11
12# - From file id (You need to upload the media first with WhatsApp.upload_media to get the file id)
13wa.send_audio(to=recipient, audio='audio_file_id')
14
15# - From bytes
16content = requests.get('https://cdn.pixabay.com/photo/2014/10/01/10/44/animal-468228_1280.jpg').content
17wa.send_document(to=recipient, document=content)

Send a message with selection keyboard#

  • You can use selection keyboard only with text messages.

  • The maximum number of section rows is 10.

Color selection message#
 1from pywa import WhatsApp
 2from pywa.types import SectionList, Section, SectionRow
 3
 4wa = WhatsApp(phone_id='972123456789', token='xxxxx')
 5recipient = '972987654321'
 6
 7wa.send_message(
 8    to=recipient,
 9    header='Select your favorite color',
10    text='Tap a button to select your favorite color:',
11    footer='⚡ Powered by PyWa',
12    keyboard=SectionList(
13        button_title='Colors',
14        sections=[
15            Section(
16                title='Popular Colors',
17                rows=[
18                    SectionRow(
19                        title='🟥 Red',
20                        callback_data='color:red',
21                        description='The color of blood',
22                    ),
23                    SectionRow(
24                        title='🟩 Green',
25                        callback_data='color:green',
26                        description='The color of grass',
27                    ),
28                    SectionRow(
29                        title='🟦 Blue',
30                        callback_data='color:blue',
31                        description='The color of the sky',
32                    )
33                ],
34            ),
35            Section(
36                title='Other Colors',
37                rows=[
38                    SectionRow(
39                        title='🟧 Orange',
40                        callback_data='color:orange',
41                        description='The color of an orange',
42                    ),
43                    SectionRow(
44                        title='🟪 Purple',
45                        callback_data='color:purple',
46                        description='The color of a grape',
47                    ),
48                    SectionRow(
49                        title='🟨 Yellow',
50                        callback_data='color:yellow',
51                        description='The color of the sun',
52                    )
53                ]
54            )
55        ]
56    )
57)

Send a message with buttons keyboard#

  • You can attach up to 3 buttons to a message.

YouTube video info message#
 1from pywa import WhatsApp
 2from pywa.types import Button
 3
 4wa = WhatsApp(phone_id='972123456789', token='xxxxx')
 5
 6recipient = '972987654321'
 7requested_vid_id = 'T9RRe4ZsSGw'
 8
 9wa.send_image(
10    to=recipient,
11    image=f'https://i.ytimg.com/vi/{requested_vid_id}/hqdefault.jpg',
12    caption='Chandler Jokes | Friends • 2.9M views • 1 year ago',
13    footer='⚡ Powered by PyWa',
14    buttons=[
15        Button(title='⬇️ Download', callback_data=f'dl:{requested_vid_id}'),
16        Button(title='💬 Comments', callback_data=f'cmnts:{requested_vid_id}'),
17        Button(title='🎬 Info', callback_data=f'info:{requested_vid_id}'),
18    ]
19)