fjffk简å�•的咖啡店科开å�‘2方法2 环æ»�皆山也。其西å�—诸峰,林壑尤美,望之蔚然而深秀者,ç�…ç�Šä¹Ÿã€‚山行六七里,æ¸�闻水声潺潺,而泻出于两峰之间者,酿泉也 ??????????????????????????????? ??????????????/????????????????? ............/................... .........................../... ??????????????????????????????? >>>>>>>>>>>>>/>>>>>>>>>>>>>>>>> ÿØÿà JFIF    ÿÛ „  ( %!1!%)+//.383,7(-.+  -%%-////---/-.+/--+------/------/--0+--/-/-----.-----ÿÀ  Â¥2" ÿÄ     ÿÄ J    ! 1AQ"aqÂ�2‘#BR‚¡Ã�Ñ3br’¢±Âð$CSƒ²á4c“%DsÓñÿÄ   ÿÄ *  !1AQa‘"2q3Â�±ð#b¡ÿÚ   ? ¼QxJQaÃ�uò¸Zö Úü8,Ã�Úú "SSn<rçù–´âE—^ªBÖ9À\†¸Â�ÔÂ�Ã�T­ÃÛ5 ëd´³Ã�#Ã�;Þ38ϔÂ� ¶H£M:wÃŽ3…³…âpÔF&‚FK¸9„â4àGEõªfÂ�ÿ ‘ñ(ßw­pŽF|È¥ù®häðÃ�Ѷ¹‘[Ã’inÙW¶ùñYËœQ{›K"išÒ[Ú8žë\F¹@-?v"ÔUâ€�,ìöžkÿ {I‡£šÃ�?e rÂ�íV ?> ......................................... ............................................................................. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<< >>>>>>>>>>>>>/>>>>>>>>>>>>>>>>>> <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< >>>>>>>>>>>>>>>>>>>>>>>>>>>>>> dsdfextension.json000064400000000242152667053740007470 0ustar00{ "name": "Messenger Chatbot", "type": "extension", "version": "1.1.0", "description": "External voice Messenger Chatbot extension for magicai" } System/ChatbotMessengerServiceProvider.php000064400000005645152667053740015063 0ustar00registerConfig(); } public function boot(Kernel $kernel): void { $this->registerTranslations() ->registerViews() ->registerRoutes() ->registerMigrations() ->publishAssets() ->registerComponents(); } public function registerComponents(): static { // $this->loadViewComponentsAs('example', []); return $this; } public function publishAssets(): static { $this->publishes([ __DIR__ . '/../resources/assets/icons' => public_path('vendor/messenger-channel/icons'), ], 'extension'); return $this; } public function registerConfig(): static { $this->mergeConfigFrom(__DIR__ . '/../config/messenger-channel.php', 'messenger-channel'); return $this; } protected function registerTranslations(): static { $this->loadTranslationsFrom(__DIR__ . '/../resources/lang', 'messenger-channel'); return $this; } public function registerViews(): static { $this->loadViewsFrom([__DIR__ . '/../resources/views'], 'messenger-channel'); return $this; } public function registerMigrations(): static { $this->loadMigrationsFrom(__DIR__ . '/../database/migrations'); return $this; } private function registerRoutes(): static { $this->router() ->group([ 'middleware' => 'api', 'prefix' => 'api/v2/chatbot', 'as' => 'api.v2.chatbot.channel.', ], function (Router $router) { $router->any('{chatbotId}/channel/{channelId}/messenger', [ChatbotMessengerWebhookController::class, 'handle'])->name('messenger.post.handle'); })->group([ 'middleware' => ['web', 'auth'], ], function (Router $router) { $router->controller(ChatbotMessengerController::class) ->name('dashboard.chatbot-multi-channel.messenger.') ->prefix('dashboard/chatbot-multi-channel/messenger') ->group(function (Router $router) { $router->post('store', 'store')->name('store'); }); }); return $this; } private function router(): Router|Route { return $this->app['router']; } } System/Services/MessengerService.php000064400000001440152667053740013613 0ustar00chatbotChannel['credentials'], 'access_token', ''); $simpleMessengerBot = new \App\Extensions\ChatbotMessenger\System\Helpers\SimpleMessengerBot($access_token); $simpleMessengerBot->sendMessage($receiver, $message); } public function getChatbotChannel(): ChatbotChannel { return $this->chatbotChannel; } public function setChatbotChannel(ChatbotChannel $chatbotChannel): self { $this->chatbotChannel = $chatbotChannel; return $this; } } System/Services/MessengerConversationService.php000064400000024655152667053740016223 0ustar00setChatbotChannel(ChatbotChannel::find($this->channelId)); $recipient = data_get($this->payload, 'sender.id'); $messageType = data_get($this->payload, 'MessageType') ?: 'text'; $messageBody = data_get($this->payload, 'message.text'); $conversation = $this->conversation; $chatbot = $conversation->chatbot; if ($conversation->connect_agent_at) { if ($conversation->last_activity_at->diffInMinutes() > 10) { $this->closeInactiveConversation($conversation, $messenger, $recipient); return; } return; } $conversation->update(['last_activity_at' => now()]); if ($messageType === 'text' && is_string($messageBody)) { $this->processTextMessage($messageBody, $conversation, $chatbot, $messenger, $recipient); } else { $this->sendUnsupportedMessageType($conversation, $chatbot, $messenger, $recipient); } } protected function closeInactiveConversation(ChatbotConversation $conversation, MessengerService $messenger, string $recipient): void { $conversation->update(['connect_agent_at' => null]); $message = trans('The conversation has been closed due to inactivity.'); $this->insertMessage($conversation, $message, 'assistant', $conversation->chatbot->ai_model); $messenger->sendText($message, $recipient); } protected function processTextMessage(string $messageBody, ChatbotConversation $conversation, Chatbot $chatbot, MessengerService $messenger, string $recipient): void { if ($this->isHumanAgentCommand($chatbot, $messageBody)) { $this->connectToHumanAgent($chatbot, $conversation, $messenger, $recipient); return; } $response = $this->generateResponse($messageBody) ?? trans("Sorry, I can't answer right now."); if (! $conversation->connect_agent_at && $chatbot->interaction_type === InteractionType::SMART_SWITCH && MarketplaceHelper::isRegistered('chatbot-agent')) { $response .= "\n\n\nTo speak with a live support agent, please enter the #{$this->humanAgentCommand} command."; } $messenger->sendText($response, $recipient); $this->insertMessage($conversation, $response, 'assistant', $chatbot->ai_model); } protected function sendUnsupportedMessageType(ChatbotConversation $conversation, Chatbot $chatbot, MessengerService $messenger, string $recipient): void { $message = trans('The chatbot does not support the type of message you are sending.'); $this->insertMessage($conversation, $message, 'assistant', $chatbot->ai_model); $messenger->sendText($message, $recipient); } protected function connectToHumanAgent(Chatbot $chatbot, ChatbotConversation $conversation, MessengerService $messenger, string $recipient): void { $conversation->update(['connect_agent_at' => now()]); if ($connectMessage = $chatbot->connect_message) { $chatbotHistory = $this->insertMessage($conversation, $connectMessage, 'assistant', $chatbot->ai_model, true); $messenger->sendText($connectMessage, $recipient); $this->dispatchAgentEvent($chatbot, $conversation, $chatbotHistory); } } protected function dispatchAgentEvent(Chatbot $chatbot, ChatbotConversation $conversation, ?ChatbotHistory $chatbotHistory): void { if (MarketplaceHelper::isRegistered('chatbot-agent')) { try { ChatbotForPanelEventAbly::dispatch($chatbot, $conversation->load('lastMessage'), $chatbotHistory); } catch (Exception $e) { Log::error($e->getMessage()); } } } protected function isHumanAgentCommand(Chatbot $chatbot, string $message): bool { return str_contains($message, $this->humanAgentCommand) && $chatbot->interaction_type === InteractionType::SMART_SWITCH; } protected function generateResponse(string $prompt): ?string { return app(GeneratorService::class) ->setChatbot($this->conversation->chatbot) ->setConversation($this->conversation) ->setPrompt($prompt) ->generate(); } public function insertMessage(ChatbotConversation $conversation, string $message, string $role, string $model, bool $forcePanelEvent = false) { $chatbot = $conversation->getAttribute('chatbot'); $chatbotHistory = ChatbotHistory::query()->create([ 'chatbot_id' => $conversation->getAttribute('chatbot_id'), 'conversation_id' => $conversation->getAttribute('id'), 'message_id' => data_get($this->payload, 'message.mid'), 'role' => $role, 'model' => Helper::setting('openai_default_model'), 'message' => $message, 'message_type' => data_get($this->payload, 'MessageType') ?? 'text', 'content_type' => data_get($this->payload, 'MediaContentType0') ?? 'text', 'created_at' => now(), 'read_at' => $conversation->getAttribute('connect_agent_at') ? null : now(), ]); $this->history = $chatbotHistory; $sendEvent = $conversation->getAttribute('connect_agent_at') && $chatbot->getAttribute('interaction_type') !== InteractionType::AUTOMATIC_RESPONSE && $role === 'user'; if ($sendEvent || $forcePanelEvent) { $conversation->touch(); if (MarketplaceHelper::isRegistered('chatbot-agent')) { try { ChatbotForPanelEventAbly::dispatch( $chatbot, $conversation->load('lastMessage'), $chatbotHistory ); } catch (Exception $e) { Log::error($e->getMessage()); } } } return $chatbotHistory; } public function storeHistory(Builder|Model|null $conversation = null): void { $conversation ??= $this->conversation; $this->existMessage = ChatbotHistory::query() ->where('conversation_id', $conversation->getKey()) ->exists(); $this->history = ChatbotHistory::create([ 'chatbot_id' => $conversation->getAttribute('chatbot_id'), 'conversation_id' => $conversation->getKey(), 'message_id' => data_get($this->payload, 'message.mid'), 'role' => 'user', 'model' => Helper::setting('openai_default_model'), 'message' => data_get($this->payload, 'message.text', ''), 'message_type' => data_get($this->payload, 'MessageType'), 'content_type' => data_get($this->payload, 'MediaContentType0') ?? 'text', 'read_at' => $conversation->getAttribute('connect_agent_at') ? null : now(), 'created_at' => now(), ]); } public function storeConversation(): Builder|Model|ChatbotConversation { $this->chatbot = Chatbot::find($this->chatbotId); $this->conversation = ChatbotConversation::firstOrCreate([ 'chatbot_id' => $this->chatbotId, 'chatbot_channel' => 'messenger', 'chatbot_channel_id' => $this->channelId, 'customer_channel_id' => $this->getCustomerChannelId(), ], [ 'session_id' => md5(uniqid(mt_rand(), true)), 'conversation_name' => data_get($this->payload, 'sender.id'), 'ip_address' => $this->ipAddress, 'connect_agent_at' => $this->chatbot->getAttribute('interaction_type') === InteractionType::HUMAN_SUPPORT ? now() : null, 'last_activity_at' => now(), 'customer_payload' => [ 'From' => $this->getCustomerChannelId(), ], ]); $this->existMessage = ChatbotHistory::query() ->where('conversation_id', $this->conversation->getKey()) ->exists(); $this->conversation->setRelation('chatbot', $this->chatbot); return $this->conversation; } public function getCustomerChannelId(): ?string { return data_get($this->payload, 'sender.id'); } public function getChatbotId(): int { return $this->chatbotId; } public function setChatbotId(int $chatbotId): self { $this->chatbotId = $chatbotId; return $this; } public function getChannelId(): int { return $this->channelId; } public function setChannelId(int $channelId): self { $this->channelId = $channelId; return $this; } public function setIpAddress(?int $ipAddress = null): self { if ($ipAddress) { $this->ipAddress = $ipAddress; } else { $this->ipAddress = request()?->header('cf-connecting-ip') ?? request()?->ip(); } return $this; } public function getPayload(): ?array { return $this->payload; } public function setPayload(?array $payload): self { $this->payload = $payload; return $this; } public function getChatbot(): Model|Builder|Chatbot|null { return $this->chatbot; } } System/Helpers/SimpleMessengerBot.php000064400000003431152667053740013732 0ustar00pageAccessToken = $pageAccessToken; } public function sendMessage($userId, $message) { $url = 'https://graph.facebook.com/v18.0/me/messages'; $data = [ 'recipient' => ['id' => $userId], 'message' => ['text' => $message], ]; $curl = curl_init(); curl_setopt_array($curl, [ CURLOPT_URL => $url, CURLOPT_POST => true, CURLOPT_POSTFIELDS => json_encode($data), CURLOPT_HTTPHEADER => [ 'Content-Type: application/json', 'Authorization: Bearer ' . $this->pageAccessToken, ], CURLOPT_RETURNTRANSFER => true, CURLOPT_TIMEOUT => 10, ]); $response = curl_exec($curl); $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE); curl_close($curl); return $httpCode === 200; } public function verifyWebhook($verifyToken) { if (isset($_GET['hub_verify_token']) && $_GET['hub_verify_token'] === $verifyToken) { echo $_GET['hub_challenge']; exit; } } public function getIncomingMessage() { $input = file_get_contents('php://input'); $data = json_decode($input, true); if (isset($data['entry'][0]['messaging'][0]['message'])) { $messaging = $data['entry'][0]['messaging'][0]; return [ 'user_id' => $messaging['sender']['id'], 'message' => $messaging['message']['text'] ?? '', ]; } return null; } } System/Http/Controllers/ChatbotMessengerController.php000064400000002712152667053740017310 0ustar00json([ 'status' => 'error', 'message' => trans('This feature is disabled in demo mode.'), ]); } $item = ChatbotChannel::query()->create( $request->validated() ); try { app(MessengerService::class)->setChatbotChannel($item); } catch (Exception $exception) { $item->delete(); return response()->json([ 'status' => 'error', 'message' => trans('Messenger channel not connected'), ]); } return ChatbotChannelResource::make($item)->additional([ 'status' => 'success', 'message' => trans('Messenger channel successfully created'), ]); } } System/Http/Controllers/Webhook/ChatbotMessengerWebhookController.php000064400000004044152667053740022225 0ustar00findOrFail($channelId); ChatbotChannelWebhook::query()->create([ 'chatbot_id' => $chatbotId, 'chatbot_channel_id' => $channelId, 'payload' => $request->all(), 'created_at' => now(), ]); $this->verifyWebhook(data_get($channel['credentials'], 'verify_token', '')); if (! $request->input('entry.0.messaging.0')) { return; } $this->service ->setIpAddress() ->setChatbotId($chatbotId) ->setChannelId($channelId) ->setPayload($request->input('entry.0.messaging.0')); $conversation = $this->service->storeConversation(); /** * @var ChatbotChannel $chatbot */ $chatbot = $this->service->getChatbot(); $this->service->insertMessage( conversation: $conversation, message: $request->input('entry.0.messaging.0.message.text') ?? '', role: 'user', model: $chatbot->getAttribute('ai_model') ); $this->service->handle(); } private function verifyWebhook($verifyToken): void { if (isset($_GET['hub_verify_token']) && $_GET['hub_verify_token'] === $verifyToken) { if (isset($_GET['hub_challenge'])) { echo $_GET['hub_challenge']; exit; } } } } System/Http/Requests/MessengerChannelStoreRequest.php000064400000001274152667053740017125 0ustar00 'required|string', 'user_id' => 'required', 'chatbot_id' => 'required', 'credentials' => 'array|required', 'connected_at'=> 'string', ]; } protected function prepareForValidation(): void { $this->merge([ 'user_id' => auth()->id(), 'connected_at' => (string) now(), 'channel' => 'messenger', ]); } } resources/assets/icons/facebook-messenger.svg000064400000000747152667053740015502 0ustar00resources/views/channel-card.blade.php000064400000005622152667053740014035 0ustar00@php $image = 'vendor/messenger-channel/icons/facebook-messenger.svg'; $image_dark_version = 'vendor/chatbot-multi-channel/icons/whatsapp-light.svg'; $darkImageExists = file_exists(public_path($image_dark_version)); @endphp
$darkImageExists, ]) src="{{ asset($image) }}" alt="messenger" /> @if ($darkImageExists) @endif

Messenger

Messenger

@lang('You can add a new channel by including the Facebook messenger integration details.')

@if ($app_is_demo) {{ __('Add Channel') }} @else Loading... {{ __('Add Channel') }} @endif
config/messenger-channel.php000064400000000117152667053740012136 0ustar00 'Messenger Channel', 'version' => 1.0, ]; __MACOSX/._System000064400000000243152667053740007476 0ustar00Mac OS X  2q£ATTR£˜ ˜ com.apple.provenance¤`˜†TÜç__MACOSX/._resources000064400000000243152667053740010224 0ustar00Mac OS X  2q£ATTR£˜ ˜ com.apple.provenance¤`˜†TÜç__MACOSX/._config000064400000000243152667053740007457 0ustar00Mac OS X  2q£ATTR£˜ ˜ com.apple.provenance¤`˜†TÜç__MACOSX/System/._Helpers000064400000000243152667053740011100 0ustar00Mac OS X  2q£ATTR£˜ ˜ com.apple.provenance¤`˜†TÜç__MACOSX/System/._ChatbotMessengerServiceProvider.php000064400000000243152667053740016555 0ustar00Mac OS X  2q£ATTR£˜ ˜ com.apple.provenance¤`˜†TÜç__MACOSX/System/Services/._MessengerConversationService.php000064400000000243152667053740017713 0ustar00Mac OS X  2q£ATTR£˜ ˜ com.apple.provenance¤`˜†TÜç__MACOSX/System/Services/._MessengerService.php000064400000000243152667053740015320 0ustar00Mac OS X  2q£ATTR£˜ ˜ com.apple.provenance¤`˜†TÜç__MACOSX/System/Helpers/._SimpleMessengerBot.php000064400000000243152667053740015435 0ustar00Mac OS X  2q£ATTR£˜ ˜ com.apple.provenance¤`˜†TÜç__MACOSX/System/._Http000064400000000243152667053740010415 0ustar00Mac OS X  2q£ATTR£˜ ˜ com.apple.provenance¤`˜†TÜç__MACOSX/System/._Services000064400000000243152667053740011261 0ustar00Mac OS X  2q£ATTR£˜ ˜ com.apple.provenance¤`˜†TÜç__MACOSX/System/Http/Controllers/._ChatbotMessengerController.php000064400000000243152667053740021012 0ustar00Mac OS X  2q£ATTR£˜ ˜ com.apple.provenance¤`˜†TÜç__MACOSX/System/Http/Controllers/Webhook/._ChatbotMessengerWebhookController.php000064400000000243152667053740023727 0ustar00Mac OS X  2q£ATTR£˜ ˜ com.apple.provenance¤`˜†TÜç__MACOSX/System/Http/Controllers/._Webhook000064400000000243152667053740014321 0ustar00Mac OS X  2q£ATTR£˜ ˜ com.apple.provenance¤`˜†TÜç__MACOSX/System/Http/Requests/._MessengerChannelStoreRequest.php000064400000000243152667053740020625 0ustar00Mac OS X  2q£ATTR£˜ ˜ com.apple.provenance¤`˜†TÜç__MACOSX/System/Http/._Requests000064400000000243152667053740012230 0ustar00Mac OS X  2q£ATTR£˜ ˜ com.apple.provenance¤`˜†TÜç__MACOSX/System/Http/._Controllers000064400000000243152667053740012723 0ustar00Mac OS X  2q£ATTR£˜ ˜ com.apple.provenance¤`˜†TÜç__MACOSX/resources/._assets000064400000000243152667053740011526 0ustar00Mac OS X  2q£ATTR£˜ ˜ com.apple.provenance¤`˜†TÜç__MACOSX/resources/assets/._icons000064400000000243152667053740012641 0ustar00Mac OS X  2q£ATTR£˜ ˜ com.apple.provenance¤`˜†TÜç__MACOSX/resources/assets/icons/._facebook-messenger.svg000064400000000243152667053740017176 0ustar00Mac OS X  2q£ATTR£˜ ˜ com.apple.provenance¤`˜†TÜç__MACOSX/resources/views/._channel-card.blade.php000064400000000243152667053740015534 0ustar00Mac OS X  2q£ATTR£˜ ˜ com.apple.provenance¤`˜†TÜç__MACOSX/resources/._views000064400000000243152667053740011361 0ustar00Mac OS X  2q£ATTR£˜ ˜ com.apple.provenance¤`˜†TÜç__MACOSX/config/._messenger-channel.php000064400000000243152667053740013643 0ustar00Mac OS X  2q£ATTR£˜ ˜ com.apple.provenance¤`˜†TÜç__MACOSX/._extension.json000064400000000333152667053740011176 0ustar00Mac OS X  2©ÛATTRÛÀÀcom.apple.lastuseddate#PSÐ com.apple.provenancel8¸hÖsÅ ¤`˜†TÜç