Making a plurk bot


Ever want to have a bot post periodically o your plurk timeline? Well it’s easy with with the plurk-api, a simple php script, and a cronjob

This post is based on @richoz’s article with small changes on php bits and cronjob. In this post, I’m only going to use the add new plurk function of the plurk-api.
What you need:

  • A PC connected to the internet 24/7, or at least whenever you want the bot post on your timeline. For this post, I’m using a Ubuntu linux box.
  • PHP installed on the pc mentioned above. Make sure php-cli & php-curl are also installed.  to get them on ubuntu, open a terminal and do the following:

    surfer@fulcrum:~$ sudo apt-get install php5 php5-cli php5-curl

  • A plurk API key. You can get your plurk API key for free by registering here
  • Download the plurk-php-api script. Create a folder on your home directory:

    surfer@fulcrum:~$ mkdir plurkbot

    unzip the plurk-php-api script to the newly created directory

    surfer@fulcrum:~$ unzip php-plurk-api-1.6.2.zip -d plurkbot/

     

  • Create a php script for the bot. The script below is a much simplified version of a script on @richoz’s article. Go to the plurkbot directory, and create the script using text editor of your choice:

    surfer@fulcrum:~$ cd plurkbot/ surfer@fulcrum:~$ nano postplurk.php

    Copy the following script to postplurk.php

    <?php
    require(‘php-plurk-api/plurk_api.php’);
    $api_key = ‘yourapikeyhere’;
    $username = ‘mach5’;
    $password = ‘password’;
    $post = “this is not the bot you’re looking for”;
    $plurk = new plurk_api();
    $plurk->login($api_key, $username, $password);
    $message = $post;
    $plurk->add_plurk(‘en’, ‘:’, $message);
    ?>

    Change the value of several variables:

    • $api_key  value is the plurk api key that is sent to your email
    • $username value is your plurk login name, in my case it’s mach5
    • $password is your plurk account password
    • $post is the content of your plurk post

    Save the file. Please remember that the newly created script and the plurk-php-api folder must be placed inside the plurkbot directory

  • Create cronjob schedule that will automatically run the script.

    surfer@fulcrum:~$ crontab –e

  • The following entry will execute the script one time on 2:00 PM, everyday

    00 14 * * * /usr/bin/php /home/surfer/plurkbot/postplurk.php

  • Done! If you want to make several post a day, just change cronjob schedule to run several times a day. Or if you want to post different things on a single day, just create multiple script and create a cronjob for each script. Or you can also use @richoz’s script.

You can also spice up your bot by altering the text that the bot posts or make the bot fetch a comic strip. One thing that I’d like to do is to wrap the bot script with a bash script that will feed the bot with a quote from fortune-mod. These are the scripts that I use:

postplurkquote.sh

#!/bin/sh
fore="!fb Today's PlurQuote: "
quote=$(/usr/games/fortune plurquote)
echo $fore$quote > quote.txt
/usr/bin/php /home/surfer/postquote.php

postquote.php

<?php
require('php-plurk-api/plurk_api.php');
$api_key = 'plurkapikeyhere';
$username = 'mach5';
$password = 'password';
$buka = file_get_contents("quote.txt");
$pesan = $buka;
$plurk = new plurk_api();
$plurk->login($api_key, $username, $password);
$message = $pesan;
$plurk->add_plurk('en', ':', $message);
?>

Then, create a cronjob to run postplurkquote.sh, such as

00 09 * * * /home/surfer/postplurkquote.sh

..to run the script everyday at 09.00

Update /02/26/2011:
Since plurk only allows a maximum of 144 characters on a single post, you might want to create a custom fortune database. Here’s how.

Reference:

Advertisements

One Comment Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s