Skip to content

Commit a29c8f1

Browse files
committed
✨ Add separate "Test chat" and "Test mail" webhook buttons and debug condition output
- Webhook form: split the single "Test" button into "Test chat" and "Test mail", update the input placeholder to "Chat ID or mail message ID" and drop the type-change placeholder swapping in favor of explicit actions - isValidConditions(): add debug mode that collects per-condition evaluation details (attr, values, operator, VALID/INVALID) into triggerNameDebug so admins can see why a condition matched or failed - testpattern: route webhook tests by type - continuous mail events use the Continous::processEventMail debug flow, hook events validate via isValidConditions() and report a ✔️/❌ result for the tested mail - parseDeliveryStatus(): rewrite header parsing to unfold continuation lines and parse line by line, fixing multi line headers mangling the raw headers - Drop fragile preg_replace hacks when populating headers_raw_array since the parser now handles line endings itself
1 parent 7f40783 commit a29c8f1

4 files changed

Lines changed: 46 additions & 19 deletions

File tree

‎lhc_web/design/defaulttheme/tpl/lhwebhooks/form.tpl.php‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,27 +90,26 @@
9090

9191
<?php if (is_numeric($item->id)) : ?>
9292
<?php if (erLhcoreClassUser::instance()->hasAccessTo('lhgenericbot','test_pattern')) : ?>
93-
<input type="text" class="form-control form-control-sm" id="test-chat-id" name="chat_id_test" value="<?php isset($_POST['chat_id_test']) ? print (int)$_POST['chat_id_test'] : '';?>" placeholder="<?php echo erTranslationClassLhTranslation::getInstance()->getTranslation('bot/conditions','Chat ID');?>" value="">
94-
<button type="button" id="check-against-chat" class="btn btn-sm btn-secondary" title="<?php echo erTranslationClassLhTranslation::getInstance()->getTranslation('bot/conditions','Make sure to save condition first.');?>"><?php echo erTranslationClassLhTranslation::getInstance()->getTranslation('bot/conditions','Test');?></button>
93+
<input type="text" class="form-control form-control-sm" id="test-chat-id" name="chat_id_test" value="<?php isset($_POST['chat_id_test']) ? print (int)$_POST['chat_id_test'] : '';?>" placeholder="<?php echo erTranslationClassLhTranslation::getInstance()->getTranslation('chat/webhooks','Chat ID or mail message ID');?>" value="">
94+
<button type="button" id="check-against-chat" class="btn btn-sm btn-secondary" title="<?php echo erTranslationClassLhTranslation::getInstance()->getTranslation('bot/conditions','Make sure to save condition first.');?>"><?php echo erTranslationClassLhTranslation::getInstance()->getTranslation('chat/webhooks','Test chat');?></button>
95+
<button type="button" id="check-against-mail" class="btn btn-sm btn-secondary" title="<?php echo erTranslationClassLhTranslation::getInstance()->getTranslation('bot/conditions','Make sure to save condition first.');?>"><?php echo erTranslationClassLhTranslation::getInstance()->getTranslation('chat/webhooks','Test mail');?></button>
9596
<?php endif; ?>
9697
<?php endif; ?>
9798
</div>
9899
</div>
99100

100101
<?php if (is_numeric($item->id)) : ?>
101102
<script>
102-
$('#check-against-chat').click(function(){
103-
var webhookType = $('input[name="type"]:checked').val();
104-
var payload = {'webhook_id' : <?php echo $item->id?>};
105-
if (webhookType == '2') {
106-
payload['mail'] = 1;
107-
}
103+
var lhcTestWebhook = function(payload){
108104
$.post(WWW_DIR_JAVASCRIPT + 'genericbot/testpattern/' + $('#test-chat-id').val(), payload, function(data){
109105
$('#output-test').html('<pre class="fs11">'+data+'</pre>');
110106
});
107+
};
108+
$('#check-against-chat').click(function(){
109+
lhcTestWebhook({'webhook_id' : <?php echo $item->id?>});
111110
});
112-
$('input[name="type"]').change(function(){
113-
$('#test-chat-id').attr('placeholder', $(this).val() == '2' ? '<?php echo erTranslationClassLhTranslation::getInstance()->getTranslation('bot/conditions','Message ID');?>' : '<?php echo erTranslationClassLhTranslation::getInstance()->getTranslation('bot/conditions','Chat ID');?>');
111+
$('#check-against-mail').click(function(){
112+
lhcTestWebhook({'webhook_id' : <?php echo $item->id?>, 'mail' : 1});
114113
});
115114
</script>
116115
<?php endif; ?>

‎lhc_web/lib/core/lhchat/lhchatwebhookhttp.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ public function processEvent($event, $params, $single_event = false) {
8080

8181
public static function isValidConditions($continuousHook, $chat, $params = []) {
8282

83+
$debugMode = isset($params['debug']) && $params['debug'] === true;
84+
8385
$configurationParams = $continuousHook->conditions_array;
8486

8587
if (empty($configurationParams)) {
@@ -132,6 +134,7 @@ public static function isValidConditions($continuousHook, $chat, $params = []) {
132134
}
133135

134136
$previousMessageId = 0;
137+
$conditionsDebug = [];
135138

136139
// We do final check here
137140
$isValid = true;
@@ -288,6 +291,8 @@ public static function isValidConditions($continuousHook, $chat, $params = []) {
288291
} else if ($conditionsCurrent['condition'] == 'not_in_list_lowercase' && !in_array(strtolower(trim($conditionAttr)),explode('||',strtolower(trim($valueAttr))))) {
289292
$conditionItemValid = true;
290293
}
294+
295+
$conditionsDebug[] = ($conditionsCurrent['attr'] ?? '') . ' => ' . json_encode($conditionAttr) . ' ' . ($conditionsCurrent['condition'] ?? 'not_set') . ' ' . json_encode($valueAttr) . ' => ' . ($conditionItemValid ? 'VALID' : 'INVALID');
291296
}
292297

293298
if ($conditionItemValid == true){
@@ -300,6 +305,11 @@ public static function isValidConditions($continuousHook, $chat, $params = []) {
300305
}
301306
}
302307

308+
if ($debugMode) {
309+
erLhcoreClassGenericBotWorkflow::$triggerNameDebug[] = $conditionsDebug;
310+
erLhcoreClassGenericBotWorkflow::$triggerNameDebug[] = $isValid ? 'VALID' : 'INVALID';
311+
}
312+
303313
// Group is valid we can execute bot and trigger against specific chat
304314
if ($isValid === true) {
305315
return true;

‎lhc_web/lib/core/lhmailconv/lhmailconvparser.php‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ public static function syncMailbox($mailbox, $params = []) {
601601
}
602602
}
603603

604-
$message->headers_raw_array = erLhcoreClassMailconvParser::parseDeliveryStatus(preg_replace('/([\w-]+:\r\n)/i','',$head->{$attributeToUse}));
604+
$message->headers_raw_array = erLhcoreClassMailconvParser::parseDeliveryStatus($head->{$attributeToUse});
605605

606606
if (isset($message->headers_raw_array['Auto_Submitted'])) {
607607
if ($message->headers_raw_array['Auto_Submitted'] == 'auto-replied') {
@@ -907,7 +907,7 @@ public static function syncMailbox($mailbox, $params = []) {
907907

908908
$message = self::importMessage($vars, $mailbox, $mailboxHandler, $conversation, $head, $mailInfoRaw);
909909

910-
$message->headers_raw_array = erLhcoreClassMailconvParser::parseDeliveryStatus(preg_replace('/([\w-]+:\r\n)/i','',$head->{$attributeToUse}));
910+
$message->headers_raw_array = erLhcoreClassMailconvParser::parseDeliveryStatus($head->{$attributeToUse});
911911

912912
if (isset($message->headers_raw_array['Auto_Submitted'])) {
913913
if ($message->headers_raw_array['Auto_Submitted'] == 'auto-replied') {
@@ -1435,14 +1435,23 @@ public static function saveAttatchements($mail, & $message, $mailbox) {
14351435

14361436
public static function parseDeliveryStatus($text) {
14371437

1438-
$arr = preg_split('~([\w-]+: )~',$text,-1,PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY);
1438+
$res = array();
14391439

1440-
for ($res = array(), $i = 0; $i < count($arr); $i+=2) {
1441-
$key = strtr($arr[$i],array(': '=>'','-'=>'_'));
1442-
if (isset($arr[$i+1])) {
1443-
$res[$key] = trim($arr[$i+1]);
1440+
// Unfold multi line header values (continuation lines start with space or tab)
1441+
$text = preg_replace("~\r\n[ \t]+~", ' ', $text);
1442+
$text = str_replace(array("\r\n", "\r"), "\n", $text);
1443+
1444+
foreach (explode("\n", $text) as $line) {
1445+
1446+
// Header name has to be at the beginning of the line and ends with the first ":"
1447+
if (!preg_match('~^([A-Za-z0-9][A-Za-z0-9_\-]*)[ \t]*:(.*)$~', $line, $matches)) {
1448+
continue;
14441449
}
1450+
1451+
$key = str_replace('-', '_', $matches[1]);
1452+
$res[$key] = trim($matches[2]);
14451453
}
1454+
14461455
return $res;
14471456
}
14481457

‎lhc_web/modules/lhgenericbot/testpattern.php‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,17 @@
104104
} elseif (isset($_POST['webhook_id'])) {
105105

106106
$conditionToValidate = erLhcoreClassModelChatWebhook::fetch($_POST['webhook_id']);
107-
\LiveHelperChat\mailConv\Webhooks\Continous::processEventMail(['chat' => $chat, 'webhook' => $conditionToValidate, 'log' => true]);
108-
$response .= json_encode(erLhcoreClassGenericBotWorkflow::$triggerNameDebug, JSON_PRETTY_PRINT);
107+
108+
if ($conditionToValidate instanceof erLhcoreClassModelChatWebhook && $conditionToValidate->type == 2) {
109+
// Continuous mail event - use continuous events debug flow
110+
\LiveHelperChat\mailConv\Webhooks\Continous::processEventMail(['chat' => $chat, 'webhook' => $conditionToValidate, 'log' => true]);
111+
$response .= json_encode(erLhcoreClassGenericBotWorkflow::$triggerNameDebug, JSON_PRETTY_PRINT);
112+
} elseif ($conditionToValidate instanceof erLhcoreClassModelChatWebhook) {
113+
// Hook event - validate conditions same way as hook processing does for mail
114+
$isValidMail = erLhcoreClassChatWebhookHttp::isValidConditions($conditionToValidate, $chat, ['mail' => $chat, 'chat' => $chat, 'conversation' => $conversation, 'debug' => true]) === true;
115+
$response .= json_encode(erLhcoreClassGenericBotWorkflow::$triggerNameDebug, JSON_PRETTY_PRINT) . "\n" . ($isValidMail ? '✔️' : '❌');
116+
}
117+
109118
echo htmlspecialchars($response);
110119
exit;
111120

0 commit comments

Comments
 (0)