Loading demo_fb_messenger_bot/demo_fb_messenger_bot.info.yml 100755 → 100644 +2 −1 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ name: Demo Facebook Messenger Bot description: Custom demo bot built using fb_messenger_bot module. package: Custom type: module core: 8.x # core: 8.x dependencies: - fb_messenger_bot:fb_messenger_bot core_version_requirement: ^8 || ^9 fb_messenger_bot.info.yml +2 −1 Original line number Diff line number Diff line Loading @@ -2,5 +2,6 @@ name: Facebook Messenger Bot description: Create a Facebook Messenger Bot. package: Custom type: module core: 8.x # core: 8.x configure: fb_messenger_bot.admin_settings core_version_requirement: ^8 || ^9 fb_messenger_bot.services.yml +1 −1 Original line number Diff line number Diff line Loading @@ -4,7 +4,7 @@ services: arguments: ['@config.factory', '@http_client', '@logger.channel.fb_messenger_bot', '@request_stack'] fb_messenger_bot.conversation_factory: class: Drupal\fb_messenger_bot\Conversation\ConversationFactory arguments: ['@entity.query'] arguments: ['@entity_type.manager'] fb_messenger_bot.workflow: class: Drupal\fb_messenger_bot\Workflow\FBMessengerBotWorkflow arguments: ['@config.factory', '@fb_messenger_bot.conversation_factory', '@string_translation', '@fb_messenger_bot.fb_service', '@logger.channel.fb_messenger_bot'] Loading src/Conversation/ConversationFactory.php +24 −16 Original line number Diff line number Diff line Loading @@ -2,7 +2,9 @@ namespace Drupal\fb_messenger_bot\Conversation; use Drupal\Core\Entity\Query\QueryFactory; use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException; use Drupal\Component\Plugin\Exception\PluginNotFoundException; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\fb_messenger_bot\Entity\BotConversation; /** Loading @@ -13,20 +15,20 @@ use Drupal\fb_messenger_bot\Entity\BotConversation; class ConversationFactory implements ConversationFactoryInterface { /** * The entity query factory. * The entity type manager. * * @var \Drupal\Core\Entity\Query\QueryFactory * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ protected $queryFactory; protected $entityTypeManager; /** * ConversationFactory constructor. * * @param \Drupal\Core\Entity\Query\QueryFactory $queryFactory * The entity query factory. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager * The entity type manager. */ public function __construct(QueryFactory $queryFactory) { $this->queryFactory = $queryFactory; public function __construct(EntityTypeManagerInterface $entityTypeManager) { $this->entityTypeManager = $entityTypeManager; } /** Loading @@ -51,16 +53,22 @@ class ConversationFactory implements ConversationFactoryInterface { * @param string $uid * The conversation uid. * * @return array|int * The entityQuery result. * @return int|null * The query results. */ protected function getActiveConversationId($uid) { $query = $this->queryFactory->get('fb_messenger_bot_conversation') try { $query = $this->entityTypeManager ->getStorage('fb_messenger_bot_conversation') ->getQuery() ->condition('uid', $uid) ->condition('complete', BotConversationInterface::INCOMPLETE) ->sort('created') ->range(0, 1); $ids = $query->execute(); } catch (InvalidPluginDefinitionException|PluginNotFoundException $e) { return NULL; } foreach ($ids as $id) { return $id; } Loading src/FacebookService.php +13 −10 Original line number Diff line number Diff line <?php /** * @file * Contains \Drupal\fb_messenger_bot\FacebookService. * * @todo: refactor to conform to an interface standard */ namespace Drupal\fb_messenger_bot; use GuzzleHttp\Exception\RequestException; use Drupal\Component\Serialization\Json; use GuzzleHttp\ClientInterface; use Drupal\fb_messenger_bot\Message\MessageInterface; use GuzzleHttp\Exception; use Symfony\Component\HttpFoundation\Response; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Component\Utility\Html; Loading Loading @@ -65,7 +66,7 @@ class FacebookService { * A Guzzle client object. * @param \Psr\Log\LoggerInterface $logger * A logger instance. * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack * @param \Symfony\Component\HttpFoundation\RequestStack $request * The request stack. */ function __construct(ConfigFactoryInterface $configFactory, ClientInterface $httpClient, LoggerInterface $logger, RequestStack $request) { Loading Loading @@ -201,6 +202,10 @@ class FacebookService { } foreach ($data['entry'] as $entry) { if (empty($entry['messaging'])) { continue; } foreach ($entry['messaging'] as $message) { $uid = $message['sender']['id']; $messageType = self::typeFromMessage($message); Loading @@ -212,7 +217,7 @@ class FacebookService { continue; } $messages[$uid] = isset($messages[$uid]) ? $messages[$uid] : []; $messages[$uid] = $messages[$uid] ?? []; $messages[$uid][] = [ 'message_sender' => $message['sender'], 'message_recipient' => $message['recipient'], Loading Loading @@ -292,12 +297,10 @@ class FacebookService { public static function splitTextMessage($message, $startPosition = 0) { $maxLength = self::MESSAGE_TYPE_TEXT_OUT_LIMIT; $messageParts = array(); $message = Html::decodeEntities(trim($message), ENT_QUOTES); $message = Html::decodeEntities(trim($message)); $messagePart = substr($message, $startPosition, $maxLength); if (strlen($message) > ($startPosition + $maxLength)) { $whiteSpaceMatches = preg_match('/.*\s([^\s]+)$/', $messagePart, $matches); $trimLength = 0; if (!empty($matches[1])) { if (strlen($matches[1]) < strlen($messagePart)) { $trimLength = strlen($matches[1]); Loading Loading @@ -385,9 +388,9 @@ class FacebookService { * API we were using when we received the error. */ public function logFacebookErrorResponse($response, $api = 'Send API') { $message = isset($response['error']['message']) ? $response['error']['message'] : ''; $type = isset($response['error']['type']) ? $response['error']['type'] : ''; $code = isset($response['error']['code']) ? $response['error']['code'] : ''; $message = $response['error']['message'] ?? ''; $type = $response['error']['type'] ?? ''; $code = $response['error']['code'] ?? ''; $loggerVariables = array( '@api' => $api, '@message' => $message, Loading @@ -402,7 +405,7 @@ class FacebookService { /** * Helper function to log outgoing POST messages being sent. * * @param string $message * @param string|array $message * * @return bool */ Loading Loading
demo_fb_messenger_bot/demo_fb_messenger_bot.info.yml 100755 → 100644 +2 −1 Original line number Diff line number Diff line Loading @@ -2,6 +2,7 @@ name: Demo Facebook Messenger Bot description: Custom demo bot built using fb_messenger_bot module. package: Custom type: module core: 8.x # core: 8.x dependencies: - fb_messenger_bot:fb_messenger_bot core_version_requirement: ^8 || ^9
fb_messenger_bot.info.yml +2 −1 Original line number Diff line number Diff line Loading @@ -2,5 +2,6 @@ name: Facebook Messenger Bot description: Create a Facebook Messenger Bot. package: Custom type: module core: 8.x # core: 8.x configure: fb_messenger_bot.admin_settings core_version_requirement: ^8 || ^9
fb_messenger_bot.services.yml +1 −1 Original line number Diff line number Diff line Loading @@ -4,7 +4,7 @@ services: arguments: ['@config.factory', '@http_client', '@logger.channel.fb_messenger_bot', '@request_stack'] fb_messenger_bot.conversation_factory: class: Drupal\fb_messenger_bot\Conversation\ConversationFactory arguments: ['@entity.query'] arguments: ['@entity_type.manager'] fb_messenger_bot.workflow: class: Drupal\fb_messenger_bot\Workflow\FBMessengerBotWorkflow arguments: ['@config.factory', '@fb_messenger_bot.conversation_factory', '@string_translation', '@fb_messenger_bot.fb_service', '@logger.channel.fb_messenger_bot'] Loading
src/Conversation/ConversationFactory.php +24 −16 Original line number Diff line number Diff line Loading @@ -2,7 +2,9 @@ namespace Drupal\fb_messenger_bot\Conversation; use Drupal\Core\Entity\Query\QueryFactory; use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException; use Drupal\Component\Plugin\Exception\PluginNotFoundException; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\fb_messenger_bot\Entity\BotConversation; /** Loading @@ -13,20 +15,20 @@ use Drupal\fb_messenger_bot\Entity\BotConversation; class ConversationFactory implements ConversationFactoryInterface { /** * The entity query factory. * The entity type manager. * * @var \Drupal\Core\Entity\Query\QueryFactory * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ protected $queryFactory; protected $entityTypeManager; /** * ConversationFactory constructor. * * @param \Drupal\Core\Entity\Query\QueryFactory $queryFactory * The entity query factory. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager * The entity type manager. */ public function __construct(QueryFactory $queryFactory) { $this->queryFactory = $queryFactory; public function __construct(EntityTypeManagerInterface $entityTypeManager) { $this->entityTypeManager = $entityTypeManager; } /** Loading @@ -51,16 +53,22 @@ class ConversationFactory implements ConversationFactoryInterface { * @param string $uid * The conversation uid. * * @return array|int * The entityQuery result. * @return int|null * The query results. */ protected function getActiveConversationId($uid) { $query = $this->queryFactory->get('fb_messenger_bot_conversation') try { $query = $this->entityTypeManager ->getStorage('fb_messenger_bot_conversation') ->getQuery() ->condition('uid', $uid) ->condition('complete', BotConversationInterface::INCOMPLETE) ->sort('created') ->range(0, 1); $ids = $query->execute(); } catch (InvalidPluginDefinitionException|PluginNotFoundException $e) { return NULL; } foreach ($ids as $id) { return $id; } Loading
src/FacebookService.php +13 −10 Original line number Diff line number Diff line <?php /** * @file * Contains \Drupal\fb_messenger_bot\FacebookService. * * @todo: refactor to conform to an interface standard */ namespace Drupal\fb_messenger_bot; use GuzzleHttp\Exception\RequestException; use Drupal\Component\Serialization\Json; use GuzzleHttp\ClientInterface; use Drupal\fb_messenger_bot\Message\MessageInterface; use GuzzleHttp\Exception; use Symfony\Component\HttpFoundation\Response; use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Component\Utility\Html; Loading Loading @@ -65,7 +66,7 @@ class FacebookService { * A Guzzle client object. * @param \Psr\Log\LoggerInterface $logger * A logger instance. * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack * @param \Symfony\Component\HttpFoundation\RequestStack $request * The request stack. */ function __construct(ConfigFactoryInterface $configFactory, ClientInterface $httpClient, LoggerInterface $logger, RequestStack $request) { Loading Loading @@ -201,6 +202,10 @@ class FacebookService { } foreach ($data['entry'] as $entry) { if (empty($entry['messaging'])) { continue; } foreach ($entry['messaging'] as $message) { $uid = $message['sender']['id']; $messageType = self::typeFromMessage($message); Loading @@ -212,7 +217,7 @@ class FacebookService { continue; } $messages[$uid] = isset($messages[$uid]) ? $messages[$uid] : []; $messages[$uid] = $messages[$uid] ?? []; $messages[$uid][] = [ 'message_sender' => $message['sender'], 'message_recipient' => $message['recipient'], Loading Loading @@ -292,12 +297,10 @@ class FacebookService { public static function splitTextMessage($message, $startPosition = 0) { $maxLength = self::MESSAGE_TYPE_TEXT_OUT_LIMIT; $messageParts = array(); $message = Html::decodeEntities(trim($message), ENT_QUOTES); $message = Html::decodeEntities(trim($message)); $messagePart = substr($message, $startPosition, $maxLength); if (strlen($message) > ($startPosition + $maxLength)) { $whiteSpaceMatches = preg_match('/.*\s([^\s]+)$/', $messagePart, $matches); $trimLength = 0; if (!empty($matches[1])) { if (strlen($matches[1]) < strlen($messagePart)) { $trimLength = strlen($matches[1]); Loading Loading @@ -385,9 +388,9 @@ class FacebookService { * API we were using when we received the error. */ public function logFacebookErrorResponse($response, $api = 'Send API') { $message = isset($response['error']['message']) ? $response['error']['message'] : ''; $type = isset($response['error']['type']) ? $response['error']['type'] : ''; $code = isset($response['error']['code']) ? $response['error']['code'] : ''; $message = $response['error']['message'] ?? ''; $type = $response['error']['type'] ?? ''; $code = $response['error']['code'] ?? ''; $loggerVariables = array( '@api' => $api, '@message' => $message, Loading @@ -402,7 +405,7 @@ class FacebookService { /** * Helper function to log outgoing POST messages being sent. * * @param string $message * @param string|array $message * * @return bool */ Loading