How to check whether user is following me not using TweetSharp.TwitterService.ListFollowers()

970 Views Asked by At

Is there some function in TweetSharp that could be used in a similar way to my 'IsFollowingMe' below?

I'd like to check whether a user is following me before I attempt to send some private message.

TweetSharp.TwitterService service;
string screenName = "@some_one";
string someMessage = "Some Message";

if (service.IsFollowingMe(screenName))
{
       service.SendDirectMessage(screenName, someMessage);
   else
       NotifyThatSendingNotPossible();
}

First option to such approach is to use:

TweetSharp.TwitterService service;
TwitterCursorList<TwitterUser> followers = service.ListFollowers();

and then iterate through the result to find out if user is following my account. But this will eventually be ineffective when there are a lot of followers.

Another option is to execute service.SendDirectMessage and then check if the result is null or not. I tested such approach with success - however my application logic prefers to check in advance if sending is possible and based on this information should do different actions.

2

There are 2 best solutions below

0
On BEST ANSWER

The answer is as follows:

TweetSharp.TwitterService service;
string fromUser = "@mr_sender";
string toUser = "@some_one";
string someMessage = "Some Message";

TweetSharp.TwitterFriendship friendship = 
    service.GetFriendshipInfo(fromUser, toUser);

if (friendship.Relationship.Source.CanDirectMessage.HasValue &&
   friendship.Relationship.Source.CanDirectMessage.Value)
{
    service.SendDirectMessage(screenName, someMessage);
}
else
{
    NotifyThatSendingNotPossible();
}
0
On

I could able to solve this using below way.

var twitterFriendship = service.GetFriendshipInfo(new GetFriendshipInfoOptions() { SourceScreenName = "source_name", TargetScreenName = "target_name"});

After that, you can check like below

if(twitterFriendship.Relationship.Source.Following && twitterFriendship.Relationship.Target.FollowedBy)
    {
     service.SendDirectMessage(new SendDirectMessageOptions() { ScreenName="target_name",Text="message"})
    }