How to post a new context to my wit bot? - laravel

I'm writing a bot using Wit.ai and I'm having trouble understanding context. More specifically how/when to set it. As I've understood it you set the context by posting to the API at any point in time you see fit, for example when executing a function defined in a wit story. At least that's the approach I'm aiming for with the code below. Sadly it generates an error though so I wonder, am I trying to set the context in an incorrect way or is there something wrong with my Guzzle post?
private function storeUserName($entities) {
$witcall = $this->wit->post($this->wit_base_url.'/converse',
[
'form_params' => [
'username' => $entities->contact[0]->value
],
'query' => [
'v' => '1',
'session_id' => 'vk-'.$this->thread_id
],
'headers' => [
'Authorization' => 'Bearer '.env('WIT_TOKEN', false)
]
]
);
return [
'msg' => 'nice',
'type' => 'msg'
];
}
My error:
ClientException in RequestException.php line 111:
Client error: `POST https://api.wit.ai/converse?v=1&session_id=vk-1` resulted in a `400 Bad Request` response:
Unable to parse context in body

As it turned out using form_params was incorrect, changed to json and now it works fine. Like this:
$call = $this->wit->request('POST', 'converse', [
'json' => $this->context,
'query' => [
'v' => '1',
'session_id' => 'vk-'.$this->thread_id
]
]);

Related

Paypal API Subscription Create returns 400 Bad Request response "name", though request is formed as in documentation

I try to implement Paypal subscription service according to: https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_create
This is my first try.
In sandbox business account I have created two test subscriptions: monthly and yearly and configured application with their id's.
This is the method:
public function createSubscription($planSlug, $name, $email) {
return $this->makeRequest(
'POST',
'v1/billing/subscriptions',
[],
[
'plan_id' => $this->plans[$planSlug],
'subscriber' => [
'name' => [
'given_name' => $name,
],
'email_address' => $email
],
'application_context'=> [
'brand_name' => config('app.name'),
'shipping_preference' => 'NO_SHIPPING',
'user_action' => 'SUBSCRIBE_NOW',
'return_url' => route('subscribe.approval', ['plan' => $planSlug]),
'cancel_url'=> route('subscribe.cancelled')
],
],
[],
$isJsonRequest = true
);
}
However, when I make a call to API, to create a test subscription, I get weird response that 'name' parameter is formed incorrectly:
php artisan tinker
>>> $paypal = resolve(App\Services\PaypalService::class);
=> App\Services\PaypalService {#3413}
>>> $paypal->createSubscription('monthly', 'Test', 'test#test.com');
GuzzleHttp\Exception\ClientException with message 'Client error: `POST https://api-
m.sandbox.paypal.com/v1/billing/subscriptions` resulted in a `400 Bad Request` response:
{"name":"INVALID_REQUEST","message":"Request is not well-formed, syntactically incorrect, or
violates schema.","debug_id (truncated...)
This is strange, because in Paypal API doc (see above), the 'name' param is described exactly like that!
Do I miss something or it is Paypal API is acting funky?
try this :
try {
// your code here
} catch(\Throwable $th) {
if ($th instanceof ClientException) {
$r = $th->getResponse();
$responseBodyAsString = json_decode($r->getBody()->getContents());
dd($responseBodyAsString);
}
}
I've faced this too before and it was not easy for me to figure out how to show an explicit error message.
'return_url' => route('subscribe.approval', ['plan' => $planSlug]),
'cancel_url'=> route('subscribe.cancelled')
the problem is in this two url, may be you have changed the APP_URL in the .env
APP_URL=http://127.0.0.1:8000
put app url that and try

Not receiving Slack interactive payload

I have set up a notification with action buttons, but I do not receive the in interactive payload. I have configured the webhook URL here:
When I post to that URL in Postman everything is fine. But when I press the buttons I get no callback to that URL I get nothing(I'm logging all requests to that URL). I also get a warning triangle next to my buttons like this:
I get no other feedback and no Ideas where I should debug.
The massage with the buttons is published with Laravel's Slack notification channel plus this package for block support: https://github.com/nathanheffley/laravel-slack-blocks
This is the code for sending the notification, but I don't think it is relevant:
public function toSlack()
{
$model = $this->model;
$message = (new SlackMessage())
->error()
->content('New report!');
$message->attachment(function ($attachment) use ($model) {
$attachment->block(function ($block) {
$block
->type('actions')
->elements([
[
'type' => 'button',
'text' => [
'type' => 'plain_text',
'text' => 'Approve',
'emoji' => false,
],
'value' => 'click_me_123',
'action_id' => 'approve_x',
'style' => 'primary',
],
[
'type' => 'button',
'text' => [
'type' => 'plain_text',
'text' => 'Reject',
'emoji' => false,
],
'value' => 'click_me_1234',
'action_id' => 'reject_x',
'style' => 'danger',
],
]);
});
});
return $message;
}
Any ideas how I should proceed with debugging?
Today, I noticed that I got some more information if I hoovered the triangle, but I'm pretty sure that didn't work yesterday. I got the message "500 server error" there.
The problem was that the payload JOSN is sent as a form-data field and not in the POST request body like normal. Why do they send the payload like this? It makes no sense.

Stripe webhook test returns empty table on execution, but works on stripe's webhook tester

We're having a problem with our test suite.
When we run this using the test suite we get a 'The table is empty...' response from PHPUnit.
We know it works as we've also tested using Stripe's 'Send a web hook' test function which works, and the response is stored as expected.
Our code is here:
public function test_webhook_received()
{
$this->expectsJobs([StoreStripeWebHookJob::class]);
$this->postJson('/stripeHook', [
'created' => 1326853478,
'livemode' => false,
'id' => 'evt_00000000000000',
'type' => 'account.external_account.created',
'object' => 'event',
'request' => NULL,
'pending_webhooks' => 1,
'api_version' => '2019-12-03',
'data' => [
'object' => [
'id' => 'ba_00000000000000',
'object' => 'bank_account',
'account' => 'acct_00000000000000',
'account_holder_name' => 'Jane Austin',
'account_holder_type' => 'individual',
'bank_name' => 'STRIPE TEST BANK',
'country' => 'US',
'currency' => 'gbp',
'fingerprint' => '8JXtPxqbdX5GnmYz',
'last4' => '6789',
'metadata' => [],
'routing_number' => '110000000',
'status' => 'new',
],
],
]);
$this->assertDatabaseHas('stripe_webhooks', [
'stripe_created_at' => 1326853478,
'type' => 'account.external_account.created',
]);
}
The response received is:
Failed asserting that a row in the table [stripe_webhooks] matches the
attributes {
"stripe_created_at": 1326853478,
"type": "account.external_account.created" }.
The table is empty..
If we remove the
$this->expectsJobs([StoreStripeWebHookJob::class]);
tests succeed. Obviously the expectsJob() call should be where it is though.
ExpectsJob also intercepts the job. Much like expectsException. Judging from your clean naming convention "StoreStripe..." - I'd say it's really not storing under these test circumstances.
You'll need to test separately that your endpoint/controller is queuing a job... and that the job is storing the data. 2 tests.

Guzzle Post Null/Empty Values in Laravel

I've been trying to work with Guzzle and learn my way around it, but I'm a bit confused about using a request in conjunction with empty or null values.
For example:
$response = $client->request('POST',
'https://www.testsite.com/coep/public/api/donations', [
'form_params' => [
'client' => [
'web_id' => NULL,
'name' => 'Test Name',
'address1' => '123 E 45th Avenue',
'address2' => 'Ste. 1',
'city' => 'Nowhere',
'state' => 'CO',
'zip' => '80002'
],
'contact' => [],
'donation' => [
'status_id' => 1,
'amount' => $donation->amount,
'balance' => $donation->balance,
'date' => $donation->date,
'group_id' => $group->id,
],
]
]);
After running a test, I found out that 'web_id' completely disappears from my request if set to NULL. My question is how do I ensure that it is kept around on the request to work with my conditionals?
At this point, if I dd the $request->client, all I get back is everything but the web_id. Thanks!
I ran into this issue yesterday and your question is very well ranked on Google. Shame that it has no answer.
The problem here is that form_params uses http_build_query() under the hood and as stated in this user contributed note, null params are not present in the function's output.
I suggest that you pass this information via a JSON body (by using json as key instead of form_params) or via multipart (by using multipart instead of form_params).
Note: those 2 keys are available as constants, respectively GuzzleHttp\RequestOptions::JSON and GuzzleHttp\RequestOptions::MULTIPART
Try to define anything like form_params, headers or base_uri before creating a client, so:
// set options, data, params BEFORE...
$settings = [
'base_uri' => 'api.test',
'headers' => [
'Accept' => 'application/json',
'Authorization' => 'ajustneedatokenheredontworry'
],
'form_params' => [
'cash' => $request->cash,
'reason' => 'I have a good soul'
]
];
// ...THEN create your client,
$client = new GuzzleClient($settings);
// ...and finally check your response.
$response = $client->request('POST', 'donations');
If you check $request->all() at the controller function that you are calling, you should see that were sent successfully.
For those using laravel, use this:
Http::asJson()

Nexmo Laravel connect to PSTN endpoint not working

My code is shown below. Everything works for me, besides connecting a call to a PSTN endpoint. I'm thinking possibly that maybe the "connect" functions aren't included in the Laravel Nexmo packages that I'm using. I'm using these:
https://github.com/Nexmo/nexmo-laravel
Which is built on another nexmo package:
https://github.com/Nexmo/nexmo-php
My code:
public function getNexmoAnswer(Request $request)
{
return
[
[
'action' => 'talk',
'voiceName' => 'Justin',
'text' => 'Welcome to our great site’
],
[
'action' => 'talk',
'voiceName' => 'Justin',
'text' => "Press # to search now the e.t.a status of your latest order.",
'bargeIn' => true
],
[
'action' => 'connect',
'from' => '17181234567’,
'endpoint' => [
'type' => 'phone',
'number' => '18451234567’
]
]
];
}
The connect action is part of Nexmo's NCCO system and is unrelated to Nexmo-Laravel and Nexmo-PHP. Your NCCO looks to be correct to me.
To connect to another number, you'll need to have activated your account by adding credit. In addition, the from number you're using (17181234567) must be a number purchased from Nexmo

Resources