Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 06-11-2008, 06:05 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
TwitterBot

Links approved by Moonie.


Twitter is a social-networking site which allows you to post about what you're doing, or to send short messages to all of your friends. For example, if you are walking down the street and see a monkey, you can pull out your phone, and send a text message to Twitter saying "There's a monkey walking down the street!" which will then show up on your Twitter page, and be sent to everyone watching you (and, possibly, to their mobile devices).

TwitterBot is a script which does 3 things. I'll break it up and describe each thing that it does.

1. Access recent Tweets and display them.
TwitterBot reads the Twitter RSS feed for each of the members specified. It figures out which Tweets are new, and then displays them by stating the message in RC chat, and also by alerting every user on the alert list, and PMing them the contents. For example, I can send a Twitter message from my phone, saying "Hey, guys. I'll be back in half an hour; did you get any work done?".

2. Sending messages
Twitter does not work in a classic PM-style; instead, you post an update, and everyone sees it. So, there is no need to reply when you can just send a message. Because of the way the Twitter API is set up, you have to use a PHP file to send a message; I take care of all this for you, however. I will explain this later. To send a message, you can say "/npc twitter sendmessage Hello, we got a lot done! I can't wait to show you when you get back!". This would then be sent to my phone, where I can reply.

3. Commands
TwitterBot comes with two pretty suckish commands, but it would be easy to add more on to. The commands work where you request information, and the bot returns it. So, if I send a Twitter update that says ".playercount", it would tell me the current playercount on my server. If I send one that says ".players", it would tell me all the accounts on my server. So, I can see that the graphics admin is currently on; I can then send a message asking them if they finished a graphic yet.

Script
Copy and paste this code in to a wNPC named "TwitterBot", and customize the options at the top. I will explain these below.
PHP Code:
function onCreated()
{
  
this.username "twitter_user_name"// The name of your Twitter account to send from
  
this.password "twitter_password"// The password of your Twitter account to send from
  
this.server "Server Name"// The name of the server ... I guess you could spoof this if you really wanted to
  
this.url "http://techxonline.net/twitter.php"// URL of your PHP file ...
  // Feel free to use the link to my site, but remember it might
  // go down. Also, if you are worried about security, you may
  // want to host it yourself
  
  
this.users = { "twitter_user_1""twitter_user_2" }; // Twitter names of people to check for Tweets
  
this.alert = { "account1""account2" }; // Accounts to notify when there is a new Tweet
  
this.checkspeed 5// How often new Tweets are checked for. I recommend 5 or 10
  
  
setTimer); // No need to touch this
}
function 
onTimeOut()
{
  for ( 
temp.user this.users )
  {
    
getPostsuser );
  }
  
setTimerthis.checkspeed );
}
function 
getPostsuser )
{
  
temp.url findRSSuser );
  
temp.source requestHTTP"twitter.com"80url );
  
this.catcheventsource"onReceiveData""onSourceReceived" );
}
function 
findRSSuser )
{
  return 
"/statuses/user_timeline/" user ".rss";
}
function 
onSourceReceiveddata )
{
  if ( 
data.name.starts"HTTP_twitter.com" ) )
  {
    
displayDataparseDatadata.data  ));
  }
}
function 
parseDatadata )
{
  
temp.inItem false;
  
temp.items null;
  
temp.item null;
  
  for ( 
temp.line data )
  {
    
line line.trim();
    if ( 
line == "<item>" )
    {
      if ( 
inItem )
      {
        echo( 
"Error: Invalid RSS" );
        return;
      }
      
inItem true;
    }
    else if ( 
line == "</item>" )
    {
      
inItem false;
      
items.additem );
      
item null;
    }
    else if ( 
inItem )
    { 
      
temp.tags = { "description""link" };
      
      for ( 
temp.tag tags )
      {
        if ( 
line.starts"<" tag ">" ) )
        {
          
temp.value line.substringtag.length() + 2line.length() - ( tag.length() * ) - );
          
item.add( { tagvalue } );
          
          break;
        }
      }
      
    }
  }
  
  return 
items;
}
function 
displayDatadata )
{
  for ( 
temp.item data )
  {
    if ( 
this.received.index( @ item[1][1] ) == -)
    {
      
temp.msg item[0][1];
      
      for ( 
temp.0temp.msg.length(); temp.++ )
      {
        if ( 
msg.substringtemp.i) == ":" )
        {
          
this.ends temp.i;
        }
      }
      
      
msg msg.substringthis.ends ).trim();
      if ( 
msg.starts"." ) )
      {
        
temp.send null;
        
        switch ( 
msg.substring) )
        {
          case 
"playercount":
          {
            
send "Player count:" SPC allplayerscount;
            break;
          }
          case 
"players":
          {
            
send "Players currently online: ";
            for ( 
temp.pl allplayers )
            {
              
send @= pl.communityname " ";
            }
            break;
          }
          default:
          {
            
send "Unknown command! Try .players or .playercount";
            break;
          }
        }
        
        
sendMessagesend SPC "from $BOT on #Graal/" this.server );
        
        return;
      }
      
sendtorc"Twitter Update:" SPC item[0][1] );
      
this.received.additem[1][1] );
      
      for ( 
temp.this.alert )
      {
        
temp.pl findPlayer);
        
pl.chat "Twitter Update (see PM from NPC-Server)!";
        
pl.sendPMitem[0][1] );
      }
    }
  }
}
function 
sendMessagemsgacc )
{
  
requesturlthis.url "?user=" this.username "&pass=" this.password "&status=" msg );
}
public function 
callFuncpacc )
{
  switch ( 
p[1] )
  {
    case 
"sendMessage":
    {
      
temp.send p[2];
      
      if ( 
p.size() > )
      {
        for ( 
temp.param 3param p.size(); param ++ )
        {
          
send @= " " p[param];
        }
      }
      
      
sendMessagesend SPC "... from" SPC acc SPC "on #Graal/" this.server );
      
sendToRC"Message sent:" SPC send SPC "by" SPC acc );
      break;
    }
  }

I will now explain the things you need to change. They are all located in the onCreated() block at the top of the script.

this.username - This is the username of your Twitter account which you will be sending messages through. I highly recommend creating a new account.

this.password - The password of the username; yes, it must be stored in clear text. This is why I recommend you create a new account.

this.server - The name of the server you are currently on.

this.url - The URL of the PHP file (see below).

this.users - The list of Twitter accounts that you want to monitor Tweets from.

this.alert - These are the accounts that should receive and alert and be PMed Tweets when they arrive.

this.checkspeed - This is the speed at which TwitterBot checks for new messages; I recommend 5 or 10.

This is all you will need to change in this script.

Now, open the Control-NPC script, and add at the bottom (or to the existing onRCChat function) this code:

PHP Code:
function onRCChat()
{
  if ( 
params[0] == "twitter" )
  {
    
findNPC"TwitterBot" ).callFuncparamsplayer.account ); 
  }

Now, there is only one thing left to discuss: the PHP script.

I have a PHP file hosted on my server which will work fine with this script; however, I warn you that it may go down, and may be slow. It is hosted in a computer at my house, and therefor any power outages will put it down. I cannot guarantee that it will always be there, either.

If you are worried about security, I strongly recommend that you use the script I am providing and host it on your own server. This will also probably result in better up-time and speed.

Paste this in a file called twitter.php -- be sure to change this.url in the script to reflect this.
PHP Code:
<?php
$username 
$_GET'user' ];
$password $_GET'pass' ];

$message $_GET'status' ];

$url 'http://twitter.com/statuses/update.xml';

$curl_handle curl_init();
curl_setopt($curl_handleCURLOPT_URL"$url");
curl_setopt($curl_handleCURLOPT_CONNECTTIMEOUT2);
curl_setopt($curl_handleCURLOPT_RETURNTRANSFER1);
curl_setopt($curl_handleCURLOPT_POST1);
curl_setopt($curl_handleCURLOPT_POSTFIELDS"status=$message");
curl_setopt($curl_handleCURLOPT_USERPWD"$username:$password");
$buffer curl_exec($curl_handle);
curl_close($curl_handle);
?>
Note that the CURl PHP extension will have to be enabled.

This is all you will need to know to run TwitterBot! Be sure to leave comments and criticism, and ask for help if you need it! Suggestions are also welcome so that I can make an improved version!
__________________
Reply With Quote
  #2  
Old 06-11-2008, 12:51 PM
DrakilorP2P DrakilorP2P is offline
Registered User
DrakilorP2P's Avatar
Join Date: Apr 2006
Posts: 755
DrakilorP2P is just really niceDrakilorP2P is just really nice
Nifty.
Reply With Quote
  #3  
Old 06-11-2008, 01:00 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by DrakilorP2P View Post
Nifty.
Thanks ;o
__________________
Reply With Quote
  #4  
Old 06-11-2008, 02:28 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
That's pretty cool nice job
__________________
Reply With Quote
  #5  
Old 06-12-2008, 10:37 PM
bscharff bscharff is offline
Bloo
bscharff's Avatar
Join Date: Sep 2006
Location: San Antonio, Texas
Posts: 185
bscharff has a little shameless behaviour in the past
Send a message via AIM to bscharff Send a message via MSN to bscharff Send a message via Yahoo to bscharff
Extremely useful!

Very nice work!
__________________
Trying to be nice now...
Reply With Quote
  #6  
Old 06-12-2008, 10:43 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Quote:
Originally Posted by bscharff View Post
Extremely useful!
It's isn't an extremely useful script though, unless you're addicted to Twitter or something.. :P
More like a nifty and a very nice script

And that last line sounded a little bit sarcastic?

Anyways, again Chris, nifty script
__________________
Reply With Quote
  #7  
Old 06-12-2008, 11:19 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by Chompy View Post
It's isn't an extremely useful script though, unless you're addicted to Twitter or something.. :P
More like a nifty and a very nice script
That's true, but I like being able to talk to my server from my phone

Thanks Bloo ;o
__________________
Reply With Quote
  #8  
Old 06-12-2008, 11:24 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Quote:
Originally Posted by cbk1994 View Post
That's true, but I like being able to talk to my server from my phone
I like the phone feature kudos to you
__________________
Reply With Quote
  #9  
Old 06-12-2008, 11:43 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by Chompy View Post
I like the phone feature kudos to you
Now I just gotta make MySpaceBot ...

I'd never get a link approved
__________________
Reply With Quote
  #10  
Old 06-13-2008, 01:24 AM
bscharff bscharff is offline
Bloo
bscharff's Avatar
Join Date: Sep 2006
Location: San Antonio, Texas
Posts: 185
bscharff has a little shameless behaviour in the past
Send a message via AIM to bscharff Send a message via MSN to bscharff Send a message via Yahoo to bscharff
Non-sarcasm
lol

I'll upload the PHP script to my server-- I'll post the link later.
__________________
Trying to be nice now...
Reply With Quote
  #11  
Old 03-26-2009, 05:29 PM
Admins Admins is offline
Graal Administration
Join Date: Jan 2000
Location: Admins
Posts: 11,693
Admins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud of
I'm wondering if you really need php? It's possible to make post-requests and similar with TSocket if it's enabled on the server.
Reply With Quote
  #12  
Old 03-26-2009, 06:38 PM
Riot-Starter Riot-Starter is offline
Registered User
Join Date: Aug 2001
Location: Central Florida, USA
Posts: 79
Riot-Starter is on a distinguished road
Send a message via AIM to Riot-Starter
Quote:
Originally Posted by Stefan View Post
I'm wondering if you really need php? It's possible to make post-requests and similar with TSocket if it's enabled on the server.
While most likely possible (I've never used TSocket or seen any examples of its usage), I think that seems overkill for doing something like a POST request, especially given that to do a GET request we don't have to request anything like sockets.

Perhaps it wouldn't be out of line to request a function similar to requesturl for post data? Maybe something like, requestpost(url, "a=1&b=2") which would handle performing the POST request for us and wouldn't require us to request sockets be enabled.

Would probably also be nice to get a url encoding function (percent/form encoding) so data is properly encoded when performing the request.
Reply With Quote
  #13  
Old 03-26-2009, 07:48 PM
Admins Admins is offline
Graal Administration
Join Date: Jan 2000
Location: Admins
Posts: 11,693
Admins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud ofAdmins has much to be proud of
I think using TSocket is simplier, since the command would need to handle authentication, severel parameters etc.

This is a GET request with authentication:
PHP Code:
temp.socket = new TSocket("MySocket");
temp.socket.connect(myip80);
temp.delim char(13) @ char(10);
temp.socket.send("Get /index.php?test=123" temp.delim);
temp.socket.send("Authorization: Basic " base64encode("username:password") @ temp.delim);
temp.socket.send(temp.delim);
waitfor(temp.socket"onClose"10);
temp.answer temp.socket.data.tokenize('\n'); 
The URL encoding is already done in the engine, can add a function to use it by script (urlencode)
Update: added urlencode(url) on Kingdoms, can upload a version for hosted servers end of the week along with the recent /scripthelp improvements.
Reply With Quote
  #14  
Old 03-26-2009, 07:55 PM
Riot-Starter Riot-Starter is offline
Registered User
Join Date: Aug 2001
Location: Central Florida, USA
Posts: 79
Riot-Starter is on a distinguished road
Send a message via AIM to Riot-Starter
Quote:
Originally Posted by Stefan View Post
I think using TSocket is simplier, since the command would need to handle authentication, severel parameters etc.

This is a GET request with authentication:
PHP Code:
temp.socket = new TSocket("MySocket");
temp.socket.connect(myip80);
temp.delim char(13) @ char(10);
temp.socket.send("Get /index.php?test=123" temp.delim);
temp.socket.send("Authorization: Basic " base64encode("username:password") @ temp.delim);
temp.socket.send(temp.delim);
waitfor(temp.socket"onClose"10);
temp.answer temp.socket.data.tokenize('\n'); 
The URL encoding is already done in the engine, can add a function to use it by script (urlencode)
Update: added urlencode(url) on Kingdoms, can upload a version for hosted servers end of the week along with the recent /scripthelp improvements.
I suppose it is rather simple, as I said I've never used TSocket, being as I haven't attempted to do any graalserver -> webserver communication since GS2 was rather new, and no server I have access too has TSocket enabled. I still don't particularly like the idea of having to request it to be enabled to use it for something like this though.
Reply With Quote
  #15  
Old 03-26-2009, 10:42 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by Stefan View Post
I'm wondering if you really need php? It's possible to make post-requests and similar with TSocket if it's enabled on the server.
Yeah, I answered that when you mentioned it originally in another thread (when I was still writing this code).

The problem is that for it to be released on the Code Gallery, I wanted it to be as simple as possible, and code that would only work if you enabled sockets on their server didn't seem like a good solution.
__________________
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 09:57 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.