开发者

How can I use a twitter stream of direct messages as input?

开发者 https://www.devze.com 2023-02-03 12:55 出处:网络
I\'m writing a web application in PHP and MySQL.I want users to be able to interact with the site via twitter.If you are familiar with how RememberTheMilk incorporates twitter as a means of creating t

I'm writing a web application in PHP and MySQL. I want users to be able to interact with the site via twitter. If you are familiar with how RememberTheMilk incorporates twitter as a means of creating tasks and other activities, then you ha开发者_如何学JAVAve some idea of what I want to accomplish.

The interactions would be brief. For example: User_Jay sends a direct message to my twitter account; My application checks the direct message stream every so often, when it sees User_Jay's message, the application recognizes a key phrase and executes a bit of code; when code has been executed, the application sends a direct message to User_Jay. The end.

I don't know which twitter library would be best suited to this endeavor. Neither am I sure how to get code executed for a particular user based on their direct message.

_

For reference, users will create accounts on the actual site, and there they will be able to declare their twitter username.

Also, example code or links to example code accomplishing this sort of thing would be greatly appreciated.


You can use the Twitter streaming API for direct messages. The first thing is you don't have to write it in PHP. Though you might be writing the site in PHP, that doesn't mean you have to use PHP for the background process of getting tweets.

Firstly, you need some code that handles the data from the User Streams API. I'm not sure there is one for PHP, but phirehose gets close. There's a patch available to get it to support User Streams.

If you can't find a PHP library to support user streams, I'd suggest you consider using Ruby. It's pretty easy to write daemons in Ruby, and you can use a library like twitterstream which supports user streams.

Then you need to write some code that basically processes the User Stream: checks to see if the thing you are getting is a direct message and puts it into the database. You probably also want to think about what you plan to do if this gets popular, and think about queues and database speed and so on. And if you are sending responses, you'll need to think about API rate limits and so on.

Using the streaming API is very much like using the pull API: you get a blob of JSON with a whole set of key-value pairs. How do you decide which user you are handling? Well, you look at the user key and find their username.

You'll also need to come up with a syntax for interacting with the site using DMs, and you'll want to create either a regular expression based parser, or perhaps use a lexer. You'll also want to test this pretty hard, using unit tests and so on. You are accepting text from a user: don't trust it!

The streaming API isn't the only way to do it: you can also run a cron job and check the API for new messages. This is easier, but not nearly as good.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号