# Wolfram Mathematica

Make a service that automatically replies to tweets asking if numbers are prime.

Initialize a value to keep track of the latest processed tweet.

 In[1]:= XIf[! ValueQ[latest], latest = "466653904932188161"];

Send a tweet with the hashtag #IsItPrime for testing.

 Out[2]=

Create a function that extracts integers from a tweet and determines if they are prime.

 In[3]:= XfindPrimeIntegers[tweet_] := With[ {ints = ToExpression /@ StringCases[tweet["Message"], DigitCharacter ..]}, Transpose[{ints, PrimeQ[ints]}]]

Create a function that constructs a reply to an #IsItPrime tweet.

 In[4]:= XtoMessage[list_] := StringJoin[ ToString /@ Flatten@Riffle[(list /. {True -> " is prime", False -> " is not prime"}), ", "]]
 In[5]:= XcreateMessage[tweet_] := With[{screenname = tweet["ScreenName"]}, {"Message" -> "@" <> screenname <> " " <> toMessage[findPrimeIntegers[tweet]], "InReplyToStatusID" -> tweet["TweetID"]} ]
 In[6]:= XreplyToMessage[connection_, tweet_] := ServiceExecute[connection, "Tweet", createMessage[tweet]]

Put the pieces together in a function that collects, analyzes, and responds to tweets.

 In[7]:= XPrimeTweetResponder[] := Module[{tweets}, tweets = ServiceExecute["Twitter", "TweetSearch", {"Query" -> "#IsItPrime", "SinceID" -> latest}]; If[tweets === {}, Return[{}]]; latest = tweets[[1]]["TweetID"]; replyToMessage /@ tweets ]

Test the function on any tweets with the #IsItPrime hashtag, including your own.

 In[8]:= XPrimeTweetResponder[]
 Out[8]=

Run the function in a scheduled task that wakes up to process tweets every two minutes.