Setting Up Automated Comment Responses with Context Using an External API

Modified on Mon, 20 Jan at 8:04 AM

Overview

This guide details the steps to integrate your Facebook page with Chatbot Builder AI (CBB) for automated, context-aware comment replies. By utilising Facebook’s Graph API and OpenAI’s capabilities, you can enhance your bot’s ability to respond with tailored, on-brand replies based on post captions and user comments.


Step 1: Set Up a Facebook App

Access Meta Business Manager https://business.facebook.com/

Navigate to Business Manager > Apps.


Click Add New App

Then click Create a new app ID and name it Chatbot Builder AI


Leave the app contact email as is.


Choose Use Case and App Type:

Select Other under Use Cases and click Next.

Choose Business as the app type.


Review all details and click Create App. For the business Portfolio if you can choose, please select your main one


Go to the Graph API Explorer to test API calls and validate your app setup.


This is how it should look for you.


Step 2: Add Permissions


Core Permissions: Add these essential permissions to your app:

  • email

  • catalog_management

  • pages_show_list

  • ads_management

  • ads_read

  • business_management

  • pages_messaging

  1. Instagram and WhatsApp Permissions: Include:

    • instagram_basic

    • instagram_manage_comments

    • instagram_manage_insights

    • instagram_manage_messages

    • whatsapp_business_management

    • whatsapp_business_messaging

  2. Extended Facebook Page Permissions: Add:

    • page_events

    • pages_read_engagement

    • pages_manage_metadata

    • pages_read_user_content

    • pages_manage_ads

    • pages_manage_engagement

    • leads_retrieval

Save Changes:

After adding all permissions, review and save your settings by clicking Generate access token



Step 3: Generate and Assign Access Token


Generate the Token:

Click Generate Access Token at the top of the API Explorer.


Opt into all current and future Facebook pages for all steps it's recommended to Opt into all current and future options….



 


 



Review Chatbot Builder AI's access request and Click Save.


Save the Token Securely:

First, be sure to select your page from the drop-down under User or Page


 

once you have selected your page from the drop-down menu you can go ahead and copy the Access token above.


Step 4: Create the Flow in Chatbot Builder AI


Create a New Flow:

Name it Dynamic Facebook Comment Responder or something relevant.


 



Add an External Request Node:

Add an External Request as the starting node.


 

First, ensure that it is a GET Request type then In the Request URL field, input:

https://graph.facebook.com/v21.0/{{last_post_id}}?fields=message,permalink_url,created_time,id&access_token=YOUR_ACCESS_TOKEN

Replace YOUR_ACCESS_TOKEN with the token copied in Step 3.


 


Test the Request:

Click Test Request to verify that the API returns the expected post data (e.g., caption, permalink URL, post ID).


 

Map the Response:

Under Body Response Mapping, enter http_response_body in the JSON Path field.

Save the data to a CUF, e.g., ai_json_long. 


Step 5: Add OpenAI Integration


Configure the AI Action:


Add an OpenAI Action to the flow.


 

Set up the Business Prompt:

You are a helpful assistant for [Enter Your Business Name here]. Respond to Facebook comments referencing the caption below:

{{ai_json_long}}


In the User Message field, input {{last_fb_comment}}.

Save the response to a CUF, e.g., Comment_reply.


 

Add a Text Block:

Add a text block to output the AI-generated response.

Set the output to {{Comment_reply}}.


 
 

Publish the Flow:

Click Publish to finalize the flow.


Step 6: Configure Facebook Comment Reply


Access the Marketing Section:

Navigate to Marketing > Facebook Comment Reply.


 

Click Add New to create a new comment reply setup.


 

Set Up Replies:

Name the setup, e.g., Social Media Comment Responder.

Choose to apply the responder to all posts or specific posts.


Private and Public Replies:

Private Reply: Select a relevant flow or input a static message.

Public Reply: Choose the flow created in Step 4.


 

Advanced Options:

Adjust settings for delays, exclusions, or specific responses.




Enable the Setup:

Toggle ON the Facebook Comment Reply feature.



 

Step 7: Test and Optimize

  1. Test the Integration:

    • Post a test comment on your Facebook page to verify the bot responds appropriately.

    • Ensure the response aligns with the post context.

  2. Monitor API Usage:

    • Check for errors in the API Explorer and adjust permissions or access tokens as necessary.

  3. Optimize Responses:

    • Refine the OpenAI prompt to improve response quality.

    • Use CUFs to store additional data for enhanced personalization.


Pro Tips for Success

  1. Token Expiration:

    • Regularly refresh your access token and update it in your flow to avoid disruptions.

  2. Error Handling:

    • Add error-handling logic in your flow to manage API timeouts or invalid responses.

  3. Analytics:

    • Use CBB’s analytics tools to monitor engagement and improve response effectiveness.

Part 2: Managing Facebook Access Tokens for CBB Flowbuilder

Tokens can expire. This section shows you how to handle short-lived, long-livet tokens so your chatbot doesn’t stop unexpectedly.

Step 1: Converting a Short-Lived Token to a Long-Lived Token

What You Need

  • The short-lived token from the Graph API Explorer.

  • Your Facebook App ID and App Secret.

Convert It

In your browser url section, go to:

https://graph.facebook.com/v20.0/oauth/access_token?

grant_type=fb_exchange_token

&client_id=YOUR_APP_ID

&client_secret=YOUR_APP_SECRET

&fb_exchange_token=SHORT_LIVED_TOKEN

(be sure to enter your app secret and app id + your Short lived token where the place holders are)

Grab the New Token

You’ll see a response like this:

{

"access_token": "LONG_LIVED_TOKEN",

"token_type": "bearer",

"expires_in": 5183849

}

Copy the LONG_LIVED_TOKEN.

Update CBB Integration

Go back to your flow in CBB in the request url section of the external request node and replace YOUR_ACCESS_TOKEN with LONG_LIVED_TOKEN.


Pro Tips for Success

  • Token Expiration

     If your token expires, your chatbot can’t reply.

     Set a reminder to refresh. Also in the case that its not working but you have done everything correct please refresh permissions for Facebook in CBB by going to Settings>Channels>Facebook Messenger>Refresh Permissions

  • Error Handling

     In your flow, add steps to handle errors (e.g., if Facebook is down or the token is invalid).

     A simple fallback message like “Sorry, something went wrong. Please try again later!” is helpful.

Analytics

Use CBB’s analytics to track how many comments you receive and how users interact with your replies.

Tweak your AI prompt over time to improve the bot’s style and helpfulness.


Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article