This repository has been archived by the owner on May 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
emulate.php
84 lines (70 loc) · 2.48 KB
/
emulate.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
require __DIR__ . '/vendor/autoload.php';
use Balfour\WhatsApp\Bot\Actions\SendMessageAction;
use Balfour\WhatsApp\Bot\Actions\SendAndActivateMenuAction;
use Balfour\WhatsApp\Bot\Bot;
use Balfour\WhatsApp\Bot\Emulator;
use Balfour\WhatsApp\Bot\Menu\InMemoryMenuStateStore;
use Balfour\WhatsApp\Bot\Menu\Menu;
use Balfour\WhatsApp\Bot\Menu\Option;
use Balfour\WhatsApp\Bot\Middleware\ProcessMenuOption;
use Balfour\WhatsApp\Bot\Middleware\ProcessTrigger;
use Balfour\WhatsApp\Bot\Triggers\StringTrigger;
use Balfour\WhatsApp\Bot\Triggers\TriggerRegistry;
// create menus
$menuStateStore = new InMemoryMenuStateStore();
// first, build parent menu without any options
$root = new Menu(
"Hello! I'm your friendly WhatsApp bot.",
"You can press # to return to the main menu, or 'quit' to exit the menu."
);
// now, create weather menu, linking it to parent menu
$weatherOptions = [
new Option(
'1',
'Cape Town, South Africa',
new SendMessageAction('The weather in Cape Town, South Africa is 14°C.')
),
new Option(
'2',
'Johannesburg, South Africa',
new SendMessageAction('The weather in Johannesburg, South Africa is 9°C.')
),
];
$weatherMenu = new Menu(
"Please select your city.",
"You can press # to return to the main menu, @ to return to the previous menu or 'quit' to exit the menu.",
$weatherOptions,
$root
);
// add options to parent menu
$root->addOptions([
new Option(
'1',
'Check the weather',
new SendAndActivateMenuAction($weatherMenu, $menuStateStore)
),
new Option(
'2',
'Fetch my available balance',
new SendMessageAction('Your available balance is $10.00.')
),
]);
// create triggers
$triggers = new TriggerRegistry();
// when we get 'ping', we'll respond 'pong'
$triggers->register(new StringTrigger('ping', new SendMessageAction('pong!')));
// we'll want to bring up our root menu when the user types "menu"
$triggers->register(new StringTrigger('menu', new SendAndActivateMenuAction($root, $menuStateStore)));
// create bot with middleware loaded
$middleware = [
new ProcessTrigger($triggers),
new ProcessMenuOption($root, $menuStateStore)
];
$bot = new Bot($middleware);
// create & run emulator
$emulator = new Emulator($bot);
$emulator->run();
// if we were running this in the real world, we'll want to process any inbound message through the bot
// eg: when a message is received (via polling or web hook)
// $bot->process($message);