Skip to content

Instantly share code, notes, and snippets.

@afiqiqmal
Last active April 1, 2022 11:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save afiqiqmal/a46b4cc2680a63fd521cd98ea1ef5209 to your computer and use it in GitHub Desktop.
Save afiqiqmal/a46b4cc2680a63fd521cd98ea1ef5209 to your computer and use it in GitHub Desktop.
Laravel Botman Custom Driver Just for Combining Multiple Attachment to Single Upload Attachment

Botman Custom Incoming Message for Attachment

this code is for Botman PHP Laravel use Custom Driver Just for Combining Multiple Attachment to Single Upload Attachment

Default conditions botman split the attachment according to their type ex: Audio, Video, Files, Attachments, Images and etc..

now CustomLaraStorageDriver make the attachments combine into 1 request consist of Audio, Video, Files, Attachments, Images and etc..

<?php
use App\Conversation\Driver\CustomLaraStorageDriver;
use App\Http\Controllers\Api\ApiChatController;
use BotMan\BotMan\Drivers\DriverManager;
DriverManager::loadDriver(CustomLaraStorageDriver::class);
$botman = resolve('botman');
$botman->hears('init_chat', ApiChatController::class.'@startConversation');
<?php
/**
* Created by PhpStorm.
* User: hafiq
* Date: 18/03/2018
* Time: 11:59 PM
*/
namespace App\Conversation\Custom;
use BotMan\BotMan\Messages\Incoming\IncomingMessage;
class CustomIncomingMessage extends IncomingMessage
{
/** @var array */
protected $mixFile = [];
public function __construct($message, $sender, $recipient, $payload = null)
{
parent::__construct($message, $sender, $recipient, $payload);
}
/**
* @return array
*/
public function getMixFile()
{
return $this->mixFile;
}
/**
* @param array $mixFile
*/
public function setMixFile(array $mixFile)
{
$this->mixFile = $mixFile;
}
}
<?php
/**
* Created by PhpStorm.
* User: hafiq
* Date: 14/02/2018
* Time: 4:18 PM
*/
namespace App\Conversation\Driver;
use App\Conversation\Custom\CustomIncomingMessage;
use App\Conversation\Custom\MixFile;
use BotMan\BotMan\Messages\Attachments\CustomLocation;
use BotMan\BotMan\Messages\Attachments\Location;
use BotMan\Drivers\Web\WebDriver;
class CustomLaraStorageDriver extends WebDriver
{
const ATTACHMENT_CUSTOM = 'custom';
public function getMessages()
{
if (empty($this->messages)) {
$message = $this->event->get('message');
$userId = $this->event->get('chat_id');
if (!$userId) {
response()->error('Chat ID is Required')->send();
die();
}
$sender = $this->event->get('sender', $userId);
$incomingMessage = new CustomIncomingMessage($message, $sender, $userId, $this->payload);
$incomingMessage = $this->addCustomAttachments($incomingMessage);
$this->messages = [$incomingMessage];
}
return $this->messages;
}
protected function addCustomAttachments(CustomIncomingMessage $incomingMessage)
{
$attachment = $this->event->get('attachment');
$files = isset($this->files['attachment_data']) ? $this->files['attachment_data'] : null;
if ($attachment === self::ATTACHMENT_CUSTOM) {
$images = collect($files)->map(function ($file) {
//$path = store_file($file, 'chat'); store files
return (new MixFile($file, $path))->toWebDriver();
})->values()->toArray();
$incomingMessage->setText(MixFile::PATTERN);
$incomingMessage->setMixFile($images);
} elseif ($attachment == self::ATTACHMENT_LOCATION) {
$longitude = $this->event->get('longitude');
$latitude = $this->event->get('latitude');
$location_name = $this->event->get('location_name');
$location = new CustomLocation($longitude, $latitude, $location_name);
$incomingMessage->setText(Location::PATTERN);
$incomingMessage->setLocation($location);
}
return $incomingMessage;
}
}
<?php
namespace BotMan\BotMan\Messages\Attachments;
class CustomLocation extends Location
{
protected $location_name;
/**
* Message constructor.
* @param string $latitude
* @param string $longitude
* @param $location_name
* @param mixed $payload
*/
public function __construct($latitude, $longitude, $location_name = null, $payload = null)
{
parent::__construct($latitude, $longitude, $payload);
$this->location_name = $location_name;
}
/**
* @param string $latitude
* @param string $longitude
* @param $location_name
* @return CustomLocation
*/
public static function create($latitude, $longitude, $location_name = null)
{
return new self($latitude, $longitude, $location_name);
}
/**
* @return string
*/
public function getLocationName()
{
return $this->location_name;
}
/**
* Get the instance as a web accessible array.
* This will be used within the WebDriver.
*
* @return array
*/
public function toWebDriver()
{
return [
'type' => 'location',
'latitude' => $this->latitude,
'longitude' => $this->longitude,
'location_name' => $this->location_name,
];
}
}
<?php
/**
* Created by PhpStorm.
* User: hafiq
* Date: 18/03/2018
* Time: 11:59 PM
*/
namespace App\Conversation\Custom;
use BotMan\BotMan\Messages\Attachments\Attachment;
class MixFile extends Attachment
{
/**
* Pattern that messages use to identify image uploads.
*/
const PATTERN = '%%%_MIX_FILING_%%%';
/** @var string */
protected $url;
protected $file;
/** @var string */
protected $title;
/**
* Video constructor.
* @param $file
* @param string $url
* @param mixed $payload
*/
public function __construct($file, $url, $payload = null)
{
parent::__construct($payload);
$this->file = $file;
$this->url = $url;
}
/**
* @return string
*/
public function getFile()
{
return $this->file;
}
/**
* @param string $file
*/
public function setFile(string $file)
{
$this->file = $file;
}
/**
* @param $url
* @return MixFile
*/
public function url($url)
{
$this->url = $url;
return $this;
}
/**
* @return string
*/
public function getUrl()
{
return $this->url;
}
/**
* @param $title
* @return MixFile
*/
public function title($title)
{
$this->title = $title;
return $this;
}
/**
* @return string
*/
public function getTitle()
{
return $this->title;
}
/**
* Get the instance as a web accessible array.
* This will be used within the WebDriver.
*
* @return array
*/
public function toWebDriver()
{
return [
'type' => 'mix',
'url' => $this->url,
'file_type' => $this->file ? $this->file->getMimeType() : null,
'title' => $this->title,
];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment