Best practise for tracking Tweets which contain own @ScreenName

Mar 12, 2014 at 3:52 PM
Using UserStream it is possible to detect Tweets CreatedByFriends/CreatedByAnyone etc. However, what is best practise when one wants to detect tweets which contain @MyScreenName in them. Is this normally done with a FilteredSteam object, or with a UserStream object and some searching of each Tweet as it arrives, or is there a better way.

Mar 12, 2014 at 9:38 PM
Edited Mar 12, 2014 at 9:38 PM
Hi MatR,

It depends on what you are willing to do. If the @MyScreenName is really YOUR ScreenName, I would personally use the UserStream because it will include the private tweets.
var us = Stream.CreateUserStream();
us.TweetCreatedByAnyone += (sender, args) =>
    var userMentions = args.Tweet.UserMentions;
    if (userMentions != null && userMentions.Any(x => x.ScreenName == "MY_SCREEN_NAME"))
        // Do whatever
If you want to get all the public tweets mentioning a specific user screen you can use the FilteredStream.
var fs = Stream.CreateFilteredStream();
fs.MatchingTweetReceived += (sender, args) =>
Hope this helps.
Marked as answer by linvi on 3/13/2014 at 11:34 AM
Mar 13, 2014 at 11:52 AM
Hi Linvi

many thanks for your reply.

I too would favour the UserStream, However, in your example the compiler does not accept the .Any method. Is this dowm the datatyping of userMentions variable ?

Mar 13, 2014 at 12:00 PM

This is a Linq query, please just add the namespace :)
using System.Linq;
Mar 13, 2014 at 12:10 PM
Ahhh. Of course. Thanks - that works now.
Mar 18, 2014 at 6:58 PM
Edited Mar 18, 2014 at 6:59 PM
Using the code snippet provided above with filteredstream I am getting some unexpected results


When I track a screen name text with the "@" prefixed, e.g. "@BBC_TopGear" I get no data. But if I match on just the text part ("BBC_TopGear") it works.

Is there a reason why filteredstream should be doing this?

I really wanted to filter on the whole screenname.
Mar 19, 2014 at 1:30 AM
When you put the @, it means that it is a tweet that is in reply to the user.
It is not all the tweets containing this user name.

Start your stream with :
Go to the BBC_TopGear account and reply to one of their tweet. You will see the reply added to your filteredstream.

Marked as answer by linvi on 3/18/2014 at 5:57 PM
Mar 19, 2014 at 11:16 AM
No this is not working for me - either with a reply to one of their tweets, or originating a tweet with their @screenname in the tweet.
If I filter on their screenname without the "@" it works OK. This seems odd to me.

Any ideas?

Mar 19, 2014 at 11:23 AM
Edited Mar 19, 2014 at 11:27 AM
You need to use fs.AddFollow(). It gives all tweets made by the user and all tweets mentioning the user (including retweets of the user's tweets by other users).
var stream = Stream.CreateFilteredStream();
stream.MatchingTweetReceived += (sender, args) => Console.WriteLine(args.Tweet.Text);
stream.StreamStarted += (sender, args) => Console.WriteLine("stream started...");
I just tested by replying to bbctopgear and it works as expected.

Hope this helps.

Mar 19, 2014 at 1:01 PM
Thank you for your reply Lokesh.

If .AddFollow() is used, does this actually follow the named user, if so, and the tracking is only required for the duration of the session the stream is running for, shouldnt an explicit unfollow be done when finsihed. Or, is unfollow implicit when the stream is stopped?

Mar 19, 2014 at 1:05 PM
No, this method does not make you follow the user on twitter.

The filter api uses the above endpoint. So, AddFollow() simply adds a userID into the follow parameter.

Mar 19, 2014 at 1:45 PM
Thanks. Understood.