melo / codebits

Presentations and projects done at SAPO Codebits

This URL has Read+Write access

codebits / 2008 / xmpp-hands-on / bots / http2xmpp / http2xmpp-1.pl
100755 39 lines (32 sloc) 0.816 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
#!/usr/bin/env perl
#
# Basic client
#
 
use strict;
use warnings;
use Net::XMPP2::Client;
use AnyEvent::Mojo;
 
# Start a small bot
my $bot = Net::XMPP2::Client->new;
$bot->add_account('http2xmpp@test.simplicidade.org', 'teste', '127.0.0.1', 5222);
$bot->start;
 
# Start a HTTP server
my $server = mojo_server undef, 3001, sub {
  my (undef, $tx) = @_;
  my $req = $tx->req;
  my $url = $req->url;
  my $meth = $req->method;
  my $res = $tx->res;
  
  if ($url eq '/send' && $meth eq 'POST') {
    my $body = $req->param('body');
    my $to = $req->param('to');
    return unless $body;
    
    $bot->send_message($body, $to, undef, 'chat');
  }
  else {
    $res->code(404);
    $res->body('These are not the droids you are looking for...');
    $res->headers->content_type('text/plain');
  }
};
 
$server->run;