How to post updates to Facebook from Pega Platform
Facebook is a highly-popular social networking site. What began as a limited exercise for Harvard undergraduates who wanted to connect or find out about others on campus has become a hub where hundreds of millions of people post daily updates, play games, and get a sense of what is exciting and interesting in the world.
Having a dynamic Facebook presence is becoming essential for companies that want to present as dynamic and connected. You can post updates directly to your Facebook presence from a PRPC V 6.2 or newer application, broadcasting company milestones, job opportunities (you could even include a SnapStart URL which, when clicked, would open a job-application flow in your application), or other matters of interest.
You have to complete several tasks before you can start sending updates from your PRPC application to Facebook. Some tasks are on the Facebook side of the process; some are within PRPC.
At a high level:
- Facebook side
- PRPC side
There are two steps to establishing yourself on Facebook: creating an account and creating an application related to the account. The application is where your updates appear. The application provides a key that you send along with each posting, so the Facebook application knows to accept it.
You as an individual may already have a presence on Facebook, and there is no reason not to use your existing account while experimenting. However, your corporate Facebook presence should have its own account before you starting posting company-related updates "for real"..
Establish a presence on Facebook
Creating a Facebook account is free. When your account exists, log in to it and, at the extreme bottom right of the screen, locate and click the Developers link:
Your Developers area appears. At the top right, locate and click the Apps link:
On the screen that appears, click + Create New App: .
If this is a new account, you may have to go through a one-time Facebook verification process at this point.
When the account is verified, the Create New App form appears:
Provide a reasonable name for the application in the App Name field. The other options here are beyond the scope of this article. Click Continue.
Obtain an authentication token
There are many features and options available for your new application on the screen that appears. Describing them all is beyond the scope of this article. The key elements appear near the top of the display (distorted in this image):
The App ID lets the connector connect to the correct Facebook application. The App Secret key verifies that the message is sent by a trusted source. Copy them into a text file for later use.
Assemble a URL in the following structure, replacing the bolded text with the App ID for your application:
Copy the URL and paste it in the location field of a browser window. The following permission screen appears, and you may have to log in to Facebook with the account you created for this purpose if you are not already logged in:
Click Allow to let your PRPC application access your Facebook account data and post updates to the account's news feed. The browser displays a screen with the message "Success".
Before you close the browser, copy the page's URL and paste it into a text file The URL contains important information you need when configuring your PRPC application. The URL is in this form:
Use the string following code to assemble a new URL in the following pattern:
APP_ID and APP_SECRET are the key strings you received when you created your Facebook application's ID, and CODE_FROM is the string following code in the previous code block.
Paste the URL in a browser window, and the resulting screen displays the access token for your Facebook profile and application:
Copy the access token into a text file and save it for later use.
You can use an existing PRPC application, or create a new one if an existing application is not available. Log in to the application using credentials for an operator with developer privileges.
Establish a connection from your application to your Facebook presence
To establish a connection from PRPC to Facebook, first create four text properties to hold strings the connector requires. Save them in the class where you intend to create the connector rule. The four properties are:
Create a Connect-HTTP rule. In this example its name is PostMessageFacebook.
On the Service tab provide the following information:
- HTTP Method - GET
- HTTP Version - 1.1
- Response Timeout - 30000
- Status Value - .pyStatusValue
- Status Message Property - .pyStatusMessage
- Error Handler Flow - ConnectionProblem
On the Request tab provide the query string parameters:
Name Description Map From Map From Key callback callback Clipboard FB_Callback message message Clipboard FB_Message target_id target_id Clipboard FB_Target_ID access_token access_token Clipboard FB_Access_Token
Create an activity to post an update to the Facebook account. In this example its name is PostMessageToFacebook:
On the Steps tab:
- In Step 1 use the Property-Setmethod. You can set the access token in several ways. In this example we use a static value:
PropertyName PropertyValue .FB_Access_Token [The access token you created using Facebook URLs and saved to a text file] .FB_Callback "XML"
- In Step 2 use the Connect-HTTPmethod. Set the parameters:
Parameter Value ServiceName PostMessageFacebook (the connect rule you created earlier) ExecutionMode Run EndPointURL https://api.facebook.com/method/stream.publish
Create a process where you can write and send updates
Create a section with a field for the Facebook ID to use and the message to send. In this example the section's name is Facebook. Use the properties FB_Target_ID and FB_Message for the input fields:
You can insert sending a Facebook update into an existing flow, or include it in its own subflow. This exercise uses a simple flow, SendMessageFacebook:
At runtime, the flow creates a work item and puts it in the user's worklist. The item has one action option, to send a message to Facebook. The flow action displays the section you created.
Enter the Facebook ID for the account with the application you created, add a message, and click Submit. The message appears in the Facebook account's status update stream: