jaime / whatsapp-gupshup
WhatsApp Gupshup PHP 库
dev-main
2022-11-27 13:44 UTC
Requires (Dev)
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-09-27 18:27:30 UTC
README
安装
您可以通过composer安装此包
composer require jaime/whatsapp-gupshup
使用
出站消息
$gupshup = new OutboundMessage('SRC_NAME', 'SOURCE', 'API_KEY');
发送文本
$gupshup->setText('Texto de prueba'); $gupshup->sendRequest('573111111111');
发送图片
$url = "https://www.buildquickbots.com/whatsapp/media/sample/jpg/sample01.jpg"; $caption = "Sample image"; $gupshup->setImage($url, $caption); $gupshup->sendRequest('573111111111');
发送音频
$url = "https://www.buildquickbots.com/whatsapp/media/sample/jpg/sample01.jpg"; $gupshup->setAudio($url); $gupshup->sendRequest('573111111111');
发送文件
$url = "https://www.buildquickbots.com/whatsapp/media/sample/pdf/sample01.pdf"; $filename = "Sample funtional resume"; $gupshup->setFile($url, $filename); $gupshup->sendRequest('573111111111');
发送视频
$url = "https://www.buildquickbots.com/whatsapp/media/sample/video/sample01.mp4"; $caption = "Sample video"; $gupshup->setFile($url, $filename); $gupshup->sendRequest('573111111111');
发送贴纸
$url = "http://www.buildquickbots.com/whatsapp/stickers/SampleSticker01.webp"; $gupshup->setFile($url, $filename); $gupshup->sendRequest('573111111111');
发送列表消息
$globalButtons[] = [ 'type' => 'text', 'title' => 'Escoger' ]; $items[] = [ 'title' => 'first Section', 'subtitle' => 'first Subtitle', 'options' => [ [ 'type' => 'text', 'title' => 'section 1 row 1', 'description' => 'first row of 1 section description', 'postbackText' => 'section 1 row 1 postback payload' ], [ 'type' => 'text', 'title' => 'section 1 row 2', 'description' => 'second row of 2 section description', 'postbackText' => 'section 1 row 2 postback payload' ], [ 'type' => 'text', 'title' => 'section 1 row 3', 'description' => 'second row of 3 section description', 'postbackText' => 'section 1 row 3 postback payload' ] ] ]; $items[] = [ 'title' => 'Segunda Sección', 'subtitle' => 'Segundo Subtitulo', 'options' => [ [ 'type' => 'text', 'title' => 'section 2 row 1', 'description' => 'first row of 1 section description', 'postbackText' => 'section 1 row 1 postback payload' ], [ 'type' => 'text', 'title' => 'section 2 row 2', 'description' => 'second row of 2 section description', 'postbackText' => 'section 1 row 2 postback payload' ], [ 'type' => 'text', 'title' => 'section 2 row 3', 'description' => 'second row of 3 section description', 'postbackText' => 'section 1 row 3 postback payload' ] ] ]; // Params: $title, $body, $msgid, $globalButtons, $items $gupshup->setListMessage('title text', 'body text', rand(), $globalButtons, $items); $gupshup->sendRequest("57311111111");
发送快速回复
$content = [ 'type' => 'text', 'header' => 'this is the header', 'text' => 'this is the body', 'caption' => 'this is the footer' ]; $options = [ [ 'type' => 'text', 'title' => 'Firts', ], [ 'type' => 'text', 'title' => 'Second', ], [ 'type' => 'text', 'title' => 'Third', ] ]; $msgid = rand(); $gupshup->setQuickRepliesText($msgid, $content, $options); $gupshup->sendRequest("57311111111");
发送快速回复文本
$content = [ 'type' => 'text', 'header' => 'this is the header', 'text' => 'this is the body', 'caption' => 'this is the footer' ]; $options = [ [ 'type' => 'text', 'title' => 'Firts', ], [ 'type' => 'text', 'title' => 'Second', ], [ 'type' => 'text', 'title' => 'Third', ] ]; $msgid = rand(); $gupshup->setQuickRepliesText($msgid, $content, $options); $gupshup->sendRequest("57311111111");
获取模板列表
$templates = $gupshup->getTemplates();
通过模板发送消息
$idtemplate = 'aaaaa-bbbbb-ccccc-dddd-eeee'; $templateparams = [ "Agent", "Local Address", "Tracking code" ]; $gupshup->setTemplate($idtemplate, $templateparams); $gupshup->sendTemplate("57311111111");
获取订阅用户列表
$response = $gupshup->getOptin();
标记用户订阅/退订
$response = $gupshup->markOpt('573111111111', 'in'); $response = $gupshup->markOpt('573111111111', 'out');
检查钱包余额
$response = $gupshup->getWalletBalance();
入站消息和事件
<?php require('../vendor/autoload.php'); use Jaime\WhatsappGupshup\InboundMessageandEvents; $log = json_decode(file_get_contents('php://input'), true); $inboundGupshup = new InboundMessageandEvents($log); switch ($inboundGupshup->getTypeNotification()) { case 'user-event': // code .. break; case 'message-event': if ($inboundGupshup->getTypePayload() == 'failed') { $logfailed = $inboundGupshup->getReasonFailedMessageEvent(); // code .. file_put_contents('log-failed', '(' . date('Y-m-d H:i:s') . ') ' . print_r($logfailed, true) . PHP_EOL, FILE_APPEND | LOCK_EX); } break; default: # code... break; } http_response_code(200);