<?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>Send Content &#8211; Chatbot Dojo</title>
	<atom:link href="https://v3.chatbotdojo.com/topic-category/send-content/feed/" rel="self" type="application/rss+xml" />
	<link>https://v3.chatbotdojo.com</link>
	<description></description>
	<lastBuildDate>Sun, 27 Sep 2020 22:21:35 +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>Send Content &#8211; Chatbot Dojo</title>
	<link>https://v3.chatbotdojo.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to send content with /fb/sending/sendContent</title>
		<link>https://v3.chatbotdojo.com/topic/how-to-send-content-with-fb-sending-sendcontent/</link>
		
		<dc:creator><![CDATA[ninjawarrior]]></dc:creator>
		<pubDate>Mon, 21 Sep 2020 10:15:58 +0000</pubDate>
				<guid isPermaLink="false">https://v3.chatbotdojo.com/?post_type=sfwd-topic&#038;p=25276</guid>

					<description><![CDATA[Great! You&#8217;re here so that means you finished your Page Based API lessons. I hope they were clear to you and you already made some progress in testing them all out on your end. Today we&#8217;re moving on to sending content using the send content API, instead of through the Manychat interface or chatbot in &#8230;<p class="read-more"> <a class="" href="https://v3.chatbotdojo.com/topic/how-to-send-content-with-fb-sending-sendcontent/"> <span class="screen-reader-text">How to send content with /fb/sending/sendContent</span> Read More &#187;</a></p>]]></description>
										<content:encoded><![CDATA[
<p>Great! You&#8217;re here so that means you finished your Page Based API lessons. I hope they were clear to you and you already made some progress in testing them all out on your end.</p>



<p>Today we&#8217;re moving on to <em>sending content</em> using the send content API, instead of through the Manychat interface or chatbot in messenger.</p>



<h2>Sending content</h2>



<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/sending/sendContent" -H "accept: application/json" -H "Content-Type: application/json" -d "{ \"subscriber_id\": 0, \"data\": {}, \"message_tag\": \"ACCOUNT_UPDATE\", \"otn_topic_name\": \"Сhannel news\"}"</pre>



<p>If you look closely, you can see there are a few important variables in the command, such as the <strong>message_tag</strong> and the <strong>otn_topic_name</strong>.</p>



<p>The purpose of this API is to send your subscriber some content from anywhere other than the ManyChat interface.</p>



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



<p>A use case could be: sending your customer a message when an order is shipped, user credits to your program expire or reach a certain level etcetera.</p>



<p>The format for the content is this (obviously with sample message tag and topic name):</p>



<pre class="EnlighterJSRAW" data-enlighter-language="json" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">{
  "subscriber_id": 0,
  "data": {},
  "message_tag": "ACCOUNT_UPDATE",
  "otn_topic_name": "Сhannel news"
}</pre>



<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>How do we use this in our own system?</h2>



<p>Let&#8217;s make this chapter into a sample use case of a SaaS platform for Job opportunities. We will create a form with the ability to send a user an update about his or her job application.</p>



<p>Let&#8217;s make some assumptions for this, e.g.:</p>



<ul><li>the user has given us consent to send an update message about the job application (OTN: job application update)</li><li>we saved the user&#8217;s ManyChat ID in our system on its initial contact, so we can actually send a message</li></ul>



<p>What do we need for this to work?</p>



<ul><li>The API for sendContent</li><li>A JSON formatted message</li><li>An interface to change a status and send the message</li></ul>



<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="560" data-enlighter-title="" data-enlighter-group="">    /**
     * 
     * @param type $json_message
     * @param type $mc_api
     */
    function sendContent($json_message, $mc_api = '123456:ABCDEF')
    {

        // initialize cURL
        $ch            = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://api.manychat.com/fb/sending/sendContent");
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $json_message . 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);
        // make the request
        $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:36px" aria-hidden="true" class="wp-block-spacer"></div>



<h2>The PHP/HTML file</h2>



<p>Next we created an entire file with form fields, select inputs and a script to handle the Post request. The complete file is below. You can also keep it smaller by splitting the file, or even use jQuery/AJAX to submit your form. That is entirely up to you.</p>



<blockquote class="wp-block-quote is-style-large"><p>If you look closely in the code, you might notice something cool. I&#8217;ll quiz you later.</p></blockquote>



<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;!DOCTYPE html>
&lt;html>
    &lt;head>
        &lt;title>Send Content&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;?php
        /*
         * Load the ManyChat Config File
         */
        require_once './config.inc.php';

        /*
         * Create Page and Form
         */
        if (filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING)):

            /*
             * JSON Format  and POST method 
             */

            $json_arr = ["version" => "v2"];

            $content['messages'][] = [
                "type" => "text",
                "text" => "Hi, we have an update for your job application. Your status has been changed to: " . filter_input(INPUT_POST, 'sc_message')
            ];

            if (!empty(filter_input(INPUT_POST, 'next_flow_id'))) {
                $content['quick_replies'][] = [
                    "type"    => "flow",
                    "caption" => "Learn More >>",
                    "target"  => filter_input(INPUT_POST, 'next_flow_id')
                ];
            }

            // write content
            $json_arr['content'] = $content;

            //convert to json
            $json = json_encode($json_arr);

            // data to be posted
            $complete_message = [
                'subscriber_id'  => filter_input(INPUT_POST, 'mc_uid'),
                'data'           => $json,
                'message_tag'    => filter_input(INPUT_POST, 'message_tag'),
                "otn_topic_name" => filter_input(INPUT_POST, 'otn_topic_name')
            ];

            // encode json data or make string
            $json_message = json_encode($complete_message, JSON_PRETTY_PRINT);

            $sendContent = $mc->sendContent($json_message, $mc_api);

            if ($sendContent->status == "success") {
                echo "Your message is sent";
            } else {
                echo "We could not send your message";
            }

        else:
            ?>

            &lt;form method="post" action="&lt;?= $_SERVER["PHP_SELF"] ?>">
                
                
                &lt;div class="form-group">
                    &lt;label for="message_tag">User ID&lt;/label>                
                    &lt;input type="text" class="form-control" value="123654789" id="mc_uid" name="mc_uid" />
                &lt;/div>

                &lt;div class="form-group">
                    &lt;label for="message_tag">Message Tag&lt;/label>
                    &lt;select class="form-control" name="message_tag" id="message_tag">
                        &lt;option value=''> -- Select a Tag -- &lt;/option>
                        &lt;option value="ACCOUNT_UPDATE">ACCOUNT_UPDATE&lt;/option>
                        &lt;option value="CONFIRMED_EVENT_UPDATE">CONFIRMED_EVENT_UPDATE&lt;/option>
                        &lt;option value="POST_PURCHASE_UPDATE">POST_PURCHASE_UPDATE&lt;/option>
                        &lt;option value="HUMAN_AGENT">HUMAN_AGENT&lt;/option>
                    &lt;/select>
                &lt;/div>

                &lt;div class="form-group">  
                    &lt;label for="otn_topic_name">OTN Topic&lt;/label>
                    &lt;select class="form-control" name="otn_topic_name" id="otn_topic_name">
                        &lt;option value=''> -- Select a Topic -- &lt;/option>
                        &lt;?php
                        $page_otnTopics = $mc->getOtnTopics($mc_api);
                        foreach ($page_otnTopics AS $otnTopics) {
                            echo "&lt;option value='$otnTopics->name'>$otnTopics->name&lt;/option>";
                        }
                        ?>
                    &lt;/select>
                &lt;/div>

                &lt;div class="form-group">
                    &lt;label for="next_flow_id">New Flow to Send User to (optional)&lt;/label>
                    &lt;?php
                    $page_getFlows = json_decode($mc->getFlows($mc_api));
                    echo "&lt;select name='next_flow_id' class='form-control'>";
                    echo "&lt;option value=''> -- Select a Flow -- &lt;/option>";
                    foreach ($page_getFlows->flows AS $pageflows) {
                        echo "&lt;option value='$pageflows->flowId'>$pageflows->flowName&lt;/option>";
                    }
                    echo "&lt;/select>";
                    ?>
                &lt;/div>

                &lt;div class="form-group">
                    &lt;label for="sc_message">Message Update Status&lt;/label>
                    &lt;select class="form-control" name="sc_message" id="sc_message">
                        &lt;option value="Interview Pending">Interview Pending&lt;/option>
                        &lt;option value="Assessment Planned">Assessment Planned&lt;/option>
                        &lt;option value="Rejected">Rejected&lt;/option>
                        &lt;option value="Hired">Hired&lt;/option>
                    &lt;/select>
                &lt;/div>            

                &lt;button type="submit" class="btn btn-primary">Send Content&lt;/button>

            &lt;/form>

        &lt;?php
        endif;
        ?>        

        &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>



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



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



<figure class="wp-block-image size-large"><img loading="lazy" width="566" height="498" src="https://v3.chatbotdojo.com/wp-content/uploads/2020/09/image-2.png" alt="" class="wp-image-25291" srcset="https://v3.chatbotdojo.com/wp-content/uploads/2020/09/image-2.png 566w, https://v3.chatbotdojo.com/wp-content/uploads/2020/09/image-2-300x264.png 300w, https://v3.chatbotdojo.com/wp-content/uploads/2020/09/image-2-341x300.png 341w" sizes="(max-width: 566px) 100vw, 566px" /></figure>



<p>Did you check the code and noticed something? We actually used a few things you already studied in the Page API chapter. So you see how you implement things in real use cases already.</p>



<p>Let&#8217;s take a quiz.</p>


<div id="ld-course-list-content-00542e10b4ca18bac49ca15d505cc0a4" class="ld-course-list-content" data-shortcode-atts="{&quot;show_thumbnail&quot;:&quot;&quot;,&quot;course_grid&quot;:&quot;&quot;,&quot;post_type&quot;:&quot;sfwd-quiz&quot;,&quot;mycourses&quot;:false,&quot;status&quot;:false}">
			<div class="ld-course-list-items row">
		</div>
			</div>
		<br style='clear:both'>


<p>How did you do? Wasn&#8217;t that cool, seeing a real thing you learned? If you came all this way, you&#8217;re already way ahead of so many other basic users. Great job!</p>



<p>If you have configured it all correctly, you can press the Submit button and the result will be the notice that your message was sent. Just enter your own ManyChat userID in there (normally this field will be a (hidden) field that is, for example, populated through your system) and send a message to yourself.</p>



<p>All that will be returned is this in your browser, and the configured message in your messenger.</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="232" height="136" src="https://v3.chatbotdojo.com/wp-content/uploads/2020/09/image-3.png" alt="" class="wp-image-25309"/></figure>



<p>I suggest you copy the files in this topic and do some tryouts and then move on to the next topic. See you there!</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
