Getting tweets with your game's hashtag!

For my winter games entry, I made the ability to get and display tweets on each player’s screen. The code below allows you to get the most recent tweets using the hashtag of your choice, and puts them in a table. The scripted cap is 3 tweets at a time, but you can unlock that for a maximum twitter api allowance of 5 tweets.

local hashtagsearch = "rbxgames2014"--put your tag in there, sans hashtag
httpservice = game:GetService("HttpService")
local tweetjson = httpservice:GetAsync(
"http://programmerssyndicate.guildhome.co.uk/ROBLOX/searchTweets.php?screenname=%23"..hashtagsearch,true)
pattern = [["id_str":"%d*","text":.-,"screen_name":.-",]]
authpattern = [[.-"]]
textpattern = [["text":".-",]]
local tweets = {}
local nu = 1
for w in string.gmatch(tweetjson,pattern) do
	local discard = false
	local author = "@"..string.sub(string.reverse(string.match(string.sub(string.reverse(w),3),authpattern)),2)
	local tweettext = string.sub(string.match(w,textpattern),9)
	tweettext = string.sub(tweettext,1,string.len(tweettext)-2)
	print(author..": " ..tweettext)
	if nu >3 then discard = true end
	if not discard then
		tweets[nu] ={["auth"]=author,["text"]=tweettext}
		nu = nu+1
	end
end

you get something like this:

the tweets are stored in a table formatted like this:

tweets = {
	{
		["auth"] = screen_name,
		["text"]=tweet
	},
}

A big thanks to FiniteReality for hosting the server that this code uses!

EDIT: There is a big issue with escaped characters and displaying them on roblox textlabels/buttons/boxes. See this thread for more info: http://rbxdev.com/forum/bug-reports/885-escape-characters-printing-in-text-labels

1 Like

Here is a video of my custom tweet viewer hooked up to the above code (nothing happens until 0:12 though):

1 Like

[quote] Here is a video of my custom tweet viewer hooked up to the above code (nothing happens until 0:12 though):

-snip- [/quote]

That looks VERY nice. I like the GUI design and everything.

This is a really cool idea! Something I wouldn’t have thought of.

1 Like

That’s an extremely nice design you have there.

If anything, I should also give my thanks to As8D who helped me set up the code. I have had no idea how to use PHP until now, so he is the one who should be mostly credited for his work.

Did you know, he’s also working on a twitter interface for ROBLOX? So far you can do pretty much what you can with my code, just with a fancy interface to it. He’s currently working on making it so you can POST tweets using your own twitter account, but obviously he has to go through the usual junk; ‘we will not use your password for anything bad, blah blah blah’

EDIT: Also, you could have made your searcher a little bit more efficient; something like:

local api = assert(LoadLibrary("RbxUtility"))
local http = game:GetService("HttpService")
local jsondata = http:GetAsync("http://programmerssyndicate.guildhome.co.uk/ROBLOX/searchTweets.php?screenname=%23RbxGames2014") --let's just use this as an example

local data = api.DecodeJSON(jsondata)
local tweets = { }
for _, tweet in pairs(data.statuses) do
table.insert(tweets, {auth=tweet.user.screen_name, text=tweet.text})
end

[quote]
local api = assert(LoadLibrary(“RbxUtility”))
local http = game:GetService(“HttpService”)
local jsondata = http:GetAsync(“http://programmerssyndicate.guildhome.co.uk/ROBLOX/searchTweets.php?screenname=%23RbxGames2014”) --let’s just use this as an example

local data = api.DecodeJSON(jsondata)
local tweets = { }
for _, tweet in pairs(data.statuses) do
table.insert(tweets, {auth=tweet.user.screen_name, text=tweet.text})
end
[/code] [/quote]

Hmm, well asking for twitter passwords on roblox is really bad idea, and I don’t think roblox will like that stuff. Even if you don’t use the passwords for bad, exploiters could find way how to steal them. Of course, if you keep the passwords on client and localscript, it should be safe, but you’d need to get somekind of roblox admin review and aprooval, so users could actually trust it. Tho it’s still really messy stuff.

[quote]

EDIT: Also, you could have made your searcher a little bit more efficient; something like:

[code]
local api = assert(LoadLibrary(“RbxUtility”))
local http = game:GetService(“HttpService”)
local jsondata = http:GetAsync(“http://programmerssyndicate.guildhome.co.uk/ROBLOX/searchTweets.php?screenname=%23RbxGames2014”) --let’s just use this as an example

local data = api.DecodeJSON(jsondata)
local tweets = { }
for _, tweet in pairs(data.statuses) do
table.insert(tweets, {auth=tweet.user.screen_name, text=tweet.text})
end
[/code] [/quote]

Is the JSONEncode and Decode under httpservice somehow different than the one in RbxUtility? The only reason I switched to string manipulation was because it kept erroring when using httpservice:JSONDecode(tweetjson)

You can see more about the issue in the other thread about this.

Something I just thought about, which may make putting twitter service in a public Winter Games place a bad idea. If I’ve got the hashtag, can’t I tweet something anti-child friendly so that every player playing your game reads it?

1 Like

potentially, yes, but if I get some string checking for bad words, I can just eliminate them completely from showing up.

I think that the idea is really awesome, and it looks surprisingly official, as if it were an official ROBLOX tool.

Props to you.

[quote]

EDIT: Also, you could have made your searcher a little bit more efficient; something like:

[code]
local api = assert(LoadLibrary(“RbxUtility”))
local http = game:GetService(“HttpService”)
local jsondata = http:GetAsync(“http://programmerssyndicate.guildhome.co.uk/ROBLOX/searchTweets.php?screenname=%23RbxGames2014”) --let’s just use this as an example

local data = api.DecodeJSON(jsondata)
local tweets = { }
for _, tweet in pairs(data.statuses) do
table.insert(tweets, {auth=tweet.user.screen_name, text=tweet.text})
end
[/code] [/quote]

Is the JSONEncode and Decode under httpservice somehow different than the one in RbxUtility? The only reason I switched to string manipulation was because it kept erroring when using httpservice:JSONDecode(tweetjson)

You can see more about the issue in the other thread about this.[/quote]

yeah, it’s a little odd about what it gets.

Also, don’t forget I updated my website!

http://programmerssyndicate.guildhome.co.uk/ROBLOX/

The names of things changed a lot to be more semantic, so you’re going to have to update.

Another amazing http service demo.

We should collect these all for a blog story.

[quote] Another amazing http service demo.

We should collect these all for a blog story. [/quote]

Yay!