1

I am writing a simple program that will periodically check to see if anyone has tweeted my account, and publish a reply.

Once it has replied, I don't want it to reply again (otherwise it will keep replying forever) so it must be one reply per tweet (if the conversation continues, then it will keep adding a tweet to the end).

I want it to be stateless.

I therefore want to grab all tweet conversations, and see if they have a reply by my program already. If not, reply.

If you browse twitter you can see all the replies to a tweet as a 'conversation'. So I want to get all conversations on my wall and see if the latest tweet in each conversation was done by me. If it wasn't, then I want to add a reply.

The problem I have is that I can't get 'conversations' because I can't find how tweets are linked together properly.

I was going to build a map by getting all tweets mentioning me, and then work out the 'root' tweets (start of the conversation) and read each conversation to see if the last reply is by me.

However, I cannot work out what a root tweet is because sometimes the InReplyToUserId property is populated even for a root tweet. So I can't figure out which ones are root tweets.

Any ideas? Here's my code so far:

var allMentions = Timeline.GetMentionsTimeline();

    // Get all root tweets
    foreach (var tweet in allMentions.Where(c => !c.InReplyToUserId.HasValue))
    {
        var replies = Search.SearchDirectRepliesTo(tweet);

        var latestTweet = replies.OrderByDescending(c => c.TweetLocalCreationDate).FirstOrDefault();

        if (latestTweet.CreatedBy.Id != _myTwitterId)
        {
            string reply = GetReply();

            foreach (var mention in latestTweet.UserMentions)
            {
                if (mention.Id != _myTwitterId)
                {
                    reply = $"@{mention.ScreenName} {reply}";
                }
            }

            if (reply.Length > 140)
            {
                reply.Substring(0, 140);
            }

            Tweet.PublishTweetInReplyTo(reply, latestTweet);
        }
    }
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
NibblyPig
  • 51,118
  • 72
  • 200
  • 356

1 Answers1

0

Use the in_reply_to_status_id field. This will let you find the root tweet of a conversation.

DWRoelands
  • 4,878
  • 4
  • 29
  • 42