I want to add some attributes to api request to use them in further. I'm sending request like this:
$data = array(
"source" => "My text",
"speech" => "My text",
"displayText" =>"My text",
"contextOut" => array()
)
header('Content-Type: application/json');
echo json_encode($data);
How do I add my own custom parameters to this request?
Since you're handling the JSON yourself, the best way to do this is to add the parameters you want in a Context. This Context will be sent back to your webhook for the lifespan (number of user requests) of the Context. You can re-send the Context and extend its life at any point, or just set it to a large lifespan. Contexts are only good for the same session - they don't span conversations.
You can create a context and send it in your reply with something like this:
$context = array(
"name" => "my-context",
"lifespan" => 99,
"parameters" => array(
"parameter_one" => "value_one",
"parameter_two" => "value_two"
)
);
$contexts = [$context];
$data = array(
"source" => "My text",
"speech" => "My text",
"displayText" =>"My text",
"contextOut" => $contexts
)
In your requests, you'd look for the value in the extracted JSON body in an array at result.contexts.
Related
I'm implementing an app in "Dialog Flow"
I'm sending request to app like this
$text = "Something";
$data = array(
"source" => $text,
"speech" => $text,
"displayText" =>$text,
"contextOut" => array()
);
header('Content-Type: application/json');
echo json_encode($data);
The text displayed in app. but mic is open I want turn off the mic .
I tried expectUserResponse but not working
array(
"expectUserResponse" => false,
"source" => $text,
"speech" => $text,
"displayText" =>$text,
"contextOut" => array()
)
Please help.
The expectUserResponse parameter is not part of the Dialogflow response JSON. Instead, it is part of the Actions on Google specific part of the response. If you're using Dialogflow v1, this will be in the data.google object. If you're using Dialogflow v2, this will be in the payload.google object.
So if you're using Dialogflow v1, your code might look something like this:
array(
"speech" => $text,
"displayText" =>$text,
"contextOut" => array(),
"data" => array(
"google" => array(
"expectUserResponse": false
)
)
)
while v2 might look like
array(
"speech" => $text,
"displayText" =>$text,
"contextOut" => array(),
"payload" => array(
"google" => array(
"expectUserResponse": false
)
)
)
I think there is a trick to remove the "shared a link" next the username when you post something via the facebook api. I notice it's removed when you add a message field to your post, but I would like to get it even without any message. I tried to pass TRUE, a blankspace, " ", etc as message but that was like there was no any message.
array(
'message' => " ", // I guess the trick is somewhere here
'link' => $link,
'name' => $name,
'description' => ' ',
'caption' => $date,
)
what I would like:
what I get:
with a message:
I have a form I created in a custom module using the Form API. It is a fairly basic form with only 4 fields. It basically signs a user up for a job alert system. We are basing it only by email address with a few search parameters. We want people to be able to setup a search agent quickly and anonymously meaning they will NOT be creating a Drupal user account as we don't want them to have to deal with a password etc. They will just put in their email address, check off a few preferences and we will save the data.
Now the issue I need to deal with is allowing the user to edit their preferences later on and/or unsubscribe. Again this is not high security and it doesn't need to be. What I would like to do is initially ask ONLY for their email address in the form and allow them to submit it. I would then check the database to see if we already have an entry for that email address and if so, display the pre-filled form for them to edit or unsubsribe, other wise just show them the blank form. So I am just trying to figure out the best way to go about this. I'm thinking I just have one form with all of the fields including email address, but somehow only display the other fields besides the email address after a successful call to the database. I'm just tripping up on how to accomplish this.
EDIT:
I'm wondering if I can use Drupal's AJAX functionality to accomplish this? I tried this, but I couldn't get it to work. I put an Ajax attribute on my submit button with a wrapper ID and a callback function. I created a form element in my form with blank markup and used a prefix and suffix that created a wrapper div with the ID I used in my AJAX parameter. Then I am thinking in my callback function I can do the database lookup and then return the form elements I need either pre-filled or not into the wrapper div that was created, but when I do this, the form does submit via AJAX and I get the spinning wheel, but no matter what I return in my callback, it does not appear in my output wrapper div. Am I going about this the right way? I also made sure I have $form_state['rebuild'] = TRUE; on my original form.
Here is what I tried and it didn't work.
/**
* Implements hook_form().
*/
function _vista_form($form, &$form_state) {
$form = array();
$form_state['rebuild'] = TRUE;
$form['email'] = array(
'#type' => 'textfield',
'#title' => 'Email',
'#required' => TRUE,
);
$form['render_area'] = array(
'#type' => 'markup',
'#markup' => '',
'#prefix' => '<div id="job-agent-form">',
'#suffix' => '</div>',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
'#attributes' => array('class' => array('submit')),
'#ajax' => array(
'callback' => '_display_form',
'wrapper' => 'job-agent-form',
'method' => 'replace',
'effect' => 'fade',
),
return $form;
}
function _display_form($form, &$form_state) {
// there are other form elements that would go here also, I just added two for example
$type_options = array(
'VISTA-HealthCare-Partners-Government' => 'Vista Healthcare Partners',
'International' => 'International Locum Tenens',
'Permanent' => 'Permanent Physician',
'US-Locum-Tenens' => 'US Locum Tenens',
);
$form['job_type'] = array(
'#type' => 'checkboxes',
'#multiple' => TRUE,
'#title' => 'Type of Job',
'#options' => $type_options,
'#empty_option' => 'Choose a placement type',
'#empty_value' => 'all',
//'#default_value' => $type_selected,
);
$form['active'] = array(
'#type' => 'checkbox',
'#title' => 'Subscribe/Unsubscribe',
'#default_value' => 1,
);
return $form;
}
I would go for creating all fields in the form than use hook_form_alter() to hide the unneeded ones with ['#access'] = FALSE.
I am writing a module for my organization to cache XML feeds to static files to an arbitrary place on our webserver. I am new at Drupal development, and would like to know if I am approaching this the right way.
Basically I:
Expose a url via the menu hook, where a user can enter in a an output directory on the webserver and press the "dump" button and then have PHP go to drupal and get the feed xml. I don't need help with that functionality, because I actually have a prototype working in Python (outside of Drupal)..
Provide a callback for the form where I can do my logic, using the form parameters.
Here's the menu hook:
function ncbi_cache_files_menu() {
$items = array();
$items['admin/content/ncbi_cache_files'] = array(
'title' => 'NCBI Cache File Module',
'description' => 'Cache Guide static content to files',
'page callback' => 'drupal_get_form',
'page arguments' => array( 'ncbi_cache_files_show_submit'),
'access arguments' => array( 'administer site configuration' ),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
I generate the form in:
function ncbi_cache_files_show_submit() {
$DEFAULT_OUT = 'http://myorg/foo';
$form[ 'ncbi_cache_files' ] = array(
'#type' => 'textfield',
'#title' => t('Output Directory'),
'#description' => t('Where you want the static files to be dumped.
This should be a directory that www has write access to, and
should be accessible from the foo server'),
'#default_value' => t( $DEFAULT_OUT ),
'#size' => strlen( $DEFAULT_OUT ) + 5,
);
$form['dump'] = array(
'#type' => 'submit',
'#value' => 'Dump',
'#submit' => array( 'ncbi_cache_files_dump'),
);
return system_settings_form( $form );
}
Then the functionality is in the callback:
function ncbi_cache_files_dump( $p, $q) {
//dpm( get_defined_vars() );
$outdir = $p['ncbi_cache_files']['#post']['ncbi_cache_files'];
drupal_set_message('outdir: ' . $outdir );
}
The question: Is this a decent way of processing an arbitrary form in Drupal? I not really need to listen for any drupal hooks, because I am basically just doing some URL and file processing.
What are those arguments that I'm getting in the callback ($q)? That's the form array I guess, with the post values? Is this the best way to get the form parameters to work on?
Thanks for any advice.
You can process forms in two stages, validate and submit.
Validate is for when you want to validate some user provided and raise form errors if some user input was invalid (like an invalid url or email address)
Submit which is the one you use is called if a form passes all of its validations, so at that point if you made a proper validation you will know that the data supplied by the user is okay.
Your submit function should look like this:
function ncbi_cache_files_dump(&$form, &$form_state) {
// $form: an array containing the form data
// $form_state: data about the form, like the data inputted in the form etc.
// code...
}
I think you need 2 separate forms here:
for setting the directory (the one you have now);
for making a dump (another form that would use the configured path).
Also it seems logical to publish the previously saved path as the default value in the settings form (instead of a hard-coded path).
And in general you should check the form input data from the second parameter of the submit callback:
function ncbi_cache_files_dump(&$form, &$form_state) {
$outdir = $form_state['values']['ncbi_cache_files'];
// ...
}
I have a form in a Zend-based site which has a required "Terms and Conditions" checkbox.
I have set a custom message which says "you must agree with terms and conditions".
however, because the checkbox is "presence='required'", it returns
Field 'terms' is required by rule 'terms', but the field is missing
which is this constant defined in the Zend framework:
self::MISSING_MESSAGE => "Field '%field%' is required by rule '%rule%', but the field is missing",
I could edit this constant, but this would change the error reporting for all required checkboxes.
How can I affect the error reporting for this specific case?
If you are using the Zend_Form_Element_Checkbox you can customize the error messages on the Zend_Validate validators.
$form->addElement('checkbox', 'terms', array(
'label'=>'Terms and Services',
'uncheckedValue'=> '',
'checkedValue' => 'I Agree',
'validators' => array(
// array($validator, $breakOnChainFailure, $options)
array('notEmpty', true, array(
'messages' => array(
'isEmpty'=>'You must agree to the terms'
)
))
),
'required'=>true,
);
You want to make sure the unchecked value is "blank" and that the field is "required"
You can override the default message like this:
$options = array(
'missingMessage' => "Field '%field%' is required by rule '%rule%', dawg!"
);
And then:
$input = new Zend_Filter_Input($filters, $validators, $myData);
Or
$input = new Zend_Filter_Input($filters, $validators, $myData);
$input->setOptions($options);
...and finally:
if ($input->hasInvalid() || $input->hasMissing()) {
$messages = $input->getMessages();
}
It's mentioned on the Zend_Filter_Input manual page.
Following #gnarf answer, for those of you who may set Form Fields in a slightly different way (like me), you could also do the following:
$agree_tc_and_privacy = new Zend_Form_Element_Checkbox('agree_tc_and_privacy');
$agree_tc_and_privacy
->setLabel("My T&Cs Agreement text ...")
->addValidator('NotEmpty', false, array('messages' => 'You must and agree...'))
->setRequired(true)
->setOptions(
array(
'uncheckedValue'=> '', //important as explained by gnarf above
'checkedValue' => '1',
)
);
$this->addElement($agree_tc_and_privacy);