|
| 1 | +from whatsapp_chatbot_python import GreenAPIBot, Notification |
| 2 | + |
| 3 | +bot = GreenAPIBot( |
| 4 | + "1101000001", "d75b3a66374942c5b3c019c698abc2067e151558acbd412345" |
| 5 | +) |
| 6 | + |
| 7 | + |
| 8 | +@bot.router.message(command="start") |
| 9 | +def message_handler(notification: Notification) -> None: |
| 10 | + sender_data = notification.event["senderData"] |
| 11 | + sender_name = sender_data["senderName"] |
| 12 | + |
| 13 | + response = notification.answer_with_poll( |
| 14 | + f"Hello, {sender_name}. Here's what I can do:\n\n", |
| 15 | + [ |
| 16 | + {"optionName": "1. Report a problem"}, |
| 17 | + {"optionName": "2. Show office address"}, |
| 18 | + {"optionName": "3. Show available rates"}, |
| 19 | + {"optionName": "4. Call a support operator"} |
| 20 | + ] |
| 21 | + ) |
| 22 | + |
| 23 | + stanza = response.data["idMessage"] |
| 24 | + |
| 25 | + bot.router.poll_update_message.add_handler_with_stanza( |
| 26 | + start_poll_handler, stanza |
| 27 | + ) |
| 28 | + |
| 29 | + |
| 30 | +def start_poll_handler(notification: Notification) -> None: |
| 31 | + votes = notification.event["messageData"]["pollMessageData"]["votes"] |
| 32 | + for vote_data in votes: |
| 33 | + voters = vote_data["optionVoters"] |
| 34 | + if voters: |
| 35 | + option_name = vote_data["optionName"] |
| 36 | + if option_name == "1. Report a problem": |
| 37 | + link = "https://github.com/green-api/issues/issues/new" |
| 38 | + |
| 39 | + notification.answer(link, link_preview=False) |
| 40 | + elif option_name == "2. Show office address": |
| 41 | + notification.api.sending.sendLocation( |
| 42 | + notification.chat, 55.7522200, 37.6155600 |
| 43 | + ) |
| 44 | + elif option_name == "3. Show available rates": |
| 45 | + notification.answer_with_file("data/rates.png") |
| 46 | + elif option_name == "4. Call a support operator": |
| 47 | + notification.answer( |
| 48 | + "Good. A tech support operator will contact you soon." |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +bot.run_forever() |
0 commit comments