I recently integrated API.AI with Twitter to create a Twitter bot. While the steps seem to be fairly straight forward, there are a couple of small issues that could trip you up.
Create a Twitter app
Go to the Twitter apps page while being signed in with the Twitter bot account and click on the "Create new app" button. In my case, this is what the new app screen looks like.
Make sure to select the Terms checkbox. Once you click on the Create new application button at the bottom of this page, you will be taken to the following screen which says that the application has been successfully created. Now click on the Keys and Access Tokens tab.
Click on "modify app permissions" link. This step must be done before generating the access token.
Change permissions to "Read, write and access direct messages"
Click on "Update settings" button on that page to save the settings.
Go back to the "Keys and Access tokens" tab. Now click on "Create my access token" button at the bottom of the page.
Create a new API.AI agent as you normally would.
In the Twitter integration popup, enter the values as indicated in the images below making sure you get the values marked 1, 2, 3 and 4 correctly in the popup. Now click the "Start" button and your Twitter bot is ready to respond to your tweets. I simply used the twitter handle where it asks for Bot name, although that may not be necessary as per the documentation.
Now tweet to your Twitter bot's handle from another account. You don't need to do anything special, no need to DM, or follow or anything else. Just send a tweet to the bot handle with a greeting.(In my Default welcome Intent, I added "hi" into the user says box)
— Aravind Mohanoor (@aravindmc) March 22, 2017
And your Twitter bot should respond back.
— BookShelfBot (@bookshelfbot) March 22, 2017
Hope that helps! If you are facing any issues, leave a comment below and I will try and address it.
Update 23 June 2017:
After getting some requests from the API.AI forum thread, I tried to retrace my steps and create a new Twitter bot from scratch.
And I got it to work as expected too.
Mostly I followed the same steps, but I have found somethings in Twitter's sign up process which seem to have changed, and might be affecting your Twitter API.AI bot.
First, do you notice that in my old bot, both the name and bot handle are the same?
This time, quite by accident, I failed to do this.
So during the sign up, I used the name apiaidemo1, and then when I created the handle I chose apiaidemobot.
Let me also tell you one more small change I did from the last time.
When I modified the app permissions as mentioned in my original post, I saw the following alert:
So I decided to wait about 5 minutes before generating my access token. I am not sure if that really matters, but when you think about it, if there is anything under the hood which involves two writes to a database (permission change + new access token), and you don't have guaranteed ordering of the writes, then us humble humans have to simply wait for our machine overlords until they are ready. 🙂
Once I created the access token, I followed the same steps as my original article (including using the Twitter handle as the bot name).
And it seems to work as before:
What if you still cannot get it to work?
I will give you a few suggestions, from most likely to help to least:
- Start from scratch and create a new Twitter profile instead of using your existing one and use different values for name and bot handle
- Create a new API.AI agent for your Twitter bot (maybe something was broken in the interim)
- Change your Twitter account name to make sure it differs from your Twitter handle and try again
- Create a new Twitter app and use its settings, be sure to wait for a while before creating access token
- Modify the permissions of the existing Twitter app and recreate the access token
- Use my Twitter bot for your work! OK, that's just a joke.