L
L
lfenix2014-02-28 22:38:41
Perl
lfenix, 2014-02-28 22:38:41

How to get XMPP message with perl script?

I found the Net::XMPP module, I found how to send messages to them, but I don’t know how to receive them. Tell me please. Here is an example with sending a message:

use strict;
use Net::XMPP;

my ($recip, $msg) = @ARGV;

if(! $recip || ! $msg) {
  print 'Syntax: $0 <recipient> <message>\n';
  exit;
}

my $con = new Net::XMPP::Client();
my $status = $con->Connect(
  hostname => 'jabber.ru',
  connectiontype => 'tcpip',
  tls => 0);
die('ERROR: XMPP connection failed') if ! defined($status);

my @result = $con->AuthSend(
  hostname => 'jabber.ru',
  username => 'name',
  password => 'password');

die('ERROR: XMPP authentication failed') if $result[0] ne 'ok';
die('ERROR: XMPP message failed') if ($con->MessageSend(to => $recip, body => $msg) != 0);

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Q
q1t, 2014-02-28
@lfenix

everything seems to be in the same methods
(send, accept)
Or do you need a specific code?

M
mpirozhkov, 2017-03-08
@mpirozhkov

I asked myself the same question today and ended up writing this:

#!/usr/bin/perl

use Net::XMPP;
use Encode;
 
    my $to='[email protected]';
    my $server='domain.ru';
    my $port=5222;
    my $user='test';
    my $password="mypassword";
    my $resource="home";
    my $connectiontype="tcpip";
    my $ssl=0;
    my $tls=1;
    my $debugLevel = 0;
    my $client = Net::XMPP::Client->new(debuglevel => $debugLevel);
    
    # Определяем обработчики событий

    $client->SetCallBacks(onauth => \&onAuth,);
    
    $client->SetMessageCallBacks(#normal =>\&messageNormalCB,
                   chat    =>\&messageChatCB);
    
    my $status = $client->Execute(
                hostname        => $server,
                port            => $port,
                connectiontype  => $connectiontype,
                ssl             => $ssl,
                tls             => $tls,
        username => $user,
        password => $password,
                resource => $resource);
        
        if (!(defined($status))) {
                print "ERROR:  XMPP connection failed.\n";
                print "($!)\n";
                exit(0);
        }
    
sub onAuth {
    $client->PresenceSend(show => 'online', priority => 10);
  
  print "online\n";
}

sub messageChatCB {
    my ($sid, $mes) = @_;
    my $sender = $mes->GetFrom();
    my $body   = encode('cp866', $mes->GetBody());
    #my $thread = $mes->GetThread();

  print "$sender\n$body\n\n";
  
  print encode('cp866', decode('utf-8', 'Что скажем?'))."\n";
  
  my $mes = <STDIN>;
  
    my $text = decode('cp866', $mes);
    $client->MessageSend(
            to       => $to,
            #resource => $resource,
            #subject  => "Subj",
            type     => "chat",
            body     => $text);
  
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question