Skip to content

Instantly share code, notes, and snippets.

@phanngoc
Created August 27, 2018 01:22
Show Gist options
  • Save phanngoc/d25f67aa62fdba1d0f30d8b14272cb90 to your computer and use it in GitHub Desktop.
Save phanngoc/d25f67aa62fdba1d0f30d8b14272cb90 to your computer and use it in GitHub Desktop.
public function search($text, $referralUrl = '', $isPdca = false, $reChangeText = '', $threadId = null)
{
ubenchStart('test time addMessage');
$text = str_replace("\n", "\r\n", trim($text));
if ($isPdca) {
$this->messageService->addMessageBot($reChangeText, 'text', config('js.owner.user'), Message::RO_CLIENT);
} else {
$this->messageService->addMessageBot($text, 'text', config('js.owner.user'), Message::RO_CLIENT);
}
ubenchEnd('test time addMessage');
ubenchStart('test time search');
$firstText = $text;
$uuid = (string)Uuid::generate();
$text = self::detectLastEmoOrTrim($text);
$keyword = $this->keyword->with(['answers', 'icon'])
->where(function ($query) use ($text) {
$query->where('question', $text)
->orWhere('pdca', $text)
->orWhereRaw('JSON_SEARCH(synonym, \'all\', ?) IS NOT NULL', ['%' . self::escapeLike($text) .'%']);
})->first();
$messageTemplate = $this->mismatchService->find();
$emo = $this->emo->with(['icon'])->where('shortcut', $text)->first();
if ($text && $emo) {
$data = ['emo' => $emo, 'uuid' => $uuid];
self::publishResponse($threadId, ['type' => BaseController::MESSAGE_OK, 'data' => $data]);
$this->createConversionHistory($firstText, self::rawTextRespondEmo($emo), Conversation::MATCH, $referralUrl, $uuid);
$this->messageService->addMessageBot($emo->id, 'emo', config('js.owner.bot'), Message::RO_SERVER, true, $threadId, $uuid);
} elseif ($keyword) {
$keyword->increment('count_show');
$data = ['keyword' => $keyword, 'uuid' => $uuid];
self::publishResponse($threadId, ['type' => BaseController::MESSAGE_OK, 'data' => $data]);
$this->createConversionHistory($firstText, self::rawTextRespond($keyword), Conversation::MATCH, $referralUrl, $uuid);
$this->messageService->addMessageBot($keyword->id, 'keyword', config('js.owner.bot'), Message::RO_SERVER, true, $threadId, $uuid);
} else {
$reCheckKeyword = $this->trimAndResearch($text);
if ($reCheckKeyword) {
$data = ['keyword' => $reCheckKeyword, 'uuid' => $uuid];
self::publishResponse($threadId, ['type' => BaseController::MESSAGE_OK, 'data' => $data]);
$this->createConversionHistory($firstText, self::rawTextRespond($reCheckKeyword), Conversation::MATCH, $referralUrl, $uuid);
$this->messageService->addMessageBot($reCheckKeyword->id, 'keyword', config('js.owner.bot'), Message::RO_SERVER, true, $threadId, $uuid);
} else {
self::publishResponse($threadId, ['type' => BaseController::MESSAGE_NOT_OK, 'data' => $messageTemplate]);
$this->createConversionHistory($firstText, $messageTemplate->text, Conversation::NOT_MATCH, $referralUrl, $uuid);
$this->messageService->addMessageBot($messageTemplate->id, 'message_template', config('js.owner.bot'), Message::RO_SERVER, true, $threadId, $uuid);
}
}
ubenchEnd('test time search');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment