Skip to content

Instantly share code, notes, and snippets.

@cvega93
Last active February 11, 2021 16:49
Show Gist options
  • Save cvega93/22d8af6a675f488af68b6c0a18f3fc01 to your computer and use it in GitHub Desktop.
Save cvega93/22d8af6a675f488af68b6c0a18f3fc01 to your computer and use it in GitHub Desktop.
Send Whatsapp with Messagebird & ActiveCampaign Example - PHP
<?php
namespace App\Http\Services\User;
//Service Base
use App\Http\Services\ServiceBase;
use Illuminate\Support\Facades\Log;
/**
* Handles complex login and uses the model repo to connect to the db
*/
class MessageBirdService extends ServiceBase
{
public function __construct()
{
}
public function interviewResult($to, $name) // EJEMPLO DE COMO MANDAR EL MENSAJE
{
$params = array(
0 => ['default' => $name]
);
$fixed_number = str_replace(' ', '', $to);
return $this->sendMessage($fixed_number, 'user_admitted', 'es', $params);
}
public function sendMessage($to, $templateName, $lang, $params)
{
// return 'holi';
$accessKey = "XXXX";
$channel_id = "XXXX";
$report_url = "XXXXXX"; // ACA RECIBES UN WEBHOOK CON EL STATUS DEL ENVÍO DEL MENSAJE
$nameSpace = "XXXXXXX";
if (!config('services.messagebird.enabled')) {
return 'ok';
}
$content_array = array(
'type' => 'hsm',
'to' => $to,
'channelId' => $channel_id,
"reportUrl" => $report_url,
'content' =>
array(
'hsm' =>
array(
'namespace' => $nameSpace,
'templateName' => $templateName,
'language' =>
array(
'policy' => 'deterministic',
'code' => $lang,
),
'params' => $params
),
),
);
$parsedContent = json_encode($content_array);
$curl = "curl -X POST 'https://conversations.messagebird.com/v1/conversations/start' -H 'Authorization: AccessKey $accessKey' -H 'Content-Type: application/json' -d '$parsedContent'";
// Log::info($curl);
$res = shell_exec($curl);
\App\Entities\MessagebirdLog::create(['response' => $res]);
return $res;
}
}
// CON ESTO CREAS LA INTERFÁZ PARA INTEGRARLO A ACTIVE CAMPAIGN.
// LA VARIABLE 1 ES EL NOMBRE, MUY IMPORTANTE, LUEGO LOS OTRAS VARIABLES SON LAS POSUCIONES DEL ARRAY.
public function sendTemplateMessagebird()
{
request()->merge(['password' => config('services.messagebird.local_key')]); // TRICK PARA VALIDAR UN TOKEN PARA MÁS SEGURIDAD JE
request()->validate([
'key' => 'required|same:password',
'contact.phone' => 'required|string|max:20',
'template_name' => 'required|string|max:100',
'contact.first_name' => 'string|max:50',
'params' => 'array',
]);
$to = str_replace(['+', ' '], '', request()->input('contact.phone'));
$templateName = request()->input('template_name');
$params = request()->input('params');
$parsed_params = [
['default' => request()->input('contact.first_name')] // LA POSICION 1 SIEMPRE ES EL NOMBRE CREAR LAS PLANTILLAS CONSIDERANDO ESTO
];
if ($params) {
foreach($params as $param) {
$parsed_params[] = ['default' => $param];
}
}
return $this->messageBirdService->sendMessage($to, $templateName, 'es', $parsed_params);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment