twitter-bot-fw-integration
twitter-bot-fw-integration copied to clipboard
A class library with a sample demonstrating how to add Twitter as one of the Microsoft Bot Framework channels with the help of Direct Line API.
Twitter - Bot Framework Integration
Running and testing the sample
Prerequisites
The assumption here is that you already have a bot created. If not, go to the Bot Framework developer portal to learn how to create one. Note that you need to have Direct Line enabled and you will need the Direct Line secret.
-
You need to have a Twitter account. If you don't have one, sign up at twitter.com
-
Create a Twitter app:
- Navigate to the Twitter Application Management portal
- Select Create New App
- Fill in the details and click Create your Twitter application
- Once your application is created, navigate to the Permissions tab, select Read, Write and Access direct messages and click Update Settings
- Navigate to the Keys and Access Tokens tab
- Since we changed the permissions, we need to regenerate the secrets - so click Regenerate Consumer Key and Secret
- Under Token Actions click Create my access token
- Now collect the following credentials (we will need these later):
- Consumer Key (API key)
- Consumer Secret (API Secret)
- Access Token
- Access Token Secret
Running the solution
Now you should have the following details at hand:
- Bot Framework
- Direct Line secret
- Twitter
- Consumer Key (API key)
- Consumer Secret (API Secret)
- Access Token
- Access Token Secret
-
Open the solution (TwitterBotSample.sln) in Visual Studio, locate the Secrets.config file and insert the aforementioned keys and secrets there.
<appSettings> <add key="directLineSecret" value="BOT DIRECT LINE SECRET HERE" /> <add key="consumerKey" value="TWITTER CONSUMER KEY HERE" /> <add key="consumerSecret" value="TWITTER CONSUMER SECRET HERE" /> <add key="accessToken" value="TWITTER ACCESS TOKEN HERE" /> <add key="accessTokenSecret" value="TWITTER ACCESS TOKEN SECRET HERE" /> </appSettings>
-
Run the solution (make sure that
TwitterBotSample
is set as the start-up project). -
While the solution is running, send a Direct Message in Twitter to the Twitter user/account associated with your Twitter app.
- You can verify the user/account in Twitter Application Management portal on Keys and Access Tokens tab under Your Access Token (see Owner).
- Note that it is possible to DM yourself.
-
That's it! You should now be receiving replies from your bot.
Class library implementation
The class library (here) consists of three main classes:
- TwitterBotIntegrationManager is the main class and provides the only API you need to use.
-
DirectLineManager implements the Direct Line
connection with the bot. The class allows you to send and retrieve messages (
Activity
) to/from the bot. - TwitterManager utilizes Tweetinvi library to receive and send Tweets via the Twitter REST API.
Usage
TwitterBotIntegrationManager twitterBotIntegrationManager = new TwitterBotIntegrationManager(
directLineSecret, consumerKey, consumerSecret, accessToken, accessTokenSecret);
twitterBotIntegrationManager.Start();
Remember to dispose the object after it is no longer needed:
twitterBotIntegrationManager.Dispose();
This can be also replaced with using
statement that takes care of disposing the object after the
scope of the statement ends.
See Program.cs of the sample.