How to validate use entered phone number using slots in botpress? - botpress

I am experimenting on botpress slots . There is the new option called as slot which will validate the user input . However I am not able to find resources which will validate the user input.
The bot must validate the use input as a phone number using slot feature or any other without use of external api ?
Is this possible ?
for example:
If the user inputs a valide phone number the flow will proceed.
else if use enters invalid phone number the flow will ask to re enter a valid phone number.
I have tried multiple things but had no luck finding the proper documentation/ tutorial regarding it.

I think you can use a custom action to achieve this. A custom action for validating mobile number may look like:
const baseMessage = {
type: 'text',
markdown: false
}
/**
* check if phone number is valid
* #title validate phone number
* #category Validation
* #author Your name
* #param {string} phone - phone number
*/
const validateNumber = async phone => {
var phoneRegex = /^\d{10}$/
if (phone.match(phoneRegex)) {
temp.phone_validation = 'success'
} else {
temp.phone_validation = 'error'
}
}
return validateNumber(args.phone)
You can call the custom action in your validation flow and redirect user accordingly

Related

Drupal 8 Custom Module Stop Sending Emails Filter by Email Addresses

I am building a custom module using hook_mail_alter in Drupal 8 which stops the system from sending emails to a selected email address.
My current module stops sending any emails for some reasons. I need help to figure out why.
<?php
use Drupal\Core\Mail\MailManagerInterface;
/**
* Implements hook_mail_alter
*/
function terminate_emails_mail_alter(&$message) {
//filter by email address, which is not working
if($message['to'] = 'example#gmail.com') {
//stop sending emails
$message['send'] = FALSE;
}
}
This is funny. Check your if condition:
if($message['to'] = 'example#gmail.com') {
You are using assignment operator, not comparison. Use double equal-to (==)
if($message['to'] == 'example#gmail.com') {
What you are doing here, is assign something, and your condition will always be true

Laravel Socialite facebook get user phone number

I use laravel socialite for authentication users on my website. I get basic user data, but I need user_phone, because some users signet up for Facebook with a phone number.
Unfortunately in the documentation I have not found a field phone_number
This is screenschot based FB doc, ebout email field:
The question: if the field email is empty, where to look for a number???
Phone number is not available using Facebook API check available fields here https://developers.facebook.com/docs/graph-api/reference/v2.2/user
You can try using this. Credits goes to #amirhazz, the full thread can be found here : https://laracasts.com/discuss/channels/laravel/socialite-facebook-extra-fields?page=1
class FacebookLogin {
/**
* Since facebook will give you name, email, gender by default,
* You'll only need to initialize Facebook scopes after getting permission
*/
const facebookScope = [
'user_birthday',
'user_location',
];
/**
* Initialize Facebook fields to override
*/
const facebookFields = [
'name', // Default
'email', // Default
'gender', // Default
'birthday', // I've given permission
'location', // I've given permission
];
public function facebookRedirect()
{
return Socialite::driver('facebook')->fields(self::facebookFields)->scopes(self::facebookScope)->redirect();
}
public function facebookCallback()
{
$facebook = Socialite::driver('facebook')->fields(self::facebookFields)->user();
}
}

Interaction between two forms in a single controller

I'm trying to work with two forms in a single controller in Symfony2. The page is displaying a list of e-mail addresses with checkboxes to select them.
The first form is a research form and the second is used to remove e-mail addresses. There is also a pagination in my page.
My problem is that I can't remove an e-mail address with a research when the e-mail address is not displayed in the first page.
public function emailsAction(Request $request, $paginationParams)
{
$searchForm = $this->createForm(new SearchFormType(), null, array());
$searchForm->handleRequest($request);
if ($searchForm->isValid()) {
// FETCH E-MAILS WITH SEARCH
} else{
// FETCH E-MAILS WITHOUT SEARCH
}
// here I slice the result depending on the pagination params
$removeForm = $this->createForm(new RemoveFormType(), null, array(
'emails' => $emails
));
$removeForm->handleRequest($request);
if ($removeForm->isValid())
{
$formData = $removeForm->getData();
// here I remove the e-mails
}
...
$params['remove_form'] = $manageSubscribersForm->createView();
$params['search_form'] = $searchAbonneActuForm->createView();
return $this->renderView('my_template.html.twig', $params);
}
If I understand it correctly, the problem is that my removeForm is created with the spliced array and without the search applied when handling the removeForm.
When I search for a name, say johnsmith, it displays me on page 1, one result which is johnsmith#email.com.
If johnsmith#email.com is on page 2 without search, the removeForm handling my request is created with a spliced array not containing johnsmith, so the removeForm is not considered valid.
How should I do to create my removeForm, taking account of the search done before when submitting the removeForm? Or maybe I'm doing this wrong ?
I'm not a native english speaker so if anything is not clear, feel free to ask.
You can use a hidden field with the current page index, that will help you make your search again.
Or you can use an event listener to modify the form field before the submission for the validation.

How to auto load last submission (single submission)?

I built multiple webforms in Drupal. I want user to
Submit only ONE submission by form.
Auto load last submission when they go again on webform submitted.
How can I do this?
You could use the email webform component, set it as unique and check the User email as default, if you'd have one in your webform.
Autoloading a webform submission might be possible with by creating a views block with a contextual filters to the user giving the uid provided & a relation to webform.
As you might not want an additional webform component for your webforms (or not all of them), you could always create a module, include the webform functionality and retrieve the submitted data on a webform instance with hook_form_alter() as:
/**
* Implements hook_form_alter().
*/
function YOUR_MODULE_form_alter(&$form, &$form_state, $form_id) {
// get current user
global $user;
// include webform functionality
module_load_include('inc','webform','includes/webform.submissions');
// make sure to only alter webform forms
if (strpos($form_id, 'webform_client_form_') !== FALSE) {
// check if the user is a an authenticated user
if (in_array('authenticated user', $user->roles)) {
// build $filters array for submission retrieval
$filters = array(
'nid' => $form['#node']->webform['nid'],
'uid' => $user->uid,
);
/**
* When not using a unique webform component for 1 submission
* you can use a submission of the user on a webform instance
* to prevent another submission.
*/
// get submissions of the user by webform nid
if ($submissions = webform_get_submissions($filters)) {
// disable the form to limit multiple submissions per instance
$form['#disabled'] = TRUE;
/**
* Webform instance submission data of the current user
* can be found in $submissions[1]->data array
*/
# render your submission with Form api
}
}
}
}
Hope this helps.
Limiting to only 1 submission per webform can be done via Webform -> Form Settings -> Total Submissions Limit and Per User submission limit as shown in this screenshot
To autoload user submission, since above would not allow you to show the webform would be via using the submission id of the webform they already submitted. Based on this code
module_load_include('inc','webform','includes/webform.submissions');
$sid = 10;
$submission = webform_get_submissions(array('sid' => $sid));
$nid = $submission[$sid]->nid;
$web_submission = webform_get_submission($nid, $sid);
$node = node_load($nid);
$output = webform_submission_render($node, $web_submission, NULL, 'html');
print $output;

CakePHP HTTPS Secure payment form

Using CakePHP 1.3 we have a booking system for hotel rooms. A check-availability form should bring the user to a secure payment page (https://secure.domain.com/bookings/payment). After making the payment, the user gets a confirmation page (secured is also ok), but from here, any links in our header/footer should take the user back to the non-secured domain (http://domain.com).
Currently we have our SSL UCC Cert set up for the domains https://secure.domain.com and https://domain.com. We have also hard coded the check-availability form to run the action https://secure.domain.com/bookings/payment. Thus, we can get the user to get in to the HTTPS secured area, but not back out unless we hard code all our links in that section.
Cake's security component is quite confusing and thus I am looking for the best solution to make this happen.
Can Cake's Security component be used for HTTPS payment pages, make life easier, and keep the code more CakePHP standardized? Any other suggestions?
this is a pretty good way to go: http://techno-geeks.org/2009/03/using-the-security-component-in-cakephp-for-ssl/ so you won't even have to hard code anything.
I used the example from http://techno-geeks.org/2009/03/using-the-security-component-in-cakephp-for-ssl/ but found it problematic. I ended up adding the following to my app_controller.php.
The code below redirects HTTPS to www.example.com and HTTP to example.com. If a user is logged in (see $loggedUser), it forces HTTPS for every connection.
// Pages requiring a secure connection.
$secureItems = array();
// beforeFilter
function beforeFilter() {
// Your logic...
$this->__checkSSL();
}
/**
* Check SSL connection.
*/
function __checkSSL() {
/** Make sure we are secure when we need to be! **/
if (empty($this->loggedUser)) {
if (in_array($this->action, $this->secureItems) && !env('HTTPS')) {
$this->__forceSSL();
}
if (!in_array($this->action, $this->secureItems) && env('HTTPS')) {
$this->__unforceSSL();
}
} else {
// Always force HTTPS if user is logged in.
if (!env('HTTPS')) {
$this->__forceSSL();
}
}
}
/**
* Redirect to a secure connection
* #return unknown_type
*/
function __forceSSL() {
if (strstr(env('SERVER_NAME'), 'www.')) {
$this->redirect('https://' . env('SERVER_NAME') . $this->here);
} else {
$this->redirect('https://www.' . env('SERVER_NAME') . $this->here);
}
}
/**
* Redirect to an unsecure connection
* #return unknown_type
*/
function __unforceSSL() {
if (strstr(env('SERVER_NAME'), 'www.')) {
$server = substr(env('SERVER_NAME'), 4);
$this->redirect('http://' . $server . $this->here);
} else {
$this->redirect('http://' . env('SERVER_NAME') . $this->here);
}
}