First of all, this tutorial is intended to show an example of how to use some web-services APIs so you should have knowledge about sockets and http protocol.
In this case I'm using
Twitter web API that allows you to update your status with a single request.
Because of this example will run over only one account of Twitter, that account should be, for example, for your CS community.
Requirements:- You must have sockets module loaded and allowed in your host provider
- You must have a Twitter account
- You have to know the username and password of that account
Step 1: Twitter APITwitter API allows people to make their own software that interacts within Twitter.
As for update status task, you only need to send your new text using this url and this parameter:
Code:
http://twitter.com/statuses/update.xml?status=Your new status text
Step 2: How can we allow players to send tweets!?You can handle say command to manage only those messages that starts with a tag. I'm using the tag @tw.
So, if player says
That text will be posted as a tweet... of course on Twitter.
I guess everybody knows how to handle say but here's an example
PHP Code:
public plugin_cfg()
{
register_clcmd("say", "say_handler")
}
public say_handler(id)
{
static message[64]
read_args(message, 63)
if(!(message[0] == '@') || !(message[1] == 't') || !(message[2] == 'w') || !(message[3] == ' '))
return PLUGIN_CONTINUE
server_print("Player ID %i tries to tweet: ^"%s^"", message[4])
return PLUGIN_HANDLED
}
Step 3: Let's go!This are the tasks we have to do to make a single HTTP request that tweets
- Encode using base64 your username and password (see stock)
- Make sure the text that will be sent over HTTP request are encoded (see stock)
- Open a socket to twitter.com web server and send the request like the following example
Code:
POST /statuses/update.xml?status=Hello+World!
Host: twitter.com
Authorization: Basic dXNlcjpwYXNz
Connection: close
Look that the string and credentials are encoded (with different techniques).
Step 4: Example pluginI will share a small plugin that let's you send tweets when you're playing.
It has a few cvars to setup credentials and permissions (admin only or all users)
NotesThis technique it's valid for a lot of applications (Twitter, Facebook, LinkedIn). Also you can make your own web application to manage such kind of messages and the use it as your needs.
For example you could write a web application that receives alerts from server (hacks detected, users compliants, etc) and then forward to MSN, TALK, YAHOO, AIM, etc. Also you can store to have the history of every message received.