<?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>Create Custom Field &#8211; Chatbot Dojo</title>
	<atom:link href="https://v3.chatbotdojo.com/topic-category/create-custom-field/feed/" rel="self" type="application/rss+xml" />
	<link>https://v3.chatbotdojo.com</link>
	<description></description>
	<lastBuildDate>Tue, 15 Sep 2020 14:15:45 +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>Create Custom Field &#8211; Chatbot Dojo</title>
	<link>https://v3.chatbotdojo.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to add a Custom Field with /fb/page/createCustomField</title>
		<link>https://v3.chatbotdojo.com/topic/how-to-add-a-custom-field-with-fb-page-createcustomfield/</link>
		
		<dc:creator><![CDATA[ninjawarrior]]></dc:creator>
		<pubDate>Sun, 23 Aug 2020 14:57:23 +0000</pubDate>
				<guid isPermaLink="false">https://v3.chatbotdojo.com/?post_type=sfwd-topic&#038;p=25189</guid>

					<description><![CDATA[The curl command as displayed on the ManyChat API Swagger is: The purpose of this API call is to Add a new Custom Field in your ManyChat account. Why would you need this? The best use case for this API call, and one that we use extensively in our projects, is to generate new custom &#8230;<p class="read-more"> <a class="" href="https://v3.chatbotdojo.com/topic/how-to-add-a-custom-field-with-fb-page-createcustomfield/"> <span class="screen-reader-text">How to add a Custom Field with /fb/page/createCustomField</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/createCustomField" -H  "accept: application/json" -H  "Content-Type: application/json" -d "{  \"caption\": \"My custom field name\",  \"type\": \"text\",  \"description\": \"This field store my custom field name\"}"</pre>



<p>The purpose of this API call is to Add a new Custom Field in your ManyChat account.</p>



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



<p>The best use case for this API call, and one that we use extensively in our projects, is to <strong>generate new custom fields</strong> that belong to your project. If you create a template, but some custom fields are only set <em>inside your code</em>, there&#8217;s no way the installation of a Manychat flow or template will pick up on those fields. This can cause a lot of headaches (believe me, we&#8217;ve been through this for a couple of years when this API wasn&#8217;t available yet:-)).</p>



<p>With this API, headaches are a sign of the past!</p>



<p>This is the default response on a successful request. It looks similar to the one for creating Tags.</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",
  "field": {
    "id": 0,
    "name": "string",
    "type": "text",
    "description": "string"
  }
}</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="197" data-enlighter-title="" data-enlighter-group="">    /**
     * 
     * @param type $custom_field_name
     * @param type $mc_api
     * @param type $custom_field_description
     * @param type $type
     * @return boolean
     */
    function createCustomField($custom_field_name, $mc_api = '123456:ABCDEF', $custom_field_description = 'Custom Field', $type = 'text')
    {

        $custom_field_vars = [
            'caption'     => $custom_field_name,
            'type'        => $type,
            'description' => $custom_field_description
        ];

        $postfields_custom_field_vars = json_encode($custom_field_vars);

        $ch            = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://api.manychat.com/fb/page/createCustomField");
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields_custom_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>



<p>After we created the new function, we will create a new PHP file and call it <strong><em>page_create_custom_field.php</em></strong>. Here&#8217;s the start of our new PHP file:</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';

/*
 * Create a new Custom Field for your page
 */

// add functions here</pre>



<p>We will add a call to the function in the class file now, then set some default Custom Field information, such as the <strong>name</strong>, <strong>type</strong> and <strong>description</strong>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="12" data-enlighter-title="" data-enlighter-group="">$custom_field_name        = "My New Custom Field";
$custom_field_description = "My New Custom Field Awesome Description So I Can Easily Find It Back Later";
$custom_field_type        = "text"; // text|number|date|datetime|boolean

$page_createCustomField = $mc->createCustomField($custom_field_name, $mc_api, $custom_field_description, $custom_field_type);

if ($page_createCustomField->status == "success") {
    echo "You created a Custom Field with the name " . $page_createCustomField->data->field->name;
} else {
    echo "We could not create your new Custom Field";
}
</pre>



<p>Now, when we run the file (<strong><em>page_create_custom_field</em>.<em>php</em></strong>) in our browser, we should see the following result:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="html" data-enlighter-theme="" data-enlighter-highlight="3,4,6" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">You created a Custom Field with the name My New Custom Field</pre>



<p>That&#8217;s great! We successfully created a new custom field in the ManyChat system, all in our own environment! </p>



<p>We now have a fully functional script that creates custom fields, all from our own localhost, or server. If you need to create a lot of fields, this is very helpful since you can create them all in one place, just by running them in a loop.</p>



<p>The full PHP script now looks like this:</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';

/*
 * Create a new Custom Field for your page
 */

$custom_field_name        = "My New Custom Field";
$custom_field_description = "My New Custom Field Awesome Description So I Can Easily Find It Back Later";
$custom_field_type        = "text"; // text|number|date|datetime|boolean

$page_createCustomField = $mc->createCustomField($custom_field_name, $mc_api, $custom_field_description, $custom_field_type);

if ($page_createCustomField->status == "success") {
    echo "You created a Custom Field with the name " . $page_createCustomField->data->field->name;
} else {
    echo "We could not create your new Custom Field";
}
</pre>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
