AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Code Snippets/Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=83)
-   -   [TUT] How to post on Twitter from CS (https://forums.alliedmods.net/showthread.php?t=117748)

joropito 02-04-2010 09:00

[TUT] How to post on Twitter from CS
 
1 Attachment(s)
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 API
Twitter 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

Code:

@tw Hello World!
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(message63)

    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 plugin
I 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)

Notes
This 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.

01101101 02-04-2010 09:04

Re: [TUT] How to post on Twitter from CS
 
n1 i never took much interest in sockets but they seem really useful

Edit:

Why are you messing so much with this?

PHP Code:

        formatex(requestcharsmax(request), "POST %s%s HTTP/1.1^n"g_urltmp)
        
formatex(request2charsmax(request2), "%sHost: %s^nAccept-Encoding: none^n"requestg_host)
        
formatex(requestcharsmax(request), "%sAuthorization: Basic %s^n"request2encoded)
        
formatex(request2charsmax(request2), "%sConnection: Close^n^n"request)
        
socket_send(socketrequest2charsmax(request2)) 

If you could use a single formatex?
Or, 2 formats could also be posible, since you can rewrite them.

joropito 02-04-2010 09:10

Re: [TUT] How to post on Twitter from CS
 
Quote:

Originally Posted by 01101101 (Post 1078045)

Why are you messing so much with this?

PHP Code:

        formatex(requestcharsmax(request), "POST %s%s HTTP/1.1^n"g_urltmp)
        
formatex(request2charsmax(request2), "%sHost: %s^nAccept-Encoding: none^n"requestg_host)
        
formatex(requestcharsmax(request), "%sAuthorization: Basic %s^n"request2encoded)
        
formatex(request2charsmax(request2), "%sConnection: Close^n^n"request)
        
socket_send(socketrequest2charsmax(request2)) 

If you could use a single formatex?
Or, 2 formats could also be posible, since you can rewrite them.

Because I'm crazy and I like clean code.

AntiBots 02-04-2010 15:10

Re: [TUT] How to post on Twitter from CS
 
I was working with Facebook ( http://forums.alliedmods.net/showthread.php?t=117776 ). And you have to create a sessionid. :down:

Also facebook is very secury. But any won me!!!

rak 03-22-2012 01:47

Re: [TUT] How to post on Twitter from CS
 
this don't work :( I put first in the list of plugin.ini.. but show me the admin message..

fysiks 03-22-2012 02:09

Re: [TUT] How to post on Twitter from CS
 
Quote:

Originally Posted by rak (Post 1673498)
this don't work :( I put first in the list of plugin.ini.. but show me the admin message..

Twitter has since changed their authentication method (now OAuth) so this will no longer work.


All times are GMT -4. The time now is 11:49.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.