Twitter bot in php with oAuth support

For last couple of days, I was looking into twitter API. One day I asked Lenin vai about a bot he made. He suggested me a Twitter bot. I was so excited at the very beginning. But when I had started I gone through some new features which I didn’t know and of course sometimes silly things give us so much pain ๐Ÿ˜›

About Tweet Bot:

The bot collects data from one or more locations and then tweets those one by one. Very simple php script. Usable with any website or blog and even any person at Twitter.

Tweet Bot Features:
  • oAuth supported
  • Parses feed with simple feed parser
  • Formatting links by shortening url
  • Format large title
Setting up Tweet Bot

Register application from here. For oAuth support I’ll be using Abraham William’s oAuth Library. Download the Twitter oAuth Library from github. The bot will be getting feeds from RSS. For that we need a parser. So, here we’ll be using Simplepie to parse RSS feed. Download the parser from here.

I always prefer to make a configuration file, just to keep the code clean and ready to use any time.

config.php

define('CONSUMER_KEY', 'YOUR APPLICATION KEY');
define('CONSUMER_SECRET', 'YOUR APPLICATION SECRET');
define('ACCESS_TOKEN', 'YOUR ACCESS TOKEN');
define('ACCESS_TOKEN_SECRET', 'YOUR ACCESS TOKEN SECRET');

/* Change this url as needed */
define('FEED_URL', 'https://mdshaonimran.wordpress.com/feed/');

Now let’s make our hand dirty with some coding ๐Ÿ™‚

autobot.php

Connect to twitter via Abraham’s oAuth Library

$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);

Setup Simplepie to parse RSS feed.

$feed = new SimplePie();
$feed->set_feed_url(FEED_URL);
$feed->init();
$feed->handle_content_type();

We need to save the parsed link so that we can prevent the bot tweeting the same thing more than once. Twitter has word limitation of 140 words. So, the bot parses the title as well as the link. It always matches the latest link with the last tweeted one.

/* find the total number of available feed */
$max = $feed->get_item_quantity();

/* Get last tweeted data */
$last_saved_tweet = file_get_contents('last_tweet');

/* Get the first item on the feed */
$item = $feed->get_item(0);

/* permalink of the first item */
$itemlink = $item->get_permalink();

/* save the permalink into file */
file_put_contents('last_tweet', $itemlink);

/* title of the first item */
$itemtitle = $item->get_title();

/* length of the item title */
$titlelength = strlen($itemtitle);

if ($titlelength > 110) {
    $itemtitle = substr($itemtitle, 0, 107) . "...";
}

if ($itemlink != $last_saved_tweet) {

    /* shorten the url with tinyurl */
    $shortlink = file_get_contents("http://tinyurl.com/api-create.php?url=" . $itemlink);

    /* Update status with Abraham's oAuth Library */
    $connection->post('statuses/update', array ('status' => $itemtitle . " " . $shortlink));
}

So, now the Tweet Bot is ready to shoot! ๐Ÿ™‚

Facebook Application Setup

Making web applications and desktop applications are something like cooking food. To cook a delicious food is not always necessary when we are learners. To cook foods the first thing we need is to know the fundamental tools we need. Then we might need a setup to make the food eatable. Then light up your burner and start cooking. The taste is yours!!!

Here I am going to discuss about basic facebook application setup on Facebook Platform.

Types of facebook application?

There are basically two types of applications on FB.
1. iFrame
2. FBML

How facebook application runs on facebook platform?

iFramw
iFrame

another form is,

fbml
fbml

Go to Account -> Application setting

I assume that you have a developer account. If not, follow the necessary steps to make one.

Then choose Developer from the menu and selectย  + Set Up New Application. Here you’ll be prompt to name your application. Give a name and choose Agree.

Now, you’ll see something like this. These are the basic information of your application.

Essential info
Essential info

Now select the canvas tab. Remember this is the crucial part of your application. Canvas page URL, is the address of your application on Facebook Platform. Canvas Callback URL, where application is hosted.

canvas url
canvas url

Canvas Setting, here we are to develop a iFrame application. So, choose iFrame from the option. Choose other options as you need.

Save settings and TADAAAA!!!!!!!!!!!! This is enough for a simple application to go on.

Location API on Aloashbei Platform

To work with Aloahbei platform, first configure the .wsdl file for LBS which is given on the Aloashbei site. Or you can download it from here.

index.php

class LBSRequest {
 public $msisdn;
 functionย  __construct($num) {
 $this->msisdn = $num;
 }
 public $registrationID = "******";
 public $password= "******";
}

class GPInit {
 public $LBSRequest;
}

if(isset ($_POST['number'])) {
 $GPInit = new GPInit();
 $GPInit->LBSRequest = new LBSRequest("88".$_POST['number']);
 $result = $soap->requestLocation($GPInit);
}

Download full code

Send SMS with GrameenPhone API and PHP

To work with Aloahbei platform, first edit the .wsdl file for SMS which is given on the Aloashbei site. Or you can download it from here.

First, make a config.php file.

$soap = new SoapClient("http://service.onnobela.com/sms.wsdl");

$var['registrationID'] = "******";
$var['password'] = "****";
$var['sourceMsisdn'] = '88017********';
$var['smsPort'] = 7424;
$var['msgType'] = 4;
$var['charge'] = 0.00;
$var['chargedParty'] = '88017********';
$var['contentArea'] = 'gpgp_psms';

Now, make the main index.php file put the number and message into the $var array and call sendSMS api.

$var['destinationMsisdn'] = '88'.$_POST['number'];
$var['msgContent'] = $_POST['msg'];

$result = $soap->sendSMS( array ("SendSMSRequest" => $var) );

Download full code

Install LAMP on ubuntu

Installing LAMP(Linux, Apache, MySQL, PHP) is not that tough now-a-days.

The easiest way
First open the synaptic manager with sudo synapti
then Edit ==> Mark Packages by Task
select LAMP server from the list and click OK.

Another easy way
go to terminal and type
sudo tasksel

press space bar to select and then tab to <ok>
Follow the screen.

One line command setup
go to terminal and type
sudo apt-get install lamp-server^
N. B the character ‘^’ is not a typo
Follow the screen, it’ll ask to change password for the root user of MySQL database.

Now. test Apache. Open a browser and type http://localhost/

if you see this, you are done with you apache installation.

test PHP installation. open terminal and type
sudo vim /var/www/test.php
save the file.
again, sudo /etc/init.d/apache2restart
now, open browser and try http://localhost/test.php

you see this, means your PHP installation is completed.

Test MySQL setup
go to terminal and type
mysql -u root -p

you see this, ahhhh….you are done with the famous LAMP installation ๐Ÿ˜€

For phpmyadmin setup try this