<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Set Botfield By Name &#8211; Chatbot Dojo</title>
	<atom:link href="https://v3.chatbotdojo.com/topic-category/set-botfield-by-name/feed/" rel="self" type="application/rss+xml" />
	<link>https://v3.chatbotdojo.com</link>
	<description></description>
	<lastBuildDate>Sun, 20 Sep 2020 21:34:34 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.8.2</generator>

<image>
	<url>https://v3.chatbotdojo.com/wp-content/uploads/2020/07/favicon-56x56.png</url>
	<title>Set Botfield By Name &#8211; Chatbot Dojo</title>
	<link>https://v3.chatbotdojo.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to set botfields by Name with /fb/page/setBotFieldByName</title>
		<link>https://v3.chatbotdojo.com/topic/how-to-set-botfields-by-name-with-fb-page-setbotfieldbyname/</link>
		
		<dc:creator><![CDATA[ninjawarrior]]></dc:creator>
		<pubDate>Sun, 20 Sep 2020 16:07:49 +0000</pubDate>
				<guid isPermaLink="false">https://v3.chatbotdojo.com/?post_type=sfwd-topic&#038;p=25245</guid>

					<description><![CDATA[The curl command as displayed on the ManyChat API Swagger is: The purpose of this API is the same as the one in our previous chapter, which is to set a botfield value in your ManyChat account. However, this particular API uses the BotField Name. Why would you need this? A use case could be: &#8230;<p class="read-more"> <a class="" href="https://v3.chatbotdojo.com/topic/how-to-set-botfields-by-name-with-fb-page-setbotfieldbyname/"> <span class="screen-reader-text">How to set botfields by Name with /fb/page/setBotFieldByName</span> Read More &#187;</a></p>]]></description>
										<content:encoded><![CDATA[
<p>The curl command as displayed on the ManyChat API Swagger is:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">curl -X POST "https://api.manychat.com/fb/page/setBotFieldByName" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"field_name\": \"string\", \"field_value\": \"'string', 123, true, '2018-07-18', '2018-07-02T00:00:00+00:00'\"}"</pre>



<p>The purpose of this API is the same as the one in our previous chapter, which is to set a botfield value in your ManyChat account. However, this particular API uses the BotField Name.</p>



<h2>Why would you need this?</h2>



<p>A use case could be: setting a botfield for a license key to your platform, a language string, a business address or phone number etcetera. Anything <em>static </em>throughout the chatbot.</p>



<p>This is the default response on a successful request</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{
  "status": "success"
}
</pre>



<h2>So how do we use this in our own system?</h2>



<blockquote class="wp-block-quote"><p>We assume you started in the First chapter of this course, and are now aware of the creation of the class file and the config file.<br>Those files are being used in ALL scripts we explain in the course. If you missed it, <a href="https://v3.chatbotdojo.com/topic/fb-page-getinfo/">go here</a> and read it.</p></blockquote>



<p>First we will add a new function to our class file in classes/manychat.class.php.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="511" data-enlighter-title="" data-enlighter-group="">    /**
     * 
     * @param type $bot_field_name
     * @param type $bot_field_value
     * @param type $mc_api
     * @return boolean
     */
    function setBotFieldByName($bot_field_name, $bot_field_value, $mc_api = '123456:ABCDEF')
    {

        $bot_field_vars = [
            'field_name'        => $bot_field_name,
            'field_value' => $bot_field_value
        ];

        $postfields_bot_field_vars = json_encode($bot_field_vars);

        $ch            = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://api.manychat.com/fb/page/setBotField");
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields_bot_field_vars . PHP_EOL);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $headers       = [
            'Authorization: Bearer ' . preg_replace("/^(\w+\s+)/", "", $mc_api),
            'Accept: application/json',
            'Content-Type: application/json'
        ];
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $server_output = curl_exec($ch);
        curl_close($ch);

        $json_obj  = json_decode($server_output);
        $good_call = $json_obj->status;

        if ($good_call == "success") {
            return $json_obj;
        } else {
            return false;
        }
    }</pre>



<div style="height:57px" aria-hidden="true" class="wp-block-spacer"></div>



<blockquote class="wp-block-quote is-style-large"><p><span class="has-inline-color has-luminous-vivid-orange-color">As mentioned in the previous chapter, we will use this lesson to make it a bit more advanced. Instead of using the PHP script similar to what we showed in each previous topic, we will create a more advanced version that will process the updating of botfields.</span></p></blockquote>



<div style="height:57px" aria-hidden="true" class="wp-block-spacer"></div>



<h2>On with the cool stuff</h2>



<p>Now we continue with a couple things, let&#8217;s recap what we&#8217;ll need:</p>



<ol><li>a script with HTML to display the botfields in a select menu</li><li>a script to process the botfield update and show the result </li></ol>



<p>Let&#8217;s start with 1.) and create the PHP file with HTML form. Please note this is all very bare, you need to make it your own with nice bootstrap stylesheets and whatnot. We call the file <em>page_get_botfields_select.php</em></p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;!DOCTYPE html>
&lt;html>
    &lt;head>
        &lt;title>BotFields&lt;/title>
        &lt;meta charset="UTF-8">
        &lt;meta name="viewport" content="width=device-width, initial-scale=1.0">
        &lt;!-- Bootstrap CSS -->
        &lt;link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    &lt;/head>
    &lt;body style="padding:50px;max-width:600px;">

        &lt;form method="post" action="page_set_bot_field_by_name.php">
            &lt;div class="form-group">
                
                &lt;?php
                /*
                 * Load the ManyChat Config File
                 */
                require_once './config.inc.php';

                /*
                 * Get the BotFields for your page
                 */

                $page_botfields = $mc->getBotFields($mc_api);

                echo "&lt;select class='form-control' name='bot_field_name' id='bot_field_name'>";
                foreach ($page_botfields AS $botfields) {
                    $b_value = $botfields->value ?: "Empty";
                    echo "&lt;option value='$botfields->name'>$botfields->name (ID: " . $botfields->id . ", Type: " . $botfields->type . ", Value: " . $b_value . ")&lt;/option>";
                }
                ?>

                &lt;/select>
            &lt;/div>
            &lt;div class="form-group">
                &lt;label for="botvalue">New Bot Value&lt;/label>
                &lt;input type="text" class="form-control" value="" id="botvalue" name="bot_field_value" />
            &lt;/div>

            &lt;button type="submit" class="btn btn-primary">Update Botfield&lt;/button>

        &lt;/form>

        &lt;!-- Optional JavaScript -->
        &lt;!-- jQuery first, Bootstrap JS -->
        &lt;script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous">&lt;/script>
        &lt;script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous">&lt;/script>
    &lt;/body>
&lt;/html></pre>



<p>The above page will look like this:</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="569" height="249" src="https://v3.chatbotdojo.com/wp-content/uploads/2020/09/image.png" alt="" class="wp-image-25270" srcset="https://v3.chatbotdojo.com/wp-content/uploads/2020/09/image.png 569w, https://v3.chatbotdojo.com/wp-content/uploads/2020/09/image-300x131.png 300w, https://v3.chatbotdojo.com/wp-content/uploads/2020/09/image-400x175.png 400w" sizes="(max-width: 569px) 100vw, 569px" /></figure>



<p>After we created the new PHP file with HTML Form for the botfields, we will create a new PHP script and call it <strong><em>page_set_bot_field_by_name.php</em></strong>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?php

/*
 * Load the ManyChat Config File
 */
require_once './config.inc.php';

/*
 * Set a BotField value by Name - Sent Via Webform
 */

$clean_data      = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
$bot_field_name  = $clean_data['bot_field_name'];
$bot_field_value = $clean_data['bot_field_value'];

$page_setBotField = $mc->setBotFieldByName($bot_field_name, $bot_field_value, $mc_api);
if ($page_setBotField->status == "success") {
    echo "You updated a BotField with the name " . $bot_field_name . " to its new value " . $bot_field_value;
} else {
    echo "We could not set your BotField value";
}</pre>



<p>That is basically all it needs to make a form with the ability to update your botfield. Be aware of the file types though, you could go more in detail to split the botfields by type and use different input form types (boolean, number, text, date, datetime) based on the selected botfield, but that is all your call. Consider it a great assignment that concludes the <em>Page API</em> Class.</p>



<p>In our next section, we are going to talk about the <strong>Sending API</strong>, which is an API to send your suscriber content without using a ManyChat flow.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
