Skip to content

Instantly share code, notes, and snippets.

@martinvonwittich
Last active February 5, 2019 21:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinvonwittich/ad2a5febb67c276848029904d53a00ed to your computer and use it in GitHub Desktop.
Save martinvonwittich/ad2a5febb67c276848029904d53a00ed to your computer and use it in GitHub Desktop.
ami-zammad.pl - listens for queue events via asterisk AMI and posts them to Zammad's generic CTI
#!/usr/bin/perl -CSDAL
use warnings;
use strict;
use Asterisk::AMI;
#use DDP;
use AnyEvent::HTTP;
use WWW::Form::UrlEncoded;
sub cb_default;
# Installation:
# 1. Enable AMI in Asterisk in /etc/asterisk/manager.conf. Use it only on localhost, I haven't bothered with SSL support yet.
# 2. Copy zammad.conf from this gist to /etc/asterisk/manager.d/. Use a random password.
# 3. Run "manager reload" in asterisk -r.
# 4. Install the use dependencies from above via package manager and/or CPAN.
# 5. Enable Zammad's CTI (Generic) interface.
# 6. Change the AMI password and the Zammad CTI token in this script.
# 7. Run this script.
my $astman = Asterisk::AMI->new(
PeerAddr => "127.0.0.1",
PeerPort => "5038",
Username => "zammad",
Secret => "xxx",
Events => "on",
Handlers => { default => \&cb_default }
);
my $zammad_url = "https://zammad.iserv.eu/api/v1/cti/xxx";
die "Unable to connect to asterisk" unless ($astman);
$astman->loop;
sub post_zammad
{
my $post = shift;
my $body = WWW::Form::UrlEncoded::build_urlencoded($post);
http_post $zammad_url, $body,
sub {
my ($data, $headers) = @_;
if ($data ne "{}")
{
p $data;
p $headers;
}
};
}
sub cb_default
{
my ($asterisk, $event) = @_;
# QueueCallerJoin -> Anruf startet
# QueueCallerAbandon -> Anrufer hat zu früh aufgelegt
# AgentConnect -> Agent hat abgenommen
# AgentComplete -> Agent hat fertig
printf "EVENT: %s\n", $event->{Event};
#p $event;
if ($event->{Event} eq "QueueCallerJoin")
{
post_zammad {
event => "newCall",
from => $event->{CallerIDNum},
to => $event->{Queue},
direction => "in",
callId => $event->{Uniqueid},
};
}
elsif ($event->{Event} eq "AgentConnect")
{
(my $agent = $event->{MemberName}) =~ s/^SIP\///;
post_zammad {
event => "answer",
direction => "in",
user => $agent,
callId => $event->{Uniqueid},
};
}
elsif ($event->{Event} eq "AgentComplete")
{
post_zammad {
event => "hangup",
direction => "in",
callId => $event->{Uniqueid},
cause => "normalClearing",
};
}
elsif ($event->{Event} eq "QueueCallerAbandon")
{
post_zammad {
event => "hangup",
direction => "in",
callId => $event->{Uniqueid},
cause => "cancel",
};
}
}
[zammad]
secret = xxx
read = agent
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment