<?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>Find User By System Field &#8211; Chatbot Dojo</title>
	<atom:link href="https://v3.chatbotdojo.com/topic-category/find-user-by-system-field/feed/" rel="self" type="application/rss+xml" />
	<link>https://v3.chatbotdojo.com</link>
	<description></description>
	<lastBuildDate>Sat, 17 Oct 2020 23:42:59 +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>Find User By System Field &#8211; Chatbot Dojo</title>
	<link>https://v3.chatbotdojo.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to get subscriber info with /fb/subscriber/findBySystemField</title>
		<link>https://v3.chatbotdojo.com/topic/how-to-get-subscriber-info-with-fb-subscriber-findbysystemfield/</link>
		
		<dc:creator><![CDATA[ninjawarrior]]></dc:creator>
		<pubDate>Sat, 17 Oct 2020 13:56:52 +0000</pubDate>
				<guid isPermaLink="false">https://v3.chatbotdojo.com/?post_type=sfwd-topic&#038;p=25415</guid>

					<description><![CDATA[This is the last GET API call in our chapter, after this lesson we start with the POST call API&#8217;s. Similar to the getInfoByUserRef API call, findBySystemField returns one record only in the response. The curl command as displayed on the ManyChat API Swagger is: The &#8216;0&#8217; in the call above must be replaced by &#8230;<p class="read-more"> <a class="" href="https://v3.chatbotdojo.com/topic/how-to-get-subscriber-info-with-fb-subscriber-findbysystemfield/"> <span class="screen-reader-text">How to get subscriber info with /fb/subscriber/findBySystemField</span> Read More &#187;</a></p>]]></description>
										<content:encoded><![CDATA[
<p>This is the last <code>GET</code> API call in our chapter, after this lesson we start with the <code>POST</code> call API&#8217;s.</p>



<p>Similar to the <code><a href="https://v3.chatbotdojo.com/topic/how-to-get-subscriber-info-with-fb-subscriber-getinfobyuserref/" class="rank-math-link">getInfoByUserRef</a></code> API call, <code>findBySystemField</code> returns one record only in the response.</p>



<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 GET "https://api.manychat.com/fb/subscriber/findBySystemField?phone=1234567890&amp;email=mail%40example.com" -H "accept: application/json" -H "Authorization: Bearer 0"</pre>



<blockquote class="wp-block-quote"><p>The &#8216;0&#8217; in the call above must be replaced by your API key and <code>phone</code> OR <code>email</code> must contain the values you are looking for.</p></blockquote>



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



<p>In essence all the methods to get your subscriber data are the same. Some return a single record, others return multiple users. We spoke about use cases such as customer support, user (order) status, segmentation. I&#8217;m sure you can come up with a use case for your personal situation as well. Please do share!</p>



<p>The response for this API is the same as with <code><a href="https://v3.chatbotdojo.com/topic/how-to-get-subscriber-info-with-fb-subscriber-getinfobyuserref/">getInfoByUserRef</a></code>, a single user is a data object. There is a similar limitation, as seen in the ManyChat Swagger:</p>



<blockquote class="wp-block-quote"><p><strong><em>Limit:</em></strong>&nbsp;100 queries per second.</p></blockquote>



<p>The way around the limitation is to pause your script after 100 records have been retrieved and go again right after. You can use the PHP function <code>sleep()</code> or <code>usleep()</code> for that. In a real case, chances are you won&#8217;t need more than 100 queries per second though.</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="">{
  "status": "success",
  "data": {
    "id": "string",
    "page_id": "string",
    "user_refs": "[{ 'user_ref':'-1543835812530302162', 'opted_in':'2018-12-03T11:17:54+00:00' }]",
    "first_name": "string",
    "last_name": "string",
    "name": "string",
    "gender": "string",
    "profile_pic": "string",
    "locale": "string",
    "language": "string",
    "timezone": "string",
    "live_chat_url": "string",
    "last_input_text": "string",
    "optin_phone": true,
    "phone": "string",
    "optin_email": true,
    "email": "string",
    "subscribed": "'2018-07-02T00:00:00+02:00'",
    "last_interaction": "'2018-07-02T00:00:00+02:00'",
    "last_seen": "'2018-07-02T00:00:00+03:00'",
    "is_followup_enabled": "true",
    "custom_fields": [
      {
        "id": 0,
        "name": "string",
        "type": "text",
        "description": "string",
        "value": "'string', 123, true, '2018-07-18', '2018-07-02T07:45:00+03:00'"
      }
    ],
    "tags": [
      {
        "id": 0,
        "name": "string"
      }
    ]
  }
}</pre>



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



<h2><em>How do we use this in our own system?</em></h2>



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



<ul><li>The API /fb/subscriber/findBySystemField</li><li>a script to translate the response JSON into any readable format for your project</li><li>the phone number or email address to look for</li></ul>



<p>Let&#8217;s add our 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="805" data-enlighter-title="" data-enlighter-group="">    /**
     * 
     * @param type $systemField
     * @param type $systemFieldValue
     * @param type $mc_api
     * @return boolean
     */
    function findSubscriberBySystemField($systemField, $systemFieldValue, $mc_api)
    {

        $ch            = curl_init();
        curl_setopt($ch, CURLOPT_URL, "https://api.manychat.com/fb/subscriber/findBySystemField?" . $systemField . "=" . $systemFieldValue);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $headers       = [
            'Authorization: Bearer ' . preg_replace("/^(\w+\s+)/", "", $mc_api),
            'Content-Type: application/json',
            'Accept: 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->data;
        } else {
            return false;
        }
    }</pre>



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



<h2>The PHP file</h2>



<p>The complete PHP file to call the method (function) in your class file is as follows. We included a check for a <code>true</code> result just in case there are zero users in the given search. Also, I added a little HTML code to tidy it up in the output.</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';

/*
 * Get the Subscriber Information by SystemField
 */

$systemField      = "phone"; // phone|email
$systemFieldValue = urlencode("+31651524964");

$subscriber_bysystemfield = $mc->findSubscriberBySystemField($systemField, $systemFieldValue, $mc_api);

if ($subscriber_bysystemfield) {
    $subscriber_name = $subscriber_bysystemfield->name;
    $subscriber_pic  = $subscriber_bysystemfield->profile_pic;
    echo "&lt;img src=\"" . $subscriber_pic . "\" align=\"left\" style=\"padding:15px;width:150px;\">";
    echo $subscriber_name;
} else {
    echo "No info available for user with " . $systemField . " " . urldecode($systemFieldValue);
}
</pre>



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



<p>Let&#8217;s see what our current setup returns as a result.</p>



<figure class="wp-block-image size-large"><img loading="lazy" width="362" height="212" src="https://v3.chatbotdojo.com/wp-content/uploads/2020/10/image-4.png" alt="" class="wp-image-25423" srcset="https://v3.chatbotdojo.com/wp-content/uploads/2020/10/image-4.png 362w, https://v3.chatbotdojo.com/wp-content/uploads/2020/10/image-4-300x176.png 300w" sizes="(max-width: 362px) 100vw, 362px" /></figure>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
