Skip to content

Commit 7acb4c7

Browse files
committed
✨ Enhance webhook handling:
- Refactor chat ID processing to support multiple IDs using placeholders {chat_id} and {chat_id_2} 🆔🔄 - Add new fields for manual message sending in the form, including chat ID templates and additional placeholders 📋✉️ - Improve error handling for missing chat IDs and webhook selection, ensuring better user guidance 🚫❓ - Update templates to reflect new fields and improve user experience in chat initiation from webhooks 💬🚀
1 parent 9f70ac4 commit 7acb4c7

4 files changed

Lines changed: 141 additions & 21 deletions

File tree

lhc_web/design/defaulttheme/tpl/lhwebhooks/form_incoming.tpl.php

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,6 @@
218218
</div>
219219
</div>
220220
</div>
221-
<div class="col-6">
222-
<div class="form-group">
223-
<label><?php echo erTranslationClassLhTranslation::getInstance()->getTranslation('chat/webhooks','Chat ID Template for manual message sending. Use {chat_id} as placeholder');?></label>
224-
<input type="text" class="form-control form-control-sm" title="{chat_id}@c.us" placeholder="E.g {chat_id}@c.us" ng-model="webhookincomingsctl.conditions.chat_id_template" value="" />
225-
</div>
226-
</div>
227221
<div class="col-6">
228222
<div class="row">
229223
<div class="col-6">
@@ -240,7 +234,6 @@
240234
</div>
241235
</div>
242236
</div>
243-
244237
</div>
245238

246239
<div class="row">
@@ -264,6 +257,50 @@
264257
</div>
265258
</div>
266259

260+
<hr>
261+
<h4><?php echo erTranslationClassLhTranslation::getInstance()->getTranslation('chat/webhooks','Manual push message')?></h4>
262+
<div class="row">
263+
<div class="col-6">
264+
<div class="form-group">
265+
<label><?php echo erTranslationClassLhTranslation::getInstance()->getTranslation('chat/webhooks','Chat ID Template for manual message sending. Use {chat_id} and {chat_id_2} as placeholder');?></label>
266+
<input type="text" class="form-control form-control-sm" title="{chat_id}@c.us" placeholder="E.g {chat_id}@c.us or {chat_id}__{chat_id_2}" ng-model="webhookincomingsctl.conditions.chat_id_template" value="" />
267+
</div>
268+
</div>
269+
270+
<div class="col-6">
271+
<div class="form-group">
272+
<label><?php echo erTranslationClassLhTranslation::getInstance()->getTranslation('chat/webhooks','Additional field placeholder.');?></label>
273+
<input type="text" class="form-control form-control-sm" placeholder="E.g {chat_id_2}" ng-model="webhookincomingsctl.conditions.add_field_value_placeholder" value="" />
274+
</div>
275+
</div>
276+
277+
<div class="col-6">
278+
<div class="form-group">
279+
<label><?php echo erTranslationClassLhTranslation::getInstance()->getTranslation('chat/webhooks','Chat ID name for our own purpose');?></label>
280+
<input type="text" class="form-control form-control-sm" title="{chat_id}@c.us" placeholder="E.g Recipient phone" ng-model="webhookincomingsctl.conditions.chat_id_name" value="" />
281+
</div>
282+
</div>
283+
284+
<div class="col-6">
285+
<div class="form-group">
286+
<label><?php echo erTranslationClassLhTranslation::getInstance()->getTranslation('chat/webhooks','Chat ID 2 name for our own purpose');?></label>
287+
<input type="text" class="form-control form-control-sm" placeholder="E.g {chat_id_2}" ng-model="webhookincomingsctl.conditions.chat_id_2_name" value="" />
288+
</div>
289+
</div>
290+
<div class="col-6">
291+
<div class="form-group">
292+
<label><?php echo erTranslationClassLhTranslation::getInstance()->getTranslation('chat/webhooks','Dataset list for chat_id');?></label>
293+
<textarea placeholder="One value per row" class="form-control" ng-model="webhookincomingsctl.conditions.chat_id_list" rows="5"></textarea>
294+
</div>
295+
</div>
296+
<div class="col-6">
297+
<div class="form-group">
298+
<label><?php echo erTranslationClassLhTranslation::getInstance()->getTranslation('chat/webhooks','Dataset list for chat_id_2');?></label>
299+
<textarea placeholder="One value per row" class="form-control" ng-model="webhookincomingsctl.conditions.chat_id_2_list" rows="5"></textarea>
300+
</div>
301+
</div>
302+
</div>
303+
267304
</div>
268305

269306
<div role="tabpanel" class="tab-pane form-group" id="text_messages">

lhc_web/design/defaulttheme/tpl/lhwebhooks/pushchat.tpl.php

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
<?php endif; ?>
1010

1111
<?php if (isset($chat)) : ?>
12-
<a title="<?php echo erTranslationClassLhTranslation::getInstance()->getTranslation('webhook/push', 'Open in a new window'); ?>" ng-click="lhc.startChatNewWindow(<?php echo $chat->id?>,'<?php echo erLhcoreClassDesign::shrt($chat->nick,10,'...',30,ENT_QUOTES);?>')"><?php echo erTranslationClassLhTranslation::getInstance()->getTranslation('webhook/push','Open chat')?></a>
12+
<a href="#/chat-id-<?php echo $chat->id?>" class="action-image" data-title="<?php echo erLhcoreClassDesign::shrt($chat->nick,10,'...',30,ENT_QUOTES);?>" onclick="lhinst.startChatNewWindow(<?php echo $chat->id?>,$(this).attr('data-title'))" title="<?php echo erTranslationClassLhTranslation::getInstance()->getTranslation('webhook/push','Open chat')?>"><?php echo erTranslationClassLhTranslation::getInstance()->getTranslation('webhook/push','Open chat')?></a>
1313
<?php endif; ?>
1414

1515
<p><?php echo erTranslationClassLhTranslation::getInstance()->getTranslation('webhook/push', 'You will initiate chat as it was response to incoming webhook.'); ?></p>
1616

17-
<form action="" method="post" ng-non-bindable>
17+
<form action="?webhook_id=<?php echo (int)$item->incoming_api_id?>" method="post" ng-non-bindable>
1818

1919
<?php include(erLhcoreClassDesign::designtpl('lhkernel/csfr_token.tpl.php'));?>
2020

@@ -26,14 +26,48 @@
2626
'css_class' => 'form-control form-control-sm',
2727
'optional_field' => erTranslationClassLhTranslation::getInstance()->getTranslation('webhook/push','Choose a webhook'),
2828
'selected_id' => $item->incoming_api_id,
29+
'on_change' => "if(this.value>0){window.location.href='" . erLhcoreClassDesign::baseurl('webhooks/pushchat') . "?webhook_id='+this.value}",
2930
'list_function' => 'erLhcoreClassModelChatIncomingWebhook::getList',
3031
'list_function_params' => array('limit' => false, 'sort' => '`name` ASC','filter' => array('disabled' => 0))
3132
) ); ?>
3233
</div>
3334

34-
<div class="form-group">
35-
<label><?php echo erTranslationClassLhTranslation::getInstance()->getTranslation('webhook/push', 'Recipient. (chatId). In most cases it is just a phone number'); ?></label>
36-
<input type="text" class="form-control form-control-sm" value="<?php echo htmlspecialchars($item->chat_id)?>" name="chat_id">
35+
<?php if ($webhook_loaded) : ?>
36+
37+
<?php
38+
$chatIdLabel = !empty($webhook_conditions['chat_id_name']) ? htmlspecialchars($webhook_conditions['chat_id_name']) : erTranslationClassLhTranslation::getInstance()->getTranslation('webhook/push', 'Chat ID') . ' (chat_id)';
39+
$chatId2Label = !empty($webhook_conditions['chat_id_2_name']) ? htmlspecialchars($webhook_conditions['chat_id_2_name']) : erTranslationClassLhTranslation::getInstance()->getTranslation('webhook/push', 'Chat ID 2') . ' (chat_id_2)';
40+
$chatIdList = !empty($webhook_conditions['chat_id_list']) ? array_filter(array_map('trim', explode("\n", $webhook_conditions['chat_id_list']))) : [];
41+
$chatId2List = !empty($webhook_conditions['chat_id_2_list']) ? array_filter(array_map('trim', explode("\n", $webhook_conditions['chat_id_2_list']))) : [];
42+
?>
43+
44+
<div class="row">
45+
<div class="col-6">
46+
<div class="form-group">
47+
<label><?php echo $chatIdLabel ?></label>
48+
<input type="text" class="form-control form-control-sm" value="<?php echo htmlspecialchars($item->chat_id)?>" name="chat_id"<?php echo !empty($chatIdList) ? ' list="datalist_chat_id"' : ''?>>
49+
<?php if (!empty($chatIdList)) : ?>
50+
<datalist id="datalist_chat_id">
51+
<?php foreach ($chatIdList as $chatIdOption) : ?>
52+
<option value="<?php echo htmlspecialchars($chatIdOption)?>">
53+
<?php endforeach; ?>
54+
</datalist>
55+
<?php endif; ?>
56+
</div>
57+
</div>
58+
<div class="col-6">
59+
<div class="form-group">
60+
<label><?php echo $chatId2Label ?></label>
61+
<input type="text" class="form-control form-control-sm" value="<?php echo htmlspecialchars($item->chat_id_2)?>" name="chat_id_2"<?php echo !empty($chatId2List) ? ' list="datalist_chat_id_2"' : ''?>>
62+
<?php if (!empty($chatId2List)) : ?>
63+
<datalist id="datalist_chat_id_2">
64+
<?php foreach ($chatId2List as $chatId2Option) : ?>
65+
<option value="<?php echo htmlspecialchars($chatId2Option)?>">
66+
<?php endforeach; ?>
67+
</datalist>
68+
<?php endif; ?>
69+
</div>
70+
</div>
3771
</div>
3872

3973
<div class="form-group">
@@ -63,4 +97,6 @@
6397

6498
<input type="submit" name="Update" class="btn btn-primary" value="<?php echo erTranslationClassLhTranslation::getInstance()->getTranslation('webhook/push','Send')?>">
6599

100+
<?php endif; ?>
101+
66102
</form>

lhc_web/lib/core/lhchat/lhchatwebhookincomming.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2679,11 +2679,11 @@ public static function sendMessage($incomingWebhook, $item) {
26792679
try {
26802680
$db->beginTransaction();
26812681

2682-
$incomingChat = erLhcoreClassModelChatIncoming::findOne(array('filter' => array('chat_external_id' => $item->chat_id)));
2682+
$incomingChat = erLhcoreClassModelChatIncoming::findOne(array('filter' => array('chat_external_id' => str_replace(['{chat_id}','{chat_id_2}'], [$item->chat_id, $item->chat_id_2], $incomingWebhook->conditions_array['chat_id_template']))));
26832683

26842684
if (!($incomingChat instanceof erLhcoreClassModelChatIncoming)) {
26852685
$incomingChat = new erLhcoreClassModelChatIncoming();
2686-
$incomingChat->chat_external_id = str_replace('{chat_id}', $item->chat_id, $incomingWebhook->conditions_array['chat_id_template']);
2686+
$incomingChat->chat_external_id = str_replace(['{chat_id}','{chat_id_2}'], [$item->chat_id, $item->chat_id_2], $incomingWebhook->conditions_array['chat_id_template']);
26872687
$incomingChat->incoming_id = $incomingWebhook->id;
26882688
$incomingChat->utime = time();
26892689
$incomingChat->incoming = $incomingWebhook;
@@ -2708,6 +2708,14 @@ public static function sendMessage($incomingWebhook, $item) {
27082708
$chat->session_referrer = '';
27092709
$chat->iwh_id = $incomingWebhook->id;
27102710

2711+
if (isset($incomingWebhook->conditions_array['add_field_value_placeholder']) && !empty($incomingWebhook->conditions_array['add_field_value_placeholder'])){
2712+
$chatVariables = [];
2713+
$chatVariables['iwh_field'] = str_replace(['{chat_id}','{chat_id_2}'], [$item->chat_id, $item->chat_id_2], $incomingWebhook->conditions_array['add_field_value_placeholder']);
2714+
if (!empty($chatVariables)) {
2715+
$chat->chat_variables = json_encode($chatVariables);
2716+
}
2717+
}
2718+
27112719
$msg = new erLhcoreClassModelmsg();
27122720
$msg->msg = $item->message;
27132721

lhc_web/modules/lhwebhooks/pushchat.php

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@
55
$item = new stdClass();
66
$item->incoming_api_id = 0;
77
$item->chat_id = '';
8+
$item->chat_id_2 = '';
89
$item->message = '';
910
$item->dep_id = 0;
1011
$item->create_chat = false;
1112
$item->close_chat = false;
1213

14+
// Pre-select webhook from GET param
15+
$webhookId = filter_input(INPUT_GET, 'webhook_id', FILTER_VALIDATE_INT);
16+
if ($webhookId > 0) {
17+
$item->incoming_api_id = $webhookId;
18+
}
19+
1320
/**
1421
* Has post data
1522
*/
@@ -24,6 +31,7 @@
2431
'incoming_api_id' => new ezcInputFormDefinitionElement(ezcInputFormDefinitionElement::OPTIONAL, 'int', ['min_range' => 1]),
2532
'message' => new ezcInputFormDefinitionElement(ezcInputFormDefinitionElement::OPTIONAL, 'unsafe_raw', null),
2633
'chat_id' => new ezcInputFormDefinitionElement(ezcInputFormDefinitionElement::OPTIONAL, 'unsafe_raw', null),
34+
'chat_id_2' => new ezcInputFormDefinitionElement(ezcInputFormDefinitionElement::OPTIONAL, 'unsafe_raw', null),
2735
'create_chat' => new ezcInputFormDefinitionElement(ezcInputFormDefinitionElement::OPTIONAL, 'boolean', null),
2836
'close_chat' => new ezcInputFormDefinitionElement(ezcInputFormDefinitionElement::OPTIONAL, 'boolean', null),
2937
'dep_id' => new ezcInputFormDefinitionElement(ezcInputFormDefinitionElement::OPTIONAL, 'int', ['min_range' => 1]),
@@ -32,10 +40,26 @@
3240
$Errors = [];
3341
$form = new ezcInputForm(INPUT_POST, $definition);
3442

43+
$webhookItem = null;
44+
if ($form->hasValidData('incoming_api_id')) {
45+
$item->incoming_api_id = $form->incoming_api_id;
46+
try {
47+
$webhookItem = erLhcoreClassModelChatIncomingWebhook::fetch($item->incoming_api_id);
48+
} catch (Exception $e) {}
49+
} else {
50+
$Errors[] = erTranslationClassLhTranslation::getInstance()->getTranslation('twilio/sendmessage','Please choose a webhook!');
51+
}
52+
3553
if ($form->hasValidData('chat_id') && !empty($form->chat_id)) {
3654
$item->chat_id = $form->chat_id;
3755
} else {
38-
$Errors[] = erTranslationClassLhTranslation::getInstance()->getTranslation('twilio/sendmessage','Please enter chatId!');
56+
$Errors[] = erTranslationClassLhTranslation::getInstance()->getTranslation('twilio/sendmessage','Please enter Chat ID 1!');
57+
}
58+
59+
if ($form->hasValidData('chat_id_2') && !empty($form->chat_id_2)) {
60+
$item->chat_id_2 = $form->chat_id_2;
61+
} elseif ($webhookItem !== null && str_contains($webhookItem->conditions_array['chat_id_template'] ?? '', '{chat_id_2}')) {
62+
$Errors[] = erTranslationClassLhTranslation::getInstance()->getTranslation('twilio/sendmessage','Please enter Chat ID 2!');
3963
}
4064

4165
if ($form->hasValidData('message') && !empty($form->message)) {
@@ -57,12 +81,6 @@
5781
$item->close_chat = false;
5882
}
5983

60-
if ($form->hasValidData('incoming_api_id')) {
61-
$item->incoming_api_id = $form->incoming_api_id;
62-
} else {
63-
$Errors[] = erTranslationClassLhTranslation::getInstance()->getTranslation('twilio/sendmessage','Please choose a webhook!');
64-
}
65-
6684
// Set department
6785
if ($form->hasValidData('dep_id')) {
6886
$item->dep_id = $form->dep_id;
@@ -95,6 +113,27 @@
95113

96114
$tpl->set('item', $item);
97115

116+
// Load webhook configuration for labels/datalists
117+
$webhookConditions = [];
118+
$webhookLoaded = false;
119+
if ($item->incoming_api_id > 0) {
120+
try {
121+
$webhookItem = erLhcoreClassModelChatIncomingWebhook::fetch($item->incoming_api_id);
122+
if ($item->dep_id == 0) {
123+
$item->dep_id = $webhookItem->dep_id;
124+
}
125+
if ($webhookItem instanceof erLhcoreClassModelChatIncomingWebhook) {
126+
$webhookConditions = $webhookItem->conditions_array;
127+
$webhookLoaded = true;
128+
}
129+
} catch (Exception $e) {
130+
// webhook not found
131+
}
132+
}
133+
$tpl->set('webhook_conditions', $webhookConditions);
134+
$tpl->set('webhook_loaded', $webhookLoaded);
135+
136+
98137
$Result['content'] = $tpl->fetch();
99138
$Result['path'] = [
100139
['url' => erLhcoreClassDesign::baseurl('system/configuration'), 'title' => erTranslationClassLhTranslation::getInstance()->getTranslation('webhooks/module', 'System configuration')],

0 commit comments

Comments
 (0)