I am using Botman 2.0 to build a Facebook Messenger chatbot.
Everything is fine with webhook verification and the hear() and reply() methods work well.
But, it looks like other methods don't work.
i.e.
Whenever I use the say() method, it never works. My server receives the request from Messenger, but it does not respond with the message response.
I have test with Botman's main example:
$botman->hears('Hello BotMan!', function($bot) {
$bot->reply('Hello!');
$bot->ask('Whats your name?', function($answer, $bot) {
$bot->say('Welcome '.$answer->getText()); //this never works
});
});
Additionally, when I try to use ButtonTemplate it raises an exception:
PHP Fatal error: Uncaught Error: Class 'ButtonTemplate' not found
Even though Botman's Facebook Driver is loaded:
DriverManager::loadDriver(\BotMan\Drivers\Facebook\FacebookDriver::class);
And my composer.json file looks right:
"require": {
"botman/driver-facebook": "^1.7"
}
What am I missing here?
You need add in your header the following classes:
use BotMan\Drivers\Facebook\Commands\AddStartButtonPayload;
use BotMan\Drivers\Facebook\Commands\AddGreetingText;
use BotMan\Drivers\Facebook\Extensions\ButtonTemplate;
use BotMan\Drivers\Facebook\Extensions\ElementButton;
This works for me. I added the below Facebook extensions immediately after the controller class
use BotMan\Drivers\Facebook\Extensions\Element as Element;
use BotMan\Drivers\Facebook\Extensions\ElementButton as ElementButton;
use BotMan\Drivers\Facebook\Extensions\ButtonTemplate as ButtonTemplate;
use BotMan\Drivers\Facebook\Extensions\GenericTemplate as GenericTemplate;
use BotMan\Drivers\Facebook\Extensions\ListTemplate as ListTemplate;
$botman = resolve('botman');
$botman->hears('Hello BotMan!', function($bot) {
$bot->reply('Hello!');
$bot->ask('Whats your name?', function($answer, $bot) {
$bot->say('Welcome '.$answer->getText()); //this works
});
});
You can check from this link as well https://www.gitmemory.com/issue/botman/botman/1055/522233149
Related
I'm trying to utilize FirePHP in typerocket but I'm having trouble when I try to use its API in a Controller.
I have the the following routes (in routes.php):
tr_route()->get()->match('/exp-get/')->do('experiment#Customer');
tr_route()->get()->match('/exp-get2/')->do(function() {
require_once TR_PATH . '/vendor/firephp/firephp-core/lib/FirePHPCore/fb.php';
echo 'before';
fb('Hello World!', FirePHP::INFO);
echo 'after';
});
the CustomerController contains:
public function experiment() {
require_once TR_PATH . '/vendor/firephp/firephp-core/lib/FirePHPCore/fb.php';
echo 'before';
fb('Hello World!', FirePHP::INFO);
echo 'after';
}
Now, when I call https://dummy-domain.org/exp-get2/ everything runs as expected, but however, when I call https://dummy-domain.org/exp-get/ only 'before' is echoed (or printed) but not the rest! Unfortunately no errors are logged even though I've configured php logging. What might be the reason for this and why isn't the error logged if there is any error?
Additionally, I'm having the issue that I need to explicitly require_once fb.php although FirePHP should be autoloaded, as outside of the do related functions I can use the FirePHP API without having to do this! Isn't the autoload working here?
Note: I'm using the lastest TypeRocket version.
By further debugging I noticed that the php engine was complaining about that aFirePHP class was not found which is used inside the fb function. As a result I just had to put this statement on top of the class which contains that experiment method:
use \FirePHP;
those require_once TR_PATH . '/vendor/firephp/firephp-core/lib/FirePHPCore/fb.php'; lines are unnecessary since the autoloader handels that stuff already.
This answer led me to the right direction.
I'm new to the Angular World (I have been using Angularjs all the while).
I want to make a login app and would like to make an api call to the backend (the php and mysql server, running locally on my computer).
In Angularjs, I would write a service like:
(function () {
'use strict';
angular.module('module name').service('userService', user);
function user ($resource) {
return $resource('api/users', {}, {update: {method: PUT}});
}
})();
With that, I could make calls to the server by simply calling
$onInit = function () {
var user = new userService();
user.$save(.....) //post request.
user.$update(....) //put request.
}
In angular, I see things are different. I can't seem to connect to the server using the same code I wrote for AngularJS.
Making an api call like:
this.http.post('api/users', userAccess.value).subscribe(data => {
console.log(data);
}
gives me post http://localhost:3001/api/users 404 not found error.
PS. userAccess is the ReactiveForm formGroup object containing an email address and a password.
The api code sits in a folder under src folder, next to app folder. It has 3 files, api.php, rest.inc.php and the htaccess file. It's my own implementation of an api interface.
Here is my LoginComponent.ts..
import { Component, OnInit } from '#angular/core';
import { FormBuilder, FormGroup, Validators } from "#angular/forms";
import { HttpClient } from '#angular/common/http';
#Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.sass']
})
export class LoginComponent implements OnInit {
hide: boolean = true;
constructor(
private fb: FormBuilder,
private http: HttpClient
) { }
ngOnInit() {
}
userAccess: FormGroup = this.fb.group({
email: ['', [Validators.email, Validators.required]],
password: ['', [Validators.required]]
})
onSubmit = () => {
this.http.post('api/users',
{
username: this.userAccess.value.email,
encryption: this.userAccess.value.password
}
).subscribe(data => {
console.log(data);
});
}
getEmailMessage = () => {
return this.userAccess.controls['email'].hasError('required') ? 'Please enter an email address' : this.userAccess.controls['email'].hasError('email') ? 'Not a valid email' : '';
}
}
And here is the folder structure..
Here is the snapshot of the api folder..
This is the error I get no matter what path I put...
How should I go about doing this the right way?
You are getting 404 error that does mean you are able to call HTTP service from your angular code. Please check whether the URL for your API is correct or not. (Hint: Use Browser tools)
In Angular, to access API it is recommended to create a separate service class which can be later injected wherever you require it e.g. component.
You can create service class using following CLI command
ng g s NameOfYourService
Later int the constructor of your service simply inject a reference of HttpClient. Refer below example
constructor(private httpClient:HttpClient)
you can use this reference as shown below
public performUserLogin(cred:Credentials):Promise<void> {
return this.httpClient.post<void>('http://yourhost:yourport/api_path', cred).toPromise();
}
Note you have choice to either return promise or Observable.
For more details you can go through the official documentation.
path you have defined here is incorrect.
this.http.post('http://localhost/project_name/api/api.php',
{
username: this.userAccess.value.email,
encryption: this.userAccess.value.password
}
).subscribe(data => {
console.log(data);
});
change the path of the api folder, bring it to the root folder(at the same level of e2e folder), then do as mentioned.
add these lines to the php file
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: X-Requested-With');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
header('Content-Type: application/json');
and to get the data in post request use this
$data = json_decode(file_get_contents("php://input"));
From what I see in the error log, you get this 404 because you are not calling the good url, you try to reach localhost:4200 but you want to call localhost:3001.
On localhost:4200 you certainly have your angular app running if you launched it using the "ng serve" command, because ng serve uses port 4200.
Since you are not specifying the full path in your post, it is trying to reach the url from where you are calling it, here 4200.
So to fix that you should specify the full path in your post like this:
this.http.post('http://localhost:3001/api/users', userAccess.value).subscribe(data => {console.log(data);}
this should fix it, at least it should call your php api.
I got it working. I just added a proxy configuration and run the server on a different port
I'm using Botman 2.0 and Codeigniter 3.1.6
Ngrok & FB Webhook setup successfully..
Did a simple hears & reply method, working good :
$this->botman->hears('foo','HelloWorld#handleFoo');
$this->botman->hears('hello',function($bot){
$bot->reply('bye~');
});
But when using Botman's conversation method, then Bot is not replying...my conversation code as below:
$this->load->library('BotConversations/OnboardingConversation');
$this->botman->hears('sup', function($bot) {
$bot->startConversation(OnboardingConversation);
});
// Listen
$this->botman->listen();
anyhow, I've follow Botman's Cache conf's guide to setup Cache via Codeigniter method
Below are some of my test files:
Controller
Class
appreciate your help! thanks....
Everything works fine right now, I overlook on a simple syntax issue...
updated my code as below :
$this->botman->hears('sup', function($bot) {
$bot->startConversation(New OnboardingConversation);
});
<?php
include(APPPATH.'/libraries/REST_Controller.php');
class Quiz extends REST_Controller{
function __construct()
{
// Call the Model constructor
parent::__construct();
}
public function user_get()
{
$this->load->model('Quizmodel');
$data = $this->Quizmodel->getAll();
$this->response($data, 200);
}
function restclient()
{
$this->load->library('rest', array(
'server' => 'http://localhost/CodeIg/index.php/quiz/'
));
$userr = $this->rest->get('user','','json');
echo $userr;
}
}
?>
I am able to get JSON output if I type http://localhost/CodeIg/index.php/quiz/user in my browser, however if I type http://localhost/CodeIg/index.php/quiz/restclient it gives this error: {"status":false,"error":"Unknown method"}
I tried changing get to post but still the same error.
I referred this page https://code.tutsplus.com/tutorials/working-with-restful-services-in-codeigniter--net-8814 to do it.
You pinged me on GitHub, even though I haven't used or even thought about this code in at least 4 years.
https://github.com/chriskacerguis/codeigniter-restserver/blob/d19dc77f03521c7a725a4555407e1e4e7a85f6e1/application/libraries/REST_Controller.php#L680
This is where that error is being triggered. Throw a few breakpoints in there or var_dump()'s until you see what is causing the trouble.
You probably want to get off CodeIgniter though, and use something more actively maintained like SlimPHP or Lumen.
firstly I want as you have loaded rest api and created your controller quiz as an api to call , where you can only create your functions like user_get or restclient_get and access them the same manner you are doing.Just change you function name restclient to restclient_get then it will call instead it is even not running at this moment.
I have a free installed instance of Cakephp 3.0 with several code examples, everything is working fine. Now I want to create a Auth component in ./src/Auth, concerning the documentation here
Thats my code:
<?php
namespace App\Auth;
use Cake\Auth\BaseAuthenticate;
class AlephAuthenticate extends BaseAuthenticate
{
public function authenticate(Request $request, Response $response)
{
// Do things for OpenID here.
// Return an array of user if they could authenticate the user,
// return false if not.
}
}
In AppContoller.php I initialize this component:
public function initialize() {
$this->loadComponent('Auth');
$this->Auth->config('authenticate', ['Aleph']);
}
Calling the application URL in the browser shows:
Fatal error: Declaration of App\Auth\AlephAuthenticate::authenticate() must be compatible with Cake\Auth\BaseAuthenticate::authenticate(Cake\Network\Request $request, Cake\Network\Response $response) in /var/www/art/src/Auth/AlephAuthenticate.php on line 7
Any idea whats wrong here?
Thanks!
Christoph
Please read the error message it is obvious. If you still have trouble understanding it just paste the error message into a search engine. This is the basic procedure for almost every error message if you don't know it and will probably always solve it. Guess it's in the php documentation as well.
Let me rephrase the error message for you: The method signatures don't match. To get rid off the error message make them match.