Coder Social home page Coder Social logo

tompaana / twitter-bot-fw-integration Goto Github PK

View Code? Open in Web Editor NEW
16.0 4.0 5.0 27 KB

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.

License: Other

C# 100.00%
twitter microsoft-bot-framework chatbot

twitter-bot-fw-integration's Introduction

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:

    1. Navigate to the Twitter Application Management portal
    2. Select Create New App
    3. Fill in the details and click Create your Twitter application
    4. Once your application is created, navigate to the Permissions tab, select Read, Write and Access direct messages and click Update Settings
    5. 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
  1. 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>
  2. Run the solution (make sure that TwitterBotSample is set as the start-up project).

  3. While the solution is running, send a Direct Message in Twitter to the Twitter user/account associated with your Twitter app.

  4. That's it! You should now be receiving replies from your bot.

Class library implementation

The class library (here) consists of three main classes:

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.

twitter-bot-fw-integration's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

twitter-bot-fw-integration's Issues

duplicate messages

Hi,
Sometimes I receive the same response twice, attached to the response of the next iteration.
Other times, the bot simply halts... specially If I use formflows or send many postasync msgs and a context.wait later

Null exception on StartStream

I tried to run this project on a new PC and it throws a null exception on the line 26 in Program.cs, why could it be?

thanks!

Not receiving proactive messages

When trying to send proactive messages twitter doesn't show anything, the code I am using is the following:

var userAccount = new ChannelAccount() {Id = activityRecibida.FromId, Name = activityRecibida.FromName};
                var botAccount = new ChannelAccount()
                {
                    Id = activityRecibida.RecipientId,
                    Name = activityRecibida.RecipientName
                };
                string url = activityRecibida.ServiceUrl;

                
                MicrosoftAppCredentials.TrustServiceUrl(url, DateTime.Now.AddYears(2));
                var appId = System.Configuration.ConfigurationManager.AppSettings["MicrosoftAppId"];
                var appPassword = System.Configuration.ConfigurationManager.AppSettings["MicrosoftAppPassword"];
                var account = new MicrosoftAppCredentials(appId, appPassword);
                var connector = new ConnectorClient(new Uri(url), account);

                IMessageActivity activity = Activity.CreateMessageActivity();
                activity.From = botAccount;
                activity.Recipient = userAccount;
                activity.Conversation = new ConversationAccount() {Id = activityRecibida.ConversationId};
                activity.Text = $"[{from}]: " + message;
                activity.Locale = activityRecibida.Local;
                var response = await connector.Conversations.SendToConversationAsync((Activity) activity);
                await activityLogger.LogAsync(activity);

It works on facebook, emulator and skype, but here it simply doesn't. Do you have any clue? Thanks

Enable multiple bot responses via Direct Line (Was: Why track _twitterUsersWaitingForReply ?)

In the TwitterBotIntegrationManager.cs, ReplyInTwitter method, you send a response and remove the user _twitterUsersWaitingForReply.Remove(messageIdAndTimestamp);

I'm trying to send multiple messages in response to a user's tweet so this will send the first message, but not the 2nd and 3rd as the _twitterUsersWaitingForReply.Remove(messageIdAndTimestamp); has been called.

If I comment this out, my 3 replies get delivered, but I didnt go through all the code to see the significance of the _twitterUsersWaitingForReply List.

Cant we just send a message to a user? Why do we care that they are "awaiting a reply" ?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.