PHP code example of socheatsok78 / manychat-block-sdk

1. Go to this page and download the library: Download socheatsok78/manychat-block-sdk library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

socheatsok78 / manychat-block-sdk example snippets


namespace App\Http\Controllers\Flow;

use ManyChat\Dynamic\Chat;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class WelcomeMessage extends Controller
{
    /**
     * Handle the incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function __invoke(Request $request)
    {
        $chat = new Chat();

        $text = new Text('Welcome to ManyChat Dynamic Response');
        $chat->reply($text);

        return $chat;
    }
}

use ManyChat\Dynamic\Messages\Text;

$text = new Text('Example text message');

# or

$text = Text::create('Example text message');

use ManyChat\Dynamic\Messages\Element;
use ManyChat\Dynamic\Messages\LargeList;
use ManyChat\Dynamic\Messages\CompactList;

# You need to create at lease 2 Element block
$element_1 = new Element([/* ... */]);
$element_2 = new Element([/* ... */]);

$compactList = new CompactList([$element_1, $element_2]);
$largeList = new LargeList([$element_1, $element_2]);

use ManyChat\Dynamic\Messages\Element;
use ManyChat\Dynamic\Messages\SquareCard;
use ManyChat\Dynamic\Messages\HorizontalCard;

# You need to create at lease 2 Element block
$element_1 = new Element([/* ... */]);
$element_2 = new Element([/* ... */]);

$horizontalCard = new HorizontalCard([$element_1, $element_2]);
$squareCard = new SquareCard([$element_1, $element_2]);

use ManyChat\Dynamic\Messages\Element;

$element = new Element();
$element->title = 'Unsplash';
$element->subTitle = 'Photos for everyone';
$element->imageUrl = 'https://source.unsplash.com/random';
$element->actionUrl = 'https://unsplash.com';

# or

$element = new Element([
    'title' => 'Unsplash',
    'subTitle' => 'Photos for everyone',
    'imageUrl' => 'https://source.unsplash.com/random',
    'actionUrl' => 'https://unsplash.com',
]);

use ManyChat\Dynamic\Attachments\File;

$file = new File('/* URL to the file */');

# or

$file = File::url('/* URL to the file */');

use ManyChat\Dynamic\Attachments\Image;

$image = new Image('https://source.unsplash.com/random');

# or

$image = Image::url('https://source.unsplash.com/random');

use ManyChat\Dynamic\Attachments\Audio;

$audio = new Audio('/* URL to the audio file */');

# or

$audio = Audio::url('/* URL to the audio file */');

use ManyChat\Dynamic\Attachments\Video;

$video = new Video('/* URL to the video file */');

# or

$video = Video::url('/* URL to the video file */');

use ManyChat\Dynamic\Buttons\Call;

$call = new Call('+123456789');

# or

$call = Call::phone('+123456789');

use ManyChat\Dynamic\Buttons\Url;

$url = new Url('https://example.com');

# or

$url = Url::create('https://example.com');

use ManyChat\Dynamic\Buttons\Url;

$url = new Url('https://example.com');

$url->full()     # 100 %, Default
    ->medium()   # 75 %
    ->compact(); # 50 %

use ManyChat\Dynamic\Buttons\Buy;

$buy = new Buy('T-Shirt', 2000);

# or

$buy = Buy::create('T-Shirt', 2000);

use ManyChat\Dynamic\Buttons\Buy;
use ManyChat\Dynamic\Foundation\Customer;
use ManyChat\Dynamic\Foundation\Product;

$buy = new Buy();

# Update Product name and price
$buy->withProduct(function (Product $product) {
    $product->name = 'T-Shirt';
    $product->price = 2000;
});

# Update Customer ntact name
        ->withoutContactPhone()        # Not 

use ManyChat\Dynamic\Buttons\Node;

$node = new Node('Welcome Message');

# or

$node = Node::create('Welcome Message');

use ManyChat\Dynamic\Buttons\Flow;

$flow = new Flow('content20180221085508_278589');

# or

$flow = Flow::create('content20180221085508_278589');

$object->addTag('tag_name');

# or add multiple tags

$object->addTags(['tag_1', 'tag_2']);

$object->removeTag('tag_name');

# or remove multiple tags

$object->removeTags(['tag_1', 'tag_2']);

$object->addField('Example_Field_1', 'value');

# or add multiple custom fields

$object->addFields([
    'Example_Field_1' => 'value',
    'Example_Field_2' => 'value'
]);

$object->removeField('Example_Field_1');

# or add multiple custom fields

$object->removeFields(['Example_Field_1', 'Example_Field_2']);

use ManyChat\Dynamic\Chat;
use ManyChat\Dynamic\Buttons\Node;
use ManyChat\Dynamic\Buttons\Flow;

$chat = new Chat();

$node = new Node('Welcome Message');
$flow = new Flow('content20180221085508_278589');
$dynamic = new DynamicBlock('https://example.com/api');

$chat->quickReply($node);
$chat->quickReply($flow);
$chat->quickReply($dynamic);

use ManyChat\Dynamic\Callback\DynamicBlock;

$dynamic = new DynamicBlock('https://example.com/api');

# Set Block caption
$dynamic->setCaption('Custom Caption');

# Add HTTP request header
$dynamic->setHeader('x-header', 'value');
$dynamic->setHeaders([
    'x-header-2' => 'value',
    'x-header-2' => 'value'
]);

# Add HTTP request payload
$dynamic->setPayload('key', 'value');
$dynamic->setPayloads([
    'key-1' => 'value',
    'key-2' => 'value'
]);

use ManyChat\Dynamic\Chat;
use ManyChat\Dynamic\Callback\ExternalCallback;

$chat = new Chat();

$external = new ExternalCallback('https://example.com/api/flow/2');

# Add HTTP request header
$external->setHeader('x-header', 'value');
$external->setHeaders([
    'x-header-2' => 'value',
    'x-header-2' => 'value'
]);

# Add HTTP request payload
$external->setPayload('key', 'value');
$external->setPayloads([
    'key-1' => 'value',
    'key-2' => 'value'
]);

$chat->callback($external);