-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmqtt2twitter.pl
More file actions
executable file
·85 lines (61 loc) · 1.82 KB
/
mqtt2twitter.pl
File metadata and controls
executable file
·85 lines (61 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/perl
#
# mqtt2twitter.pl
#
# Subscribe to an MQTT topic and post received messages to twitter.
# The *vast* majority of this is taken from the "subscribe.pl" example
# that is part of the WebSphere::MQTT::Client cpan module.
#
# To use, install WebSphere::MQTT::Client with cpan, set the username, password
# and topic variables below and then run. The post a message to the subscribed
# topic with another client.
#
# There is a publish example in the WebSphere::MQTT::Client module.
use WebSphere::MQTT::Client;
use Data::Dumper;
use LWP::UserAgent;
use strict;
my $username = "WaitingCloud";
my $password = "lynx6686";
my $mqtt_topic = "test";
#
my $mqtt = new WebSphere::MQTT::Client(
Hostname => 'localhost',
Port => 1883,
Debug => 1,
);
# Connect to Broker
my $res = $mqtt->connect();
die "Failed to connect: $res\n" if ($res);
print Dumper( $mqtt );
sleep 1;
print "status=".$mqtt->status()."\n";
sleep 1;
print "status=".$mqtt->status()."\n";
# Subscribe to topic
my $res = $mqtt->subscribe( $mqtt_topic );
print "Subscribe result=$res\n";
sleep 2;
print "status=".$mqtt->status()."\n";
sleep 2;
# For sending twitter updates
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new(POST => 'http://twitter.com/statuses/update.xml');
$req->content_type('application/x-www-form-urlencoded');
# Get Messages
while( 1 ) {
my @res = $mqtt->receivePub();
#errors can be caught by eval { }
$req->content("status=".$res[1]); # set http content as twitter status.
$req->authorization_basic($username, $password); # set http authorisation
my $res = $ua->request($req); # send http request
print Dumper(@res);
}
# Unsubscribe from topic
my $res = $mqtt->unsubscribe( 'twitter' );
print "Unubscribe result=$res\n";
sleep 2;
print "status=".$mqtt->status()."\n";
# Clean up
$mqtt->terminate();
print Dumper( $mqtt );