I'm trying to create a card with my webhook service. my website is in php but I don't know how to respond to api.ai in a way to display my result in card format on client's phone. I ask my question with more detail in here.
Considering, you know receiving and checking action from Api.ai request in php
For responding with the card, you can use this:
$request== file_get_contents("php://input");
$messages=[];
// Building Card
array_push($messages, array(
"type"=> "basic_card",
"platform"=> "google",
"title"=> "Card title",
"subtitle"=> "card subtitle",
"image"=>[
"url"=>'http://image-url',
"accessibility_text"=>'image-alt'
],
"formattedText"=> 'Text for card',
"buttons"=> [
[
"title"=> "Button title",
"openUrlAction"=> [
"url"=> "http://url redirect for button"
]
]
]
)
);
// Adding simple response (mandatory)
array_push($messages, array(
"type"=> "simple_response",
"platform"=> "google",
"textToSpeech"=> "Here is speech and additional msg for card"
)
);
$response=array(
"source" => $request["result"]["source"],
"speech" => "Speech for response",
"messages" => $messages,
"contextOut" => array()
);
json_encode($response);
Make sure you don't push more than one card and have 'simple response' with it.
Related
I connect to the online api and to the payment api using the official PHP SDK.
I want to create invoices and pay for them by clients on my website.
I figured out the documentation and already made the connection and all requests, but when I create an invoice and payment, and then charge money from the client's card, this charge transaction is not automatically linked to the payment, as it is said in the documentation. Instead, I get an alert in the quickbooks dashboard.
Below, I will attach examples of my code how I create: invoice, payment for invoice and card charge:
$amount = 0.5;
$invoice_add = Invoice::create([
"Line"=>[
"DetailType"=>"SalesItemLineDetail",
"Amount"=>$amount,
"SalesItemLineDetail"=>[
"ItemRef"=>[
"value"=>4
],
"UnitPrice"=>$amount,
"Qty" => 1
]
],
"CustomerRef"=>[
"value"=>"".$payment_account->profile_id //customer id
],
"BillEmail" => [
"Address" => $pharmacy->email
],
"AllowOnlineCreditCardPayment"=> true,
"AllowOnlineACHPayment"=> true
]);
$res_invoice = $dataService->Add($invoice_add);
//$res_invoice = $dataService->SendEmail($res_invoice,$pharmacy->email);
.
$payment_add = Payment::create([
"TotalAmt"=>$amount,
"UnappliedAmt"=>$amount,
"PaymentMethodRef"=>[
"value"=>"5"
],
"Line"=>[
"LinkedTxn"=>[
"TxnId"=>$res_invoice->Id,
"TxnType"=>"Invoice"
],
"Amount"=>$amount
],
"CustomerRef"=>[
"value"=>"".$payment_account->profile_id //customer id
],
"ProcessPayment"=>"true",
"PaymentType"=>"CreditCard",
"CreditCardPayment"=> [
"CCDetail"=> [
"ProcessPayment"=> "true",
]
],
"TxnSource"=>"IntuitPayment"
]);
$res_payment = $dataService->Add($payment_add);
.
$array0 = [
"amount" => $amount,
"currency" => "USD",
"cardOnFile" => $payment_account->payment_profile_id, //customer default card id
"context" => [
"mobile" => "false",
"isEcommerce" => "false"
]
];
$charge = new Charge($array0);
$response = $client->charge($charge);
if($response->failed()){
$errorMessage = $response->getBody();
$error.= $errorMessage;
dd($errorMessage);
} else {
$charge_res = $response->getBody();
}
Maybe someone faced such a problem, or you will see that I'm doing something wrong?
I am using the MPESA-Express also called the STK push api V1 to receive payments from my clients.
To get the customer paying, I am looking for the PhoneNumber value in the results body of the response if the payment is successful. This way I can associate a payment with a customer.
However now that we'll be having data minimisation on the MPesa api, the PhoneNumber will not be displayed fully, and I am facing a challenge of how to associate a payment transaction with a client. I have tried setting the AcccountReference in the request as shown below, but I can't get this AccountReference back in the response results body. I was thinking of setting a unique AcccountReference for each customer.
The data I am sending to the endpoint https://sandbox.safaricom.co.ke/mpesa/stkpush/v1/processrequest
$postData = json_encode([
"BusinessShortCode" => Yii::$app->params['businessShortCode'],
"Password" => $this->createMpesaRequestsPassword($timestamp),
"Timestamp" => $timestamp,
"TransactionType" => $transactionType,
"Amount" => $amount,
"PartyA" => $phoneNumber,
"PartyB" => Yii::$app->params['businessShortCode'],
"PhoneNumber" => $phoneNumber,
"CallBackURL" => $callBackUrl,
"AccountReference" => $phoneNumber,
"TransactionDesc" => $transactionDesc
]);
On my callback url I get this response:
{
"Body": {
"stkCallback": {
"MerchantRequestID": "9183-42212949-1",
"CheckoutRequestID": "ws_CO_23072022133552132714385056",
"ResultCode": 0,
"ResultDesc": "The service request is processed successfully.",
"CallbackMetadata": {
"Item": [
{
"Name": "Amount",
"Value": 1
},
{
"Name": "MpesaReceiptNumber",
"Value": "QGN2XSH6MQ"
},
{
"Name": "Balance"
},
{
"Name": "TransactionDate",
"Value": 20220723133617
},
{
"Name": "PhoneNumber",
"Value": 254711111111
}
]
}
}
}
}
How do know which transaction belongs to which user?
Working off of the 'Send and email' example:
https://github.com/microsoftgraph/msgraph-sdk-php/wiki/Example-calls#send-an-email
I'm trying to set the 'from name' of the email in the header of the sent email to show 'TEST' instead of the name associated with my OWA account.
$graph = new Graph();
$graph->setAccessToken(access_token);
$mailBody = array( "Message" => array(
"subject" => $subject . ' ' . $from_name,
"body" => array(
"contentType" => "html",
"content" => $body
),
"sender" => array(
"emailAddress" => array(
"name" => "TEST",
"address" => $from_user->email
)
),
"from" => array(
"emailAddress" => array(
"name" => 'TEST',
"address" => $from_user->email
)
),
"toRecipients" => array(
array(
"emailAddress" => array(
"name" => $to_user->name,
"address" => $to_user->email
)
)
)
)
);
$response = $graph->createRequest("POST", "/me/sendMail")
->attachBody($mailBody)
->execute();
But the message header always has the name from my OWA (outlook) account instead.
AFAIK it is not supported to override name property of emailAddress resource via senMail endpoint.
But Microsoft Graph supports the feature which allows a user to send mail that appears to be sent from another user, distribution list, group, resource, or shared mailbox (official documentation)
Two mailbox permissions affect how a message is sent:
Send on Behalf
Send As
Below is demonstrated on how to sent mail from distribution group called Finance Department
POST https://graph.microsoft.com/v1.0/me/sendMail
{
"message": {
"subject": "Finance results",
"body": {
"contentType": "text",
"content": "Some finance results goes here..."
},
"toRecipients": [
{
"emailAddress": {
"address": "mary#contoso.onmicrosoft.com"
}
}
],
"from": {
"emailAddress": {
"address": "financedeplist#contoso.onmicrosoft.com"
}
}
}
}
Current user: Jon Doe (jdoe#contoso.onmicrosoft.com)
Option 1. Send on Behalf
Option 2. Send As
I am trying to send notification using php to Android Application, and which is working fine without sound. I am receiving the notification both foreground and background as expected.
Here is the PHP code,
<?php
$token = $_GET['token'];
$action = $_GET['action'];
$msgTitle = $_GET['msgTitle'];
$msgDescription = $_GET['msgDescription'];
$notificationTitle = $_GET['notificationTitle'];
require './google-api-php-client-2.2.2/vendor/autoload.php';
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setAuthConfig('./testPrjoectAPP-firebase-adminsdk-9hn21-22c1b3f426.json');
$client->addScope('https://www.googleapis.com/auth/firebase.messaging');
$httpClient = $client->authorize();
$project = "testPrjoectAPP";
$message = [
"message" => [
"notification" => [
"body" => "Message FCM",
"title" => $notificationTitle
],
"token" => $token,
"data" => [
"action" => $action,
"msgTitle" => $msgTitle,
"msgDescription" => $msgDescription
]
]
];
$response = $httpClient->post("https://fcm.googleapis.com/v1/projects/{$project}/messages:send", ['json' => $message]);
echo$response->getReasonPhrase(); // OK
?>
But when I add sound parameter to notification payload and execute php I am getting Bad Request error from php.
.
$message = [
"message" => [
"notification" => [
"body" => "Message FCM",
"title" => $notificationTitle,
"sound" => "default"
],
// Send with token is not working
"token" => $token,
"data" => [
"action" => $action,
"msgTitle" => $msgTitle,
"msgDescription" => $msgDescription
]
]
];
Edit
Here is the error message I got while printing with
data: "{\n \"error\": {\n \"code\": 400,\n \"message\": \"Invalid JSON payload received. Unknown name \\\"sound\\\" at 'message.notification': Cannot find field.\",\n \"status\": \"INVALID_ARGUMENT\",\n \"details\": [\n {\n \"#type\": \"type.googleapis.com/google.rpc.BadRequest\",\n \"fieldViolations\": [\n {\n \"field\": \"message.notification\",\n \"description\": \"Invalid JSON payload received. Unknown name \\\"sound\\\" at 'message.notification': Cannot find field.\"\n }\n ]\n }\n ]\n }\n}\n"
As per my comment, You have to use your JSON like following way.
Solution:
The appearance of message in your JSON indicates you are using the HTTP v1 API. The documentation you linked is for the legacy API.
The HTTP v1 API JSON to send a notification with sound for Android and iOS devices should be:
{
"message":{
"token":"your-token-value",
"notification":{
"title":"Test",
"body":"Test message from server"
},
"android":{
"notification":{
"sound":"default"
}
},
"apns":{
"payload":{
"sound":"default"
}
}
}
}
Reference Link is: Unable to add sound to notification payload
Thank you.
Try this:
{
"message":{
"token":"your-token-value",
"notification":{
"title":"Test",
"body":"Test message from server"
},
"android":{
"notification":{
"sound":"default"
}
},
"apns":{
"payload":{
"sound":"default"
}
}
}
}
I am having trouble getting any data out of a mashape API, I have made the UNIREST POST but I am unable to echo the data back to myself, this is what the call should return;
{
"result": {
"name": "The Elder Scrolls V: Skyrim",
"score": "92",
"rlsdate": "2011-11-11",
"genre": "Role-Playing",
"rating": "M",
"platform": "PlayStation 3",
"publisher": "Bethesda Softworks",
"developer": "Bethesda Game Studios",
"url": "http://www.metacritic.com/game/playstation-3/the-elder-scrolls-v-skyrim"
}
}
And my code...
require_once 'MYDIR/mashape/unirest-php/lib/Unirest.php';
$response = Unirest::post(
"https://byroredux-metacritic.p.mashape.com/find/game",
array(
"X-Mashape-Authorization" => "MY API AUTH CODE"
),
array(
"title" => "The Elder Scrolls V: Skyrim",
"platform" => undefined
)
);
echo "$response->body->name";
?>
Can anyone suggest how I can get it to echo the "name".
Any help would be appreciated :)
It appears the way you are trying to display the body is preventing you from seeing an error in the response telling you that the property platform is required to be a number.
Try using json_encode($response->body) instead to see the full response:
<?php
require_once 'lib/Unirest.php';
$response = Unirest::post(
"https://byroredux-metacritic.p.mashape.com/find/game",
array(
"X-Mashape-Authorization" => "-- your authorization key goes here --"
),
array(
"title" => "The Elder Scrolls V: Skyrim",
"platform" => 1 # try placing undefined here.
)
);
echo json_encode($response->body);
?>
Once you've gotten the response you can then use $response->body->result->name:
$result = $response->body->result;
echo "{$result->name} has a score of [{$result->score}]";