POST: 500 Internal Server Error from Messenger Bot in laravel - laravel

I setup my Webhok by Ngrok URL for my Facebook page, and I applied all of the requirements for the Messenger Platform, but when I send the messages to my Facebook page I encounter the following error:
POST /Facebook_Messenger_Token 500 Internal Server Error
and in routs file in Laravel I use Get and Post functions as follow:
Route::get('Facebook_Messenger_Token', 'MessengerController#index');
Route::post('Facebook_Messenger_Token', 'MessengerController#index');
When I send the messages I get the following error in storage/app.logs/laravel:
[2020-06-08 18:44:21] local.ERROR: Undefined variable: id {"exception":"[object] (ErrorException(code: 0): Undefined variable: id at C:\\xampp\\htdocs\\AzadApp\\app\\Http\\Controllers\\MessengerController.php:17)
[stacktrace]
my public function index:
public function index()
{
// here we can verify the webhook.
// i create a method for that.
$this->verifyAccess();
$user = json_decode($this->getUser($id)); --this is line 17
$input = json_decode(file_get_contents('php://input'), true);
$id = $input['entry'][0]['messaging'][0]['sender']['id'];
$message = $input['entry'][0]['messaging'][0]['message']['text'];
$response = [
'recipient' => ['id' => $id ],
'message' => ['text' => "Thanks for watching {$user->first_name} {$user->last_name}! :)"]
];
$this->sendMessage($response);
}
Please support and thanks.

The $id is being defined on line 19 (that is after line 17). in order to use it on
$user = json_decode($this->getUser($id));
you should place the above line after line 19
$id = $input['entry'][0]['messaging'][0]['sender']['id'];
do not move line 19 up (as i said in my comment) instead move line 17 down since
$id = $input['entry'][0]['messaging'][0]['sender']['id'];
is using $input that is defined before.
all you have to do is move line 17 under line 19.
so the full function now should look like this:
public function index()
{
// here we can verify the webhook.
// i create a method for that.
$this->verifyAccess();
$input = json_decode(file_get_contents('php://input'), true);
$id = $input['entry'][0]['messaging'][0]['sender']['id'];
$message = $input['entry'][0]['messaging'][0]['message']['text'];
$user = json_decode($this->getUser($id));
$response = [
'recipient' => ['id' => $id ],
'message' => ['text' => "Thanks for watching {$user->first_name} {$user->last_name}! :)"]
];
$this->sendMessage($response);
}

Related

Call to a member function move() on null when update in laravel

I got this errror when trying to update data. If I update the image there's no error, but if I'm not it showing Call to a member function move() on null
This is my code:
public function update($id, Request $request)
{
$change = Profile::findorfail($id);
$before = $change->foto_profil;
$profile = [
'fullname' => $request['fullname'],
'phone' => $request['phone'],
'ttl' => $request['ttl'],
'foto_profil' => $before
];
$request->foto_profil->move(public_path().'/image', $before);
$change->update($profile);
return redirect('/profil');
}
You may determine if a file is present on the request using the hasFile() method :
if($request->hasFile('foto_profil')){
$request->foto_profil->move(public_path().'/image', $before);
}
See the documentation here
just add validation if photo exists in request
if($request->foto_profil) {
$request->foto_profil->move(public_path().'/image', $before);
}

Make request (pass data) from Command File to Controller in Lumen/Laravel

i get data (query) in command file and want to pass to controller via API (route)
here my request code in command file :
$request = Request::create('/create_data_account', 'post', ['data'=>$data]);
$create = $app->dispatch($request);
this is the route :
$router->post('/create_data_account', 'APIController#create_account_data_api');
and my controller :
public function create_account_data_api(Request $request)
{
$count = 0;
foreach ($data as $key => $value) {
$insert = Account::create([
'account_name' => $value->account_name,
'type' => $value->type,
'role_id' => $value->role_id
]);
if($insert){
$count++;
}else{
return $response = ['result'=>false, 'count'=>$count, 'message'=>'Internal error. Failed to save data.'];
}
}
return $response = ['result'=>true, 'count'=>$count, 'message'=>'Account data saved Successfully'];
}
i'm confused why passing data to controller not working with that code. anyone can give me solution ? Thanks
As you are making the request with ['data'=>$data]. That means all the data is contained on the key data of your array. So before the foreach loop, you need to add a declaration statement $data = $request->input('data'); to get data in $data variable.

laravel 5.4 Send Email error : Default value for parameters with a class type hint can only be NULL in CssSelectorConverter.php line 34

I run code on local host send email success. But, I run code on Linux host send email error
This image error
My controller
public function vendorApprove($vendor_id)
{
//dd($vendor_id);
$approve = DB::table('vendor')->where('vid', '=', $vendor_id)->update(['active' => 1]);
$mail = DB::table('vendor as v')
->join('vendor_contact as vc', 'v.vid', '=', 'vc.vid')
->where('v.vid', '=', $vendor_id)
->first();
//dd($mail);
Mail::to($mail->email)->send(new VendorApproveMail($mail));
return redirect(route('admin.dashboard'))->with('success', 'Vendor Approve success');
}
And Mailable code
public function __construct($mail)
{
$this->mail = $mail;
}
public function build()
{
return $this->from(config('mail.username'))
->subject('Vendor Approve')
->markdown('admin.emails.approve', [
'url' => url( route('vendor.profile', $this->mail->vid )),
'name' => $this->mail->vcontName,
'email' => $this->mail->email,
'message' => 'The vendor your approved',
]);
}
If I comment //Mail::to($mail->email)->send(new VendorApproveMail($mail)); This code, it working
I think function send email not working
The formatting of the markdown must always be left.
This my markdown
This is the correct format.

laravel phpunit testing expecting status code 201 but received 302?

I have a simple PostController with a store method that receives data, validates it, stores it and then redirects to /posts with a success message.
I have written a test to ensure that the post is created and saved as follows:
/** test */
public function testStore()
{
$user = factory('App\User')->create();
$post = factory('App\Post')->create(['user_id' => $user->id]);
$response = $this->actingAs($user)
->json('POST', '/notice', $post->toArray())
->assertStatus(201)
->assertJson([
'created' => true,
]);
}
But I am receiving 301(redirection) instead of 201. I do not understand this.

How to attach variable to reset password link view in laravel?

I am using laravel 5.2 where I need to sent an OTP code to reset password, though email is being sent with built in subject and limited message done by make:auth command but how to customize? I have tried to follow the link unfortunately I am unable to understand how i can use this to solve.
I customized the api like this
public function sendResetLinkEmail(Request $request)
{
$this->validateSendResetLinkEmail($request);
$broker = $this->getBroker();
$email = $request->input('email');
$userid = DB::table('users')->where('email','=',$email)->value('id');
$uniqueotp = "DIYA".uniqid();
$curr_timestamp = strtotime(date("Y-m-d H:i:s"));
$date = strtotime("+7 day", $curr_timestamp);
$expiry_otp = date('Y-m-d H:i:s',$date);
$ip_address = $request->ip();
DB::table('otp_users')->insert([
'user_id' => $userid,
'status' => 0,
'otp_code' => $uniqueotp,
'ipaddress'=>$ip_address,
'expires_at'=>$expiry_otp
]);
$response = Password::broker($broker)->sendResetLink(
$this->getSendResetLinkEmailCredentials($request),
$this->resetEmailBuilder()
);
switch ($response) {
case Password::RESET_LINK_SENT:
return $this->getSendResetLinkEmailSuccessResponse($request,$response);
case Password::INVALID_USER:
default:
return $this->getSendResetLinkEmailFailureResponse($response);
}
}
Any idea how I can achieve?
My required email message like this:
Hello, Tamaghna Banerjee Click here to reset your password:
Your OTP is: B16445512121
Reset Your Password through http://localhost/diya/public/password/reset/83baba9f61fc851b9d80b515415ec86c43b03b56b068e1888256db7a7831ba83?email=tamaghnabanerjee%40live.com

Resources