0

I'm working on a fairly basic Alexa skill that, in essence, searches through a specific Twitter feed looking for a hashtag, parses that tweet, and reads it back. What's the simplest way to pull data from a Twitter feed? I've been having issues working with the Twitter API (see below) and scraping data from Twitter appears to be against the TOS.

... crawling the Services is permissible if done in accordance with the provisions of the robots.txt file, however, scraping the Services without the prior consent of Twitter is expressly prohibited.

To write an Alexa skill

  1. Follow a tutorial.

    Simple enough. Use a pretty interface to add an invocation, some intents, and a slot type with the data I want.

  2. Write an AWS Lambda function to handle everything.

    Python because I know it better than JavaScript. Pick one of the Python wrappers for the Twitter API, then realize that because Twitter, all of their API requires authentication - even basic searches.

  3. Register an app with Twitter so I have keys and tokens.

  4. Realize having authentication keys and tokens in plaintext in an application is a bad idea, and decide to figure out account linking for Alexa.

  5. Try following the one tutorial around, twice. Have trouble and go to StackExchange.

    Why the need for an external webapp?

    ...we need an OAuth implementation of our own to make the integration work correctly

    What's wrong with the one provided by Twitter? Why can't any issues be fixed within the Lambda method, since the account integration isn't being touched otherwise AFAIK? Isn't having the tokens passed around via the URL a bad idea too? Their example code seems to require that the Consumer Secret be hard coded too.

    Enter: “https://alexa-twitter-airport-info.herokuapp.com/oauth/request_token?vendor_id=XXXXXX&consumer_key=YYYYYY&consumer_secret=ZZZZZZ”.

    At the very least, their webapp seems to be down for the time being, and it'd be nice to have an option that doesn't require paying money to host another copy.

I've seen this post discussing a Node.js OAuth implementation, but the necessity for such an implementation still escapes me.

JMY1000
  • 131
  • 2
  • 9
  • What exactly is the question? – Jonas Aug 22 '17 at 18:12
  • @Jonas What's the simplest way to pull data from a Twitter feed? If the response is "Use the Twitter API", how can I accomplish account linking to authenticate with the API? – JMY1000 Aug 22 '17 at 18:41
  • The [API Console](https://dev.twitter.com/rest/tools/console) is probably the simplest way to authenticate and use the API (no programming required). If you feel comfortable using Python then [this wrapper](https://github.com/geduldig/TwitterAPI) might make it easier for you. – Jonas Aug 22 '17 at 18:57
  • @Jonas I seem to be missing something still. It looks like those still use the REST API and therefore require authentication. – JMY1000 Aug 22 '17 at 19:10
  • That's correct. When you said "account linking to authenticate with the API" I thought you wanted to use your account to authenticate. There is no way around this if you decide to use the API. – Jonas Aug 22 '17 at 19:23
  • Amazon appears to provide a way to have the user link their account though, that way access into my account isn't hard-coded. – JMY1000 Aug 23 '17 at 08:06
  • I see what you you are looking for now. Since this is not your original question, you should create a new question and ask, How does one authenticate a Twitter application with their Twitter user id and password? This is possible with Python or Node. – Jonas Aug 23 '17 at 14:00
  • @Jonas Fair enough, just thought there might be some other way that didn't involve the API. – JMY1000 Aug 23 '17 at 18:52

1 Answers1

0

I found twitter Node package useful doing this. I used search/tweets GET API for my purpose which is shown below. However you have various APIs which are available to work with the tweets. The code below only shows the parts needed to access Twitter API from within Node JS.

var Twitter = require('twitter');
var client = new Twitter({
      // I added the keys as AWS Environment variables
      consumer_key: process.env.consumer_key,
      consumer_secret: process.env.consumer_secret,
      access_token_key: process.env.access_token_key,
      access_token_secret: process.env.access_token_secret
    });

    client.get('search/tweets', {q: <<your search query>>,count:<<if you want to receive specific number of tweets>>}, function(error, tweets, response) {
          var noOfTweets = tweets.statuses.length;
           if(error) {
               speechOutput = "I could not find tweets for <<your reason>>"
               self.emit(':tell', speechOutput);
           } else if(noOfTweets === 0) {
               speechOutput = "another speech output";
               self.emit(':tell', speechOutput);
           } else {
               tweets.statuses.forEach(function(tweet) {
                   //process the tweet the way you want to.
               });
           }
    });
Amit
  • 1,111
  • 1
  • 8
  • 14
  • Doesn't help with the problem that I was having (plus I was using Python, but that's besides the point.) How'd you actually accomplish the account linking so the keys could be added as environment variables. – JMY1000 Aug 22 '17 at 18:40
  • What do you mean by account linking ? You mentioned Node in your post so I thought you are looking for Node JS solution. Moreover I searched for similar library for twitter in Python and found this - https://github.com/bear/python-twitter – Amit Aug 22 '17 at 19:22
  • Letting the user provide their own authentication through Amazon's provided OAuth methods and therefore use their tokens with the API rather than having mine hard coded. I linked to a Node post because it dealt with the same issue, not because that's the solution I wanted. You can see in my main post i specified Python as my language as choice. I'm also already aware of that library, it's the one I'm using ATM. – JMY1000 Aug 23 '17 at 08:08