Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
SocolaDaiCa committed Feb 5, 2018
1 parent db8fbfc commit 538b88b
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 43 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/vendor/
test.php
/test/
composer.lock
95 changes: 95 additions & 0 deletions demo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?php
// require_once __DIR__ . '/' . '../src/socola/Chatfuel.php';
// // Chatfuel::sendText("ádas");
// $bot = new Chatfuel();


// print_r($_SERVER);
require_once __DIR__ . '/' . 'vendor/autoload.php';
use Socola\Chatfuel;
## Create bot
$bot = new Chatfuel();

## Send text
$text = 'Socola';
$texts = [$text, $text, $text, $text];
$bot->sendText($text);
$bot->sendText($texts);

## Send image
$image = 'http://i.imgur.com/luWlRwV.jpg';
$images = [
'http://i.imgur.com/luWlRwV.jpg',
'http://i.imgur.com/luWlRwV.jpg'
];
$bot->sendImage($image);
$bot->sendImage($images);

## Send file
$file = 'https://01b02091.ngrok.io/test.pdf';
$files = array(
'https://01b02091.ngrok.io/test.pdf',
'https://01b02091.ngrok.io/test.pdf'
);
$bot->sendFile($file);
$bot->sendFile($files);

## Send audio
$audio = 'https://01b02091.ngrok.io/test.mp3';
$audios = [
'https://01b02091.ngrok.io/test.mp3',
'https://01b02091.ngrok.io/test.mp3'
];
$bot->sendAudio($audio);
$bot->sendAudio($audios);

## Create a button
### Create Button To URL
$title = "button to url";
$url = "http://www.facebook.com";
$buttonToURL = $bot->createButtonToURL($title, $url, Null);

### Create Button To Block
$title = "button to block";
$block = "re-start";
$buttonToBlock = $bot->createButtonToBlock($title, $block, Null);

### Create Button Share
$buttonShare = $bot->createButtonShare();

### Create Button Call
$phoneNumber = '096******5';
$buttonCall = $bot->createButtonCall($phoneNumber, 'Call');

### Create Button Quick Reply
$block = 're-start';
$blocks = [
'play',
'pause'
];
$bot->createButtonQuickReply($title, $block);
$bot->createButtonQuickReply($title, $blocks);


### Send a text card with one or more button (max 3 buttons)
$text = 'this is text card';
$buttons = [
$buttonToURL,
$buttonToBlock,
$buttonShare
];
$bot->sendTextCard($text, $buttonToURL);
$bot->sendTextCard($text, $buttons);


### Create element
$title = 'this is element';
$image = 'http://i.imgur.com/luWlRwV.jpg';
$subTitle = 'this is sub title';
$element = $bot->createElement($title, $image, $subTitle, $buttonToURL);
$elements = [$element, $element];
$bot->sendGallery($element);
### Send a list template min 2 element
$topElementStyle = 'large';
$bot->sendList($elements, $topElementStyle);
?>
85 changes: 42 additions & 43 deletions src/socola/Chatfuel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,32 @@
/**
* http://docs.chatfuel.com/plugins/plugin-documentation/json-api
*/
// interface ChatfuelInterface{
// public function sendText($text_or_arrayTexts);
// public function sendImage($image_or_arrayImages);
// public function sendFile($file_or_arrayFiles);
// public function sendVideo($url_or_arrayUrls);
// public function sendAudio($audio_or_arrayAudios);

// public function createButtonToURL($title, $url, $setAttributes = NULL);
// public function createButtonToBlock($title, $block_or_arrayBlocks, $setAttributes = NULL);
// public function createButtonShare();
// public function createButtonCall($phoneNumber, $title = 'Call');
// public function createButtonQuickReply($title, $block_or_arrayBlocks);

// public function sendTextCard($text, $buttons_or_arrayButtons);
// public function createElement($title, $image, $subTitle, $buttons);
// public function sendGallery($element_or_arrayElement);
// public function sendList($arrayElements, $topElementStyle = 'large');
// public function isURL($url);
// }
namespace Socola;
class Chatfuel/* implements ChatfuelInterface*/
interface ChatfuelInterface{
public function sendText($text_or_arrayTexts);
public function sendImage($image_or_arrayImages);
public function sendFile($file_or_arrayFiles);
public function sendVideo($url_or_arrayUrls);
public function sendAudio($audio_or_arrayAudios);

public function createButtonToURL($title, $url, $setAttributes = NULL);
public function createButtonToBlock($title, $block_or_arrayBlocks, $setAttributes = NULL);
public function createButtonShare();
public function createButtonCall($phoneNumber, $title = 'Call');
public function createButtonQuickReply($title, $block_or_arrayBlocks);

public function sendTextCard($text, $buttons_or_arrayButtons);
public function createElement($title, $image, $subTitle, $buttons);
public function sendGallery($element_or_arrayElement);
public function sendList($arrayElements, $topElementStyle = 'large');
public function isURL($url);
}
class Chatfuel implements ChatfuelInterface
{
private $debug;
public $messages;
public function __construct($debug = false)
protected $debug;
protected $messages;
function __construct($debug = false)
{
// header('Content-Type: application/json; charset=UTF-8');
$this->debug = $debug;
}

Expand All @@ -50,27 +49,27 @@ public function sendText($messages = null){

public function sendImage($url)
{
$this->sendMedia('image', $url);
self::sendMedia('image', $url);
}

public function sendFile($url)
{
$this->sendMedia('file', $url);
self::sendMedia('file', $url);
}

public function sendVideo($url)
{
$this->sendMedia('video', $url);
self::sendMedia('video', $url);
}

public function sendAudio($url)
{
$this->sendMedia('audio', $url);
self::sendMedia('audio', $url);
}

public function createButtonToURL($title, $url, $setAttributes = NULL)
{
if ($this->isURL($url)) {
if (self::isURL($url)) {
return null;
}

Expand Down Expand Up @@ -146,7 +145,7 @@ public function sendTextCard($text, $buttons)
if(empty($buttons[0]['title']))
$buttons = [$buttons];

$this->sendAttachment('template', [
self::sendAttachment('template', [
'template_type' => 'button',
'text' => $text,
'buttons' => $buttons
Expand All @@ -158,7 +157,7 @@ public function createElement($title, $image, $subTitle, $buttons)
if(empty($buttons[0]['type']))
$buttons = [$buttons];

if ($this->isURL($image) && is_array($buttons)) {
if (self::isURL($image) && is_array($buttons)) {
return array(
'title' => $title,
'image_url' => $image,
Expand All @@ -175,7 +174,7 @@ public function sendGallery($elements)
if(empty($elements[0]['title']))
return FALSE;

$this->sendAttachment('template', [
self::sendAttachment('template', [
'template_type' => 'generic',
'elements' => $elements
]);
Expand All @@ -186,16 +185,16 @@ public function sendList($elements, $topElementStyle = 'large')
if(empty($elements[0]['title']))
$elements = array($elements);

$this->sendAttachment('template', [
self::sendAttachment('template', [
'template_type' => 'list',
'top_element_style' => $topElementStyle,
'elements' => $elements
]);
}

public function __destruct()
function __destruct()
{
$this->sentMessages();
self::sentMessages();
}

public function isURL($url){
Expand All @@ -209,10 +208,10 @@ public function sendMedia($type, $url)
}

foreach ($url as $value) {
if ($this->isURL($value)) {
$this->sendAttachment($type, ['url' => $value]);
if (self::isURL($value)) {
self::sendAttachment($type, ['url' => $value]);
} else {
$this->sendText('Error: Invalid URL!');
self::sendText("Error: Invalid {$type} URL!");
}
}
}
Expand All @@ -235,19 +234,19 @@ private function sendAttachment($type, $payload){
)
);
} else {
$this->sendText('Error: Invalid type!');
self::sendText('Error: Invalid type!');
}
}

public function sentMessages()
{
if(is_null($this->messages))
$this->sendText(NULL);

self::sendText(NULL);
header('Content-Type: application/json; charset=UTF-8');
if($this->debug){
echo json_encode($this, JSON_PRETTY_PRINT);
echo json_encode(['messages' => $this->messages], JSON_PRETTY_PRINT);
}else{
echo json_encode($this);
echo json_encode(['messages' => $this->messages]);
}
}

Expand Down

0 comments on commit 538b88b

Please sign in to comment.