Skip to content

Commit 2f39b18

Browse files
SDK-36: Требуется добавить поддержку кнопок в SDK Python
1 parent bfdb79e commit 2f39b18

File tree

7 files changed

+268
-57
lines changed

7 files changed

+268
-57
lines changed

.github/workflows/python-package.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ on:
1111
jobs:
1212
build:
1313
name: ${{ matrix.python-version }}
14-
runs-on: ubuntu-20.04
14+
runs-on: ubuntu-24.04
1515
strategy:
1616
matrix:
17-
python-version: [ "3.7", "3.8", "3.9", "3.10", "3.11", "3.12" ]
17+
python-version: [ "3.8", "3.9", "3.10", "3.11", "3.12" ]
1818

1919
steps:
2020
- uses: actions/checkout@v3

README.md

Lines changed: 81 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ To start the bot, call the `bot.run_forever` function. You can stop the bot with
9999

100100
In this example, the bot will only answer the `message` message.
101101

102-
Link to example: [base.py](https://github.com/green-api/whatsapp-chatbot-python/blob/master/examples/base.py).
102+
Link to example: [base.py](./examples/base.py).
103103

104104
```
105105
@bot.router.message(text_message="message")
@@ -123,7 +123,7 @@ body.
123123

124124
In this example, the bot receives all incoming messages.
125125

126-
Link to example: [event.py](https://github.com/green-api/whatsapp-chatbot-python/blob/master/examples/event.py).
126+
Link to example: [event.py](./examples/event.py).
127127

128128
```
129129
@bot.router.message()
@@ -206,7 +206,7 @@ command = ("help", "!/")
206206

207207
In this example, the bot will send a photo in response to the `rates` command.
208208

209-
Link to example: [filters.py](https://github.com/green-api/whatsapp-chatbot-python/blob/master/examples/filters.py).
209+
Link to example: [filters.py](./examples/filters.py).
210210

211211
```
212212
@bot.router.message(command="rates")
@@ -217,32 +217,48 @@ def message_handler(notification: Notification) -> None:
217217
bot.run_forever()
218218
```
219219

220-
### How to handle buttons (deprecated)
221-
222-
The method is temporarily not working. When the method is called, a 403 error will be returned.
220+
### How to handle buttons
223221

224222
To be notified when a button is pressed, you must use the `bot.router.buttons` object.
225223

226-
Link to example: [buttons.py](https://github.com/green-api/whatsapp-chatbot-python/blob/master/examples/buttons.py).
224+
Link to example: [interactive_buttons.py](./examples/interactive_buttons.py).
225+
227226

228227
```
229-
@bot.router.buttons()
230-
def buttons_handler(notification: Notification) -> None:
231-
notification.answer_buttons("Choose a color", [
232-
{
233-
"buttonId": 1,
234-
"buttonText": "Red"
228+
@bot.router.message()
229+
230+
def show_interactive_buttons_handler(notification: Notification) -> None:
231+
notification.answer_with_interactive_buttons(
232+
"This message contains interactive buttons",
233+
[{
234+
"type": "call",
235+
"buttonId": "1",
236+
"buttonText": "Call me",
237+
"phoneNumber": "79123456789"
235238
},
236239
{
237-
"buttonId": 2,
238-
"buttonText": "Green"
240+
"type": "url",
241+
"buttonId": "2",
242+
"buttonText": "Green-api",
243+
"url": "https://green-api.com"
244+
}],
245+
"Hello!",
246+
"Hope you like it!"
247+
)
248+
249+
notification.answer_with_interactive_buttons_reply(
250+
"This message contains interactive reply buttons",
251+
[{
252+
"buttonId": "1",
253+
"buttonText": "First Button"
239254
},
240255
{
241-
"buttonId": 3,
242-
"buttonText": "Blue"
243-
}
244-
])
245-
256+
"buttonId": "2",
257+
"buttonText": "Second Button"
258+
}],
259+
"Hello!",
260+
"Hope you like it!"
261+
)
246262
247263
bot.run_forever()
248264
```
@@ -268,7 +284,7 @@ You also have the option to save the user's data in his state.
268284

269285
The first argument is the sender ID. It can be found by calling `notification.sender`.
270286

271-
Link to example: [states.py](https://github.com/green-api/whatsapp-chatbot-python/blob/master/examples/states.py).
287+
Link to example: [states.py](./examples/states.py).
272288

273289
```python
274290
from whatsapp_chatbot_python import BaseStates, GreenAPIBot, Notification
@@ -400,14 +416,18 @@ As an example, a bot was created to support the GREEN API. Command list:
400416
- 2 or Show office address (the bot will send the office address as a map)
401417
- 3 or Show available rates (the bot will send a picture of the rates)
402418
- 4 or Call a support operator (the bot will send a text message)
419+
- 5 or Show interactive buttons (the bot will send a message with interactive buttons)
420+
- 6 or Show interactive reply buttons (the bot will send a message with interactive reply buttons)
403421

404422
To send a text message, you have to use the `notification.answer` method.
405423
To send a location, you have to use the `sending.sendLocation` method from `notification.api`.
406424
To send a message with a file, you have to use the `notification.answer_with_file` method.
425+
To send a message with interactive buttons, you have to use `notification.answer_with_interactive_buttons` method.
426+
To send a message with interactive reply buttons, you have to use `notification.answer_with_interactive_buttons_reply` method.
407427

408428
In this example, the bot only responds to commands from the list above.
409429

410-
Link to example: [full.py](https://github.com/green-api/whatsapp-chatbot-python/blob/master/examples/full.py).
430+
Link to example: [full.py](./examples/full.py).
411431

412432
```python
413433
from whatsapp_chatbot_python import GreenAPIBot, Notification
@@ -428,19 +448,19 @@ def message_handler(notification: Notification) -> None:
428448
"1. Report a problem\n"
429449
"2. Show office address\n"
430450
"3. Show available rates\n"
431-
"4. Call a support operator\n\n"
451+
"4. Call a support operator\n"
452+
"5. Show interactive buttons\n"
453+
"6. Show interactive reply buttons\n\n"
432454
"Choose a number and send to me."
433455
)
434456
)
435457

436-
437458
@bot.router.message(text_message=["1", "Report a problem"])
438459
def report_problem_handler(notification: Notification) -> None:
439460
notification.answer(
440461
"https://github.com/green-api/issues/issues/new", link_preview=False
441462
)
442463

443-
444464
@bot.router.message(text_message=["2", "Show office address"])
445465
def show_office_address_handler(notification: Notification) -> None:
446466
chat = notification.chat
@@ -449,16 +469,50 @@ def show_office_address_handler(notification: Notification) -> None:
449469
chatId=chat, latitude=55.7522200, longitude=37.6155600
450470
)
451471

452-
453472
@bot.router.message(text_message=["3", "Show available rates"])
454473
def show_available_rates_handler(notification: Notification) -> None:
455-
notification.answer_with_file("data/rates.png")
474+
notification.answer_with_file("examples/data/rates.png")
456475

457476

458477
@bot.router.message(text_message=["4", "Call a support operator"])
459478
def call_support_operator_handler(notification: Notification) -> None:
460479
notification.answer("Good. A tech support operator will contact you soon.")
461480

481+
@bot.router.message(text_message=["5", "Show interactive buttons"])
482+
def show_interactive_buttons_handler(notification: Notification) -> None:
483+
notification.answer_with_interactive_buttons(
484+
"This message contains interactive buttons",
485+
[{
486+
"type": "call",
487+
"buttonId": "1",
488+
"buttonText": "Call me",
489+
"phoneNumber": "79123456789"
490+
},
491+
{
492+
"type": "url",
493+
"buttonId": "2",
494+
"buttonText": "Green-api",
495+
"url": "https://green-api.com"
496+
}],
497+
"Hello!",
498+
"Hope you like it!"
499+
)
500+
501+
@bot.router.message(text_message=["6", "Show interactive reply buttons"])
502+
def show_interactive_buttons_reply_handler(notification: Notification) -> None:
503+
notification.answer_with_interactive_buttons_reply(
504+
"This message contains interactive reply buttons",
505+
[{
506+
"buttonId": "1",
507+
"buttonText": "First Button"
508+
},
509+
{
510+
"buttonId": "2",
511+
"buttonText": "Second Button"
512+
}],
513+
"Hello!",
514+
"Hope you like it!"
515+
)
462516

463517
bot.run_forever()
464518
```

docs/README.md

Lines changed: 75 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -215,32 +215,47 @@ def message_handler(notification: Notification) -> None:
215215
bot.run_forever()
216216
```
217217

218-
### Как обрабатывать кнопки (неактуально)
219-
220-
Метод временно не работает. При вызове метода вернется ошибка 403.
218+
### Как обрабатывать кнопки
221219

222220
Чтобы получать уведомления о нажатиях на кнопку, нужно использовать объект `bot.router.buttons`.
223221

224-
Ссылка на пример: [buttons.py](../examples/buttons.py).
222+
Ссылка на пример: [interactive_buttons.py](../examples/interactive_buttons.py).
225223

226224
```
227-
@bot.router.buttons()
228-
def buttons_handler(notification: Notification) -> None:
229-
notification.answer_buttons("Choose a color", [
230-
{
231-
"buttonId": 1,
232-
"buttonText": "Red"
225+
@bot.router.message()
226+
227+
def show_interactive_buttons_handler(notification: Notification) -> None:
228+
notification.answer_with_interactive_buttons(
229+
"This message contains interactive buttons",
230+
[{
231+
"type": "call",
232+
"buttonId": "1",
233+
"buttonText": "Call me",
234+
"phoneNumber": "79123456789"
233235
},
234236
{
235-
"buttonId": 2,
236-
"buttonText": "Green"
237+
"type": "url",
238+
"buttonId": "2",
239+
"buttonText": "Green-api",
240+
"url": "https://green-api.com"
241+
}],
242+
"Hello!",
243+
"Hope you like it!"
244+
)
245+
246+
notification.answer_with_interactive_buttons_reply(
247+
"This message contains interactive reply buttons",
248+
[{
249+
"buttonId": "1",
250+
"buttonText": "First Button"
237251
},
238252
{
239-
"buttonId": 3,
240-
"buttonText": "Blue"
241-
}
242-
])
243-
253+
"buttonId": "2",
254+
"buttonText": "Second Button"
255+
}],
256+
"Hello!",
257+
"Hope you like it!"
258+
)
244259
245260
bot.run_forever()
246261
```
@@ -398,10 +413,14 @@ def message_handler(notification: Notification) -> None:
398413
- 2 или Show office address (бот отправит адрес офиса в виде карты)
399414
- 3 или Show available rates (бот отправит картинку с тарифами)
400415
- 4 или Call a support operator (бот отправит текстовое сообщение)
416+
- 5 или Show interactive buttons (бот отправит интерактивные кнопки)
417+
- 6 или Show interactive reply buttons (бот отправит интерактивные кнопки с ответом)
401418

402419
Чтобы отправить текстовое сообщение, нужно использовать метод `notification.answer`.
403420
Чтобы отправить место (локацию), нужно использовать метод `sending.sendLocation` из `notification.api`.
404421
Чтобы отправить сообщение с файлом, нужно использовать метод `notification.answer_with_file`.
422+
Чтобы отправить сообщение с интерактивными кнопками, нужно использовать метод `notification.answer_with_interactive_buttons`.
423+
Чтобы отправить сообщение с интерактивными кнопками с ответом, нужно использовать метод `notification.answer_with_interactive_buttons_reply`.
405424

406425
В этом примере бот отвечает только на команды из списка выше.
407426

@@ -414,7 +433,6 @@ bot = GreenAPIBot(
414433
"1101000001", "d75b3a66374942c5b3c019c698abc2067e151558acbd412345"
415434
)
416435

417-
418436
@bot.router.message(command="start")
419437
def message_handler(notification: Notification) -> None:
420438
sender_data = notification.event["senderData"]
@@ -426,19 +444,19 @@ def message_handler(notification: Notification) -> None:
426444
"1. Report a problem\n"
427445
"2. Show office address\n"
428446
"3. Show available rates\n"
429-
"4. Call a support operator\n\n"
447+
"4. Call a support operator\n"
448+
"5. Show interactive buttons\n"
449+
"6. Show interactive reply buttons\n\n"
430450
"Choose a number and send to me."
431451
)
432452
)
433453

434-
435454
@bot.router.message(text_message=["1", "Report a problem"])
436455
def report_problem_handler(notification: Notification) -> None:
437456
notification.answer(
438457
"https://github.com/green-api/issues/issues/new", link_preview=False
439458
)
440459

441-
442460
@bot.router.message(text_message=["2", "Show office address"])
443461
def show_office_address_handler(notification: Notification) -> None:
444462
chat = notification.chat
@@ -447,16 +465,50 @@ def show_office_address_handler(notification: Notification) -> None:
447465
chatId=chat, latitude=55.7522200, longitude=37.6155600
448466
)
449467

450-
451468
@bot.router.message(text_message=["3", "Show available rates"])
452469
def show_available_rates_handler(notification: Notification) -> None:
453-
notification.answer_with_file("data/rates.png")
470+
notification.answer_with_file("examples/data/rates.png")
454471

455472

456473
@bot.router.message(text_message=["4", "Call a support operator"])
457474
def call_support_operator_handler(notification: Notification) -> None:
458475
notification.answer("Good. A tech support operator will contact you soon.")
459476

477+
@bot.router.message(text_message=["5", "Show interactive buttons"])
478+
def show_interactive_buttons_handler(notification: Notification) -> None:
479+
notification.answer_with_interactive_buttons(
480+
"This message contains interactive buttons",
481+
[{
482+
"type": "call",
483+
"buttonId": "1",
484+
"buttonText": "Call me",
485+
"phoneNumber": "79123456789"
486+
},
487+
{
488+
"type": "url",
489+
"buttonId": "2",
490+
"buttonText": "Green-api",
491+
"url": "https://green-api.com"
492+
}],
493+
"Hello!",
494+
"Hope you like it!"
495+
)
496+
497+
@bot.router.message(text_message=["6", "Show interactive reply buttons"])
498+
def show_interactive_buttons_reply_handler(notification: Notification) -> None:
499+
notification.answer_with_interactive_buttons_reply(
500+
"This message contains interactive reply buttons",
501+
[{
502+
"buttonId": "1",
503+
"buttonText": "First Button"
504+
},
505+
{
506+
"buttonId": "2",
507+
"buttonText": "Second Button"
508+
}],
509+
"Hello!",
510+
"Hope you like it!"
511+
)
460512

461513
bot.run_forever()
462514
```

0 commit comments

Comments
 (0)