Skip to content
This repository was archived by the owner on Aug 15, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions Magento2/app/code/Werules/Chatbot/Block/Webhook/Telegram.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,55 @@ public function requestHandler()
$messageObject = new \stdClass();
$this->messageHandler($messageObject);
}

protected function processRequest()
{
$telegramInstance = $this->getTelegramInstance();
if (!$telegramInstance->getData())
return $this->getJsonErrorResponse();
$this->logPostData($telegramInstance->getData(), 'werules_chatbot_telegram.log');

$messageObject = $this->createMessageObject($telegramInstance);
if (isset($messageObject->content))
$result = $this->messageHandler($messageObject);
else
$result = $this->getJsonSuccessResponse(); // return success to avoid receiving the same message again

return $result;
}

protected function getTelegramInstance()
{
$api_token = $this->_helper->getConfigValue('werules_chatbot_telegram/general/api_key');
$telegram = $this->_chatbotAPI->initTelegramAPI($api_token);
return $telegram;
}

protected function createMessageObject($telegram)
{
$messageObject = new \stdClass();
if ($telegram->Text())
$content = $telegram->Text();
else
return $messageObject;

$messageObject->senderId = $telegram->ChatID();
$messageObject->content = $content;
$messageObject->status = $this->_define::PROCESSING;
$messageObject->direction = $this->_define::INCOMING;
$messageObject->chatType = $this->_define::TELEGRAM_INT; // TODO
$messageObject->contentType = $this->_define::CONTENT_TEXT; // TODO
$messageObject->currentCommandDetails = $this->_define::CURRENT_COMMAND_DETAILS_DEFAULT; // TODO
$messageObject->messagePayload = '{}'; // TODO
$messageObject->chatMessageId = $telegram->MessageID();
// if ($telegram->getMessageTimestamp())
// $messageObject->sentAt = substr($telegram->getMessageTimestamp(), 0, 10);
// else
$messageObject->sentAt = time();
$datetime = date('Y-m-d H:i:s');
$messageObject->createdAt = $datetime;
$messageObject->updatedAt = $datetime;

return $messageObject;
}
}
4 changes: 3 additions & 1 deletion Magento2/app/code/Werules/Chatbot/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -1370,6 +1370,8 @@ private function setConfigPrefix($chatbotType)
{
if ($chatbotType == $this->_define::MESSENGER_INT)
$this->_configPrefix = 'werules_chatbot_messenger';
else if ($chatbotType == $this->_define::TELEGRAM_INT)
$this->_configPrefix = 'werules_chatbot_telegram';
}
}

Expand Down Expand Up @@ -1478,7 +1480,7 @@ private function getWelcomeMessage($message)
private function getDisabledByCustomerMessage($message)
{
$outgoingMessages = array();
$text = __("To chat with me, please enable Messenger on your account chatbot settings.");
$text = __("To chat with me, please enable it on your account chatbot settings."); // TODO
$contentObj = $this->getTextMessageArray($text);
$outgoingMessage = $this->createOutgoingMessage($message, reset($contentObj)); // TODO reset -> gets first item of array
if ($outgoingMessage)
Expand Down
Loading