HTML and CSS Reference
In-Depth Information
This code requires the
ntwitter
module and then creates a new
client
with the credentials you created
when you set up the app. If this were an app that could connect to multiple users, you would need to go through
the OAuth authentication process to get the Access Token information for the user, but in this case Twitter en-
ables you to generate those from the web interface.
Next it calls
verifyCredentials
to make sure you entered the configuration variables correctly. This
check isn't necessary but can help you debug problems or invalid keys.
Finally, it calls
updateStatus
to send a tweet. Twitter responds with the full data details of the tweet.
If everything goes according to plan, you should see your tweet in the Timeline. If you click the expand link,
you see the source is your app, as shown in
Figure 22-2
.
Figure 22-2:
Your first tweet.
If you try to run the program again, Twitter returns an error message as it prevents the exact same tweet from
being sent more than once.
Listening to the User Stream
Sending tweets is only half of the job. To respond to people playing the game, you need to listen for incoming
messages.
Twitter provides two APIs for listening to incoming messages: a REST API that relies on polling and a
stream API that pushes messages in real time. For a game such as
Hangman
where users might be tweeting at
the user to play the game, better performance can be achieved via the stream API.
The documentation for the Twitter stream API is available at
https://dev.twitter.com/docs/streaming-apis/
To listen to tweets associated with a specific username via the stream API, you can use the
cli-
ent.stream
method in
ntwitter
. Because the
Hangman
game will be playing with people it may not
be following, it needs to also listen to the replies from people it is not following. To achieve this, the
replies:all
option must be passed as an option.