Twitter

Hybrid Chat supports Twitter Direct Messaging integration for private user-to-user messages. It supports text message exchange via Twitter DM. It doesn't support subscribing and responding to Twitter Feeds, hashtags.

Twitter Integration Prerequisites

Get access to Twitter Developer Account (with Standard API Access)

To develop or deploy any Twitter integrations, you must have a Twitter Developer account, where you will set up a Twitter App upon which you will integrate your backend system - the application that is to interact with the Twitter Platform. Follow Twitter Getting Started to apply for a developer account to integrate HC with Twitter API.

The review feedback will be sent to the email you provided during the application process. Once the API access application has been approved, you can proceed to the below step for App Creation In Twitter Developer Account.

App Creation on Twitter Developer Account

  1. Log in to Twitter Developer Account https://developer.twitter.com/en/portal/dashboard
  2.  Create a Project and then create an app. i.e. provide a name for the app, like for example “EF-Twitter-App”.
  3. Generate the Keys and Access Tokens from the “Key and Tokens” Tab as shown in the screenshot below:                                                                                         

     

4. Generate Consumer Key, Consumer Secret, and access tokens. Make sure to save these keys somewhere save as you will need to configure them in the CCM (Channel-Manager variable.env file)

    

     

5. After you've created the APP and Generated the Keys & Tokens. Navigate to the following tab “Settings” and change the App permissions to Read+Write+Direct Messages. To do this, click on “Edit”.



     


6. Copy The Keys & Access Tokens you generated in Step 3 into a secure location for future reference.

7. Create a Lab Environment where you will access your app, Navigate as follows

Then Create the environment as follow:

      


Keep Note of this environment name, as you will need it when registering CCM’s Twitter webhook with Twitter.


CCM configuration for Twitter app

Since Twitter is cloud-based, to integrate it with CCM, the Twitter endpoint inside the CCM MUST be accessible publicly and MUST also be hosted in a secure host with a valid SSL certificate.

The Twitter endpoint in CCM will be as follows: https://FQDN/ccm/webhook/twitter

Before integrating this endpoint to Twitter for us to receive Live Twitter Direct Messaging Events, we need to configure the Keys and Access Tokens we generated above in the App creation Section. Copy and Paste Keys/Access token in ccm environment_variable.env file as following;

 

 1. Twitter Configuration in CCM .env file

VariableDescriptionValue
TWITTER_CONSUMER_SECRETTwitter Consumer secret that you generate from your twitter developer account: https://developer.twitter.com/en/portal/dashboard{CONSUMER_SECRET}
TWITTER_CONSUMER_KEYTwitter Consumer key that you generate from your twitter developer account: https://developer.twitter.com/en/portal/dashboard{CONSUMER_KEY}
TWITTER_ACCESS_TOKENTwitter Access Token that you generate from your twitter developer account: https://developer.twitter.com/en/portal/dashboard{ACCESS_TOKEN}
TWITTER_ACCESS_SECRETTwitter Access Secret that you generate from your twitter developer account: https://developer.twitter.com/en/portal/dashboard{ACCESS_SECRET}
TWITTER_ACCOUNT_ID See here how to generate THE TWITTER_ACCOUNT_ID to map to this variable.

{TWITTER_USERID}


Generate your TWITTER_ACCOUNT_ID from Twitter API

  1. Open the Insomnia Tool
  2. Choose Method as GET
  3. Paste the Twitter API URL on the address bar: https://api.twitter.com/2/users/by/username/Harriso99908721.  The highlighted part is the twitter _username for the account you are integrating, Replace this with the Twitter Account username.
  4.  Copy the BEARER_TOKEN into the Bearer Auth field
  5. Click SEND
  6. Copy the Id returned in the response and save it in your ccm_variable.env TWITTER_ACCOUNT_I


 2. CCM’s Twitter Endpoint Integration with Twitter

The CCM’s Twitter endpoint https://FQDN/ccm/webhook/twitter is the webhook that will receive the twitter events. So twitter should be conjured to send all messaging events related to our Twitter Account to these endpoints.

However, Twitter doesn’t accept the absolute URL as it is, so we need to Encode the URL. You can use any encoding tool available or you can use Postman to encode this URL.

To encode the URL using the Postman Tool. Paste the URL on the address bar in Postman, and then select the “encode” option by clicking the 3 dots shown in the screenshot below.



The encoded URL will look similar to this :  https%3A%2F%2FFQDN%2Fccm%2Fwebhook%2Ftwitter

A.   Registering the CCM’s Twitter endpoint (as a webhook) in Twitter

To register a webhook, you can use Insomnia too, you can find it here. Once you install then you may proceed as follows;

  1. Create a POST request
  2. Paste URL: https://api.twitter.com/1.1/account_activity/all/research/webhooks.json?url=https%3A%2F%2F2f9ac4d60faa.ngrok.io%2Fwebhook%2Ftwitterchat ..

 

Replace “research” with the Development Environment Label that you created on the Twitter developer platform.

 

Replace the second highlighted part of the URL with the Encoded webhook URL

       3. Choose Body as “JSON”

       4. Authentication as “OAuth 1.0” 


Replace:

            

  • Consumer key: with the “API key” (you generated for the app) 
  • Consumer secret: with the “API secret”
  • Token: with the “access token”
  • Token Secret: with the “Access secret”
  • Choose SIGNATURE METHOD as “HMAC-SHA1”

Leave the rest of the field in default values.

Then click send.

 

Note: Make sure to copy and save the id returned in the response body, as you may need it for troubleshooting in the future troubleshooting steps.

 


B. Subscribing the webhook events

This part will activate the webhook to receive the messaging events from Twitter.

 

  1. Create a POST request
  2. Paste URL: https://api.twitter.com/1.1/account_activity/all/research/subscriptions.json

 

 Replace “research” with the Development Environment Label that you created on the Twitter developer platform.

       3. Choose Body as “JSON”

       4. Authentication as “OAuth 1.0”


  Replace:

            

  • Consumer key: with the “API key” (you generated for the app) 
  • Consumer secret: with the “API secret”
  • Token: with the “access token”
  • Token Secret: with the “Access secret”
  • Choose SIGNATURE METHOD as “HMAC-SHA1”

 Leave the rest of the field in the default value

 For Success Result:  Status code: 201 - Created.

 

Replacing a webhook URL currently integrated into Twitter

From time to time you might want to change the webhook URL which Twitter calls to pass messaging events. One more likely reason to change this URL is when the HC FQDN has changed to a new FQDN.. In this case, the FQDN which forms the webhook URL has changed and twitter will expect you to reflect this change in your webhook configurations as well. 

So, how do we change the Webhook?

Changing the Webhook URL is a simple process which involves only 4 steps. Let's dive into these steps:

A. Extract the webhook id from Twitter for the current webhook that you want to change.

  – - Replace the environment label {research} in the URL https://api.twitter.com/1.1/account_activity/all/research/webhooks.json

  --- Replace {Token} with your valid Bearer Token

Copy the Id returned in the response

B. Deleting the old webhook URL's configurations

-- Replace the id in the URL https://api.twitter.com/1.1/account_activity/all/research/webhooks/1456556981267873810.json  with Id extracted from response in Step A, above.

-- Replace the Consumer Key, Consumer Secret, Token, Token Secret with your valid onces.

Success: If it was successful, it will give you 204 - No Content Status, as shown in screenshot above

C. Register the new Webhook URL

Follow the procedure described in Step: Registering the CCM’s Twitter endpoint (as a webhook) in Twitter above

D. Subscribe to receive Webhook events from the new Webhook

Follow the procedure described in Step: Subscribing the webhook events above