Let your Teneo bot respond to tweets and DM's on Twitter using the Teneo Twitter connector.
This Node.js connector enables communication between a Teneo bot and Twitter users via Direct Messages and Tweets. The connector uses Twitter's Account Activity API to detect and react to incoming Direct Messages and Tweets, without quickly exhausting Twitter's Rate Limit. This connector is partially based on the account-activity-dashboard.
Prerequisites
Teneo Engine
Your bot needs to be published and you need to know the Engine URL.
Twitter account
You will need to apply for a Twitter Developer Account and then create a Twitter App, as described ahead.
HTTPS
An Azure account with an active subscription is required. Create an account for free.
To run the connector locally, ngrok is preferred to make the connector available via HTTPS.
Node
GETTING STARTED
Create a Twitter app
- Apply for a Twitter Developer account on developer.twitter.com, select "Making a Bot", and fill in all required fields. On the Specifics section, set "Will your app use Tweet, Retweet, like, follow, or Direct Message functionality?" to YES. You may set all other options to No. Continue filling in required descriptions and validate Twitter's activation email.
- Create a Twitter App here. Tap "Create an App". You will be prompted to apply for a Twitter Developer account. Tap "Apply".
- Fill all required fields and tap "Let's do this"
- Accept the Developer Agreement & policy terms
- After you name your app, tap the "get keys" button
- Take note of your keys and tokens tap "Dashboard" (and confirm).
Create a Twitter Dev Environment
- Tap in the "Projects & Apps" section on the left of your dashboard, click on your project name
- Apply for Do you need Elevated access for your Project.
- Fill all required fields and tap "next"
- Specify how will you use twitter data, required questions and hit "next"
- Hit next on verification of information.
- Accept developer agreement & policy
Once your request for elevated Access is approved you should have a list of environments.
In the Account Activity API/Sandbox section, click Setup Dev Environment. Name your environment and link this new environment to the App you created in the previous step and take note of the environment_label
for later use.
Connector Setup Instructions
There are some ways of running this connector and described ahead.
- You can run the connector online with Azure.
- You can Run the connector locally or deploying it on a server of your choice. This is preferred if you're familiar with Node.js development and want to have a closer look at the code, or to implement modifications and enhancements.
Running the connector with Azure & Docker
Prerequisites
- Install Azure CLI.
- You must also have Docker installed locally.
Build and set up this repository
-
Click the button below to deploy the registry template to Azure
-
After login you will be prompted to fill deployment basic details for your registry.
-
Hit "Review and create"
-
After validation passed hit create
-
You will be directed to the Overview page after deployment is complete and hit the "Go to resource group" button and you will see your resources click on your App service.
-
Copy the login server from your app You will need it for the next step.
Configure the Twitter App
- Open your app's Details
- Tap the edit button on "User authentication settings"
- Enable Read, Write and direct messages, and "Web App, Automated App or Bot" as a type of APP
- Under App info paste your azure login server into both "callback url" and "website url", hit save.
- Take note of the new keys and save them.
- Under Keys Access Token and Secret click "regenerate" then confirm your option.
- Copy the new Access token and Keys and substitute the old ones.
Build and deploy Docker Image
-
Clone this repository:
bash
1git clone https://github.com/artificialsolutions/tie-api-example-twitter.git 2
-
Create a new file called .env based on .env.sample and fill in your Twitter keys, tokens, Teneo Engine URL. Like so.
env Key Twitter Key TWITTER_CONSUMER_KEY 'Api-key' TWITTER_CONSUMER_SECRET 'Api-Secret' TWITTER_ACCESS_TOKEN 'Access Token' TWITTER_ACCESS_SECRET 'Access Token Secret' TWITTER_WEBHOOK_ENV 'twitter environment name' TENEO_ENGINE_URL 'url for you teneo webchat' -
Create an ACI context which associates Docker with an Azure subscription and resource group so you can create and manage container instances. Remember to Substitute
nameyourcontext
for one of your choosing.bash
1docker login azure 2docker context create aci nameyourcontext 3
When prompted, select your Azure subscription ID, then select an existing resource group (previously created when template deployed)
-
Build the docker image for the connector. Remember to Substitute
imagename
for one of your choosing.bash
1docker build . -t imagename 2
Note: if you are using a Mac "M1 Chip" use: docker buildx build --platform=linux/amd64 -t imagename .
Tag and deploy Docker Image
-
Create an alias of the image with the fully qualified path to your registry. Remember to Substitute
registryname
andimagename
for the one you chose on previous steps.bash
1docker tag imagename registryname.azurecr.io/imagename 2
-
log into the Azure CLI and then authenticate to your registry:
bash
1az login 2az acr login --name registryname 3
-
Push the image to your registry
bash
1docker push registryname.azurecr.io/imagename 2
-
Use the az acr repository list command to verify that the push was successful
bash
1az acr repository list -n registryname 2
-
Change to your recently created ACI context. Remember to Substitute
nameyourcontext
for the one you chose on 'Create an ACI context' step .bash
1docker context use nameyourcontext 2
-
Use the docker run command to run your image from your registry in your context. Remember to Substitute
registryname
andimagename
for the one you chose on previous steps.bash
1docker run -d registryname.azurecr.io/imagename 2
which will return a randomly generated new image name 'RandomGeneratedName'
-
Use the name from the step above to run a bash shell inside the container
bash
1 docker exec -it RandomGeneratedName /bin/bash 2
-
Run our script to get direct messages or mentions
bash
1node example_scripts/get-direct-messages.js 2node example_scripts/get-mentions.js 3
Running the connector locally
Next, we need to make the connector available via https. We'll use ngrok for this.
-
Start ngrok. The connector runs on port 5000 by default, so we need to start ngrok like this:
bash
1ngrok http 5000 2
*Note for Mac Users you might get
bash
1uncaught exception: listen EADDRINUSE: address already in use :::5000
2Error: listen EADDRINUSE: address already in use :::5000
3
Airplay is using port 5000 you can deactivate it in System Preferences › Sharing and uncheck AirPlay Receiver
to release port 5000. (source)
-
Running the command above will display a public forwarding https URL. Copy it, we will use it in the step below.
-
Revisit your app's Details
-
Under App info tap the edit button on "User authentication settings" paste your public forwarding https URL (
https://yoururl.ngrok.io
) into both "callback url" and "website url", hit save.
Setup & run a Node.js web app
-
Clone this repository:
bash
1git clone https://github.com/artificialsolutions/tie-api-example-twitter.git 2
-
Create a new file called .env based on .env.sample and fill in your Twitter keys, tokens, Teneo Engine URL. Like so.
env Key Twitter Key TWITTER_CONSUMER_KEY 'Api-key' TWITTER_CONSUMER_SECRET 'Api-Secret' TWITTER_ACCESS_TOKEN 'Access Token' TWITTER_ACCESS_SECRET 'Access Token Secret' TWITTER_WEBHOOK_ENV 'twitter environment name' TENEO_ENGINE_URL 'url for you teneo webchat' -
Install dependencies locally:
bash
1npm install 2
-
Run our script to get direct messages or mentions
bash
1node example_scripts/get-direct-messages.js 2node example_scripts/get-mentions.js 3
That's it! You can now interact with your Teneo solution powered bot with Direct Messages and Tweet mentions.
Production considerations
This app is for demonstration purposes only. The application can handle light usage, but you may experience API rate limit issues under heavier load. Consider storing data locally in a secure database, caching requests, or applying for a business account for increased Rate Limits.