1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Conversations; |
4
|
|
|
|
5
|
|
|
use App\Enum\Gender; |
6
|
|
|
use BotMan\BotMan\Messages\Incoming\Answer; |
7
|
|
|
use BotMan\Drivers\Telegram\Extensions\Keyboard; |
8
|
|
|
use BotMan\Drivers\Telegram\Extensions\KeyboardButton; |
9
|
|
|
use Illuminate\Support\Arr; |
10
|
|
|
use Illuminate\Support\Collection; |
11
|
|
|
use Illuminate\Support\Str; |
12
|
|
|
|
13
|
|
|
class HomeConservation extends Conversation |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Start the conversation. |
17
|
|
|
* |
18
|
|
|
* @return void |
19
|
|
|
*/ |
20
|
|
|
public function run() |
21
|
|
|
{ |
22
|
|
|
$title = Str::ucfirst(Gender::title($this->getUserStorage('gender'))); |
23
|
|
|
$name = Str::ucfirst($this->getUser()->getFirstName()); |
24
|
|
|
|
25
|
|
|
$keyboard = Keyboard::create(Keyboard::TYPE_INLINE)->resizeKeyboard(); |
26
|
|
|
|
27
|
|
|
foreach (static::conversations(withoutStartCommand: true) as $conversation) { |
28
|
|
|
$commands = Arr::wrap($conversation['command']); |
29
|
|
|
|
30
|
|
|
$keyboard->addRow(KeyboardButton::create(Arr::last($commands)) |
31
|
|
|
->callbackData(Arr::first($commands)) |
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
$response = $this->reply( |
36
|
|
|
$question = view('conversations.home.confirm-menu', compact('title', 'name'))->render(), |
|
|
|
|
37
|
|
|
$additionalParameters = $keyboard->toArray() |
38
|
|
|
); |
39
|
|
|
|
40
|
|
|
return $this->getBot()->storeConversation($this, next: function (Answer $answer) use ($response) { |
|
|
|
|
41
|
|
|
if (!$answer->isInteractiveMessageReply()) { |
42
|
|
|
return; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->deleteTelegramMessageFromResponse($response); |
46
|
|
|
|
47
|
|
|
if (!$conversation = $this->getConversation($answer->getValue())) { |
48
|
|
|
return $this->sayFallbackMessage($answer->getText()); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return $this->startConversation($conversation); |
|
|
|
|
52
|
|
|
}, question: $question, additionalParameters: $additionalParameters); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Return specific conversation instance based on the given name. |
57
|
|
|
* |
58
|
|
|
* @param string $name |
59
|
|
|
* @return \BotMan\BotMan\Messages\Conversations\Conversation|null |
60
|
|
|
*/ |
61
|
|
|
protected function getConversation(string $name): ?Conversation |
62
|
|
|
{ |
63
|
|
|
if (!$conversation = static::conversations(withoutStartCommand: true)->first(fn ($conversation) => |
64
|
|
|
in_array($name, Arr::wrap($conversation['command'])) |
65
|
|
|
)) { |
66
|
|
|
return null; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return value($conversation['handler']); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Return list of command and its conversation handler class. |
74
|
|
|
* |
75
|
|
|
* @param bool $withoutStartCommand |
76
|
|
|
* @return \Illuminate\Support\Collection<array> |
77
|
|
|
*/ |
78
|
|
|
public static function conversations(bool $withoutStartCommand = false): Collection |
79
|
|
|
{ |
80
|
|
|
return collect([ |
|
|
|
|
81
|
|
|
[ |
82
|
|
|
'command' => 'start', |
83
|
|
|
'handler' => fn () => new StartConservation, |
84
|
|
|
'description' => 'Memulai percakapan', |
85
|
|
|
], |
86
|
|
|
[ |
87
|
|
|
'command' => [ |
88
|
|
|
'exchange', |
89
|
|
|
view('conversations.home.reply-menu-exchange')->render(), |
90
|
|
|
], |
91
|
|
|
'handler' => fn () => new ExchangeConversation, |
92
|
|
|
'description' => 'Melakukan transaksi penukaran uang', |
93
|
|
|
], |
94
|
|
|
[ |
95
|
|
|
'command' => [ |
96
|
|
|
'help', |
97
|
|
|
view('conversations.home.reply-menu-help')->render(), |
98
|
|
|
], |
99
|
|
|
'handler' => fn () => new HelpConversation, |
100
|
|
|
'description' => 'Panduan cara menggunakan chatbot ini', |
101
|
|
|
], |
102
|
|
|
[ |
103
|
|
|
'command' => [ |
104
|
|
|
'login', |
105
|
|
|
view('conversations.home.reply-menu-login')->render(), |
106
|
|
|
], |
107
|
|
|
'handler' => fn () => new LoginConversation, |
108
|
|
|
'description' => 'Mendaftarkan Chat ID Telegram pada akun (khusus admin dan staf)', |
109
|
|
|
], |
110
|
|
|
[ |
111
|
|
|
'command' => [ |
112
|
|
|
'logout', |
113
|
|
|
view('conversations.home.reply-menu-logout')->render(), |
114
|
|
|
], |
115
|
|
|
'handler' => fn () => new LogoutConversation, |
116
|
|
|
'description' => 'Menghapus Chat ID Telegram pada akun (khusus admin dan staf)', |
117
|
|
|
], |
118
|
|
|
[ |
119
|
|
|
'command' => [ |
120
|
|
|
'ask_telegram_chat_id', |
121
|
|
|
view('conversations.home.reply-menu-ask_telegram_chat_id')->render(), |
122
|
|
|
], |
123
|
|
|
'handler' => fn () => new AskTelegramChatIdConversation, |
124
|
|
|
'description' => 'Mengetahui Chat ID Telegram anda', |
125
|
|
|
], |
126
|
|
|
[ |
127
|
|
|
'command' => [ |
128
|
|
|
'check_order_status', |
129
|
|
|
view('conversations.home.reply-menu-check_order_status')->render(), |
130
|
|
|
], |
131
|
|
|
'handler' => fn () => new CheckOrderStatusConversation, |
132
|
|
|
'description' => 'Mengetahui status transaksi penukaran uang anda', |
133
|
|
|
], |
134
|
|
|
])->when($withoutStartCommand, fn (Collection $conversations) => |
135
|
|
|
$conversations->reject(fn ($conversation) => |
136
|
|
|
in_array('start', Arr::wrap($conversation['command'])) |
137
|
|
|
) |
138
|
|
|
); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|