Skip to content

Instantly share code, notes, and snippets.

@tstachl
Created October 21, 2014 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tstachl/9a2bae185a63d953736f to your computer and use it in GitHub Desktop.
Save tstachl/9a2bae185a63d953736f to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use feature qw(say);
use JSON;
use WWW::Curl::Easy;
my $DeskSite = 'https://example.desk.com';
my $Username = 'johndoe@example.com';
my $Password = 'password';
my $Customer = 1231231234;
my $case_href = CreateCase('Subject', 'Body', 5, 'new', $Customer, 'customer@example.com');
my $reply_href = CreateReply($case_href, 'Reply Subject', 'Reply Body', 'customer@example.com');
say $case_href;
say $reply_href;
sub CreateCase {
my ($subject, $body, $priority, $status, $customer, $from) = @_;
my $case;
my @labels;
push @labels, 'mix_panel';
$case->{'subject'} = $subject;
$case->{'priority'} = $priority;
$case->{'status'} = $status;
$case->{'type'} = 'email';
$case->{'labels'} = \@labels;
$case->{'custom_fields'}->{'warehouse'} = "1000001";
$case->{'message'}->{'direction'} = 'in';
$case->{'message'}->{'status'} = 'received';
$case->{'message'}->{'to'} = 'support@devel.desk-mail.com';
$case->{'message'}->{'from'} = $from;
$case->{'message'}->{'subject'} = $subject;
$case->{'message'}->{'body'} = $body;
$case->{'_links'}->{'customer'}->{'href'} = "/api/v2/customers/$customer";
my $json = encode_json($case);
my $curl = WWW::Curl::Easy->new;
$curl->setopt(CURLOPT_HEADER, 1);
$curl->setopt(CURLOPT_USERPWD, "$Username:$Password");
$curl->setopt(CURLOPT_SSL_VERIFYHOST, 1);
$curl->setopt(CURLOPT_URL, "$DeskSite/api/v2/cases");
$curl->setopt(CURLOPT_CUSTOMREQUEST, 'POST');
$curl->setopt(CURLOPT_POSTFIELDS, $json);
my $response;
$curl->setopt(CURLOPT_WRITEDATA, \$response);
my $retcode = $curl->perform;
my $case_href = 0;
if ($retcode == 0) {
my $header_size = $curl->getinfo(CURLINFO_HEADER_SIZE);
my $header = substr($response, 0, $header_size);
my $body = substr($response, $header_size);
my $status = $curl->getinfo(CURLINFO_HTTP_CODE);
$json = decode_json($body);
if($status == 201) {
$case_href = $json->{_links}->{self}->{href};
}
}
return($case_href);
}
sub CreateReply {
my ($case_href, $subject, $body, $to) = @_;
my $case;
$case->{'subject'} = $subject;
$case->{'body'} = $body;
$case->{'to'} = $to;
$case->{'from'} = 'support@devel.desk-mail.com';
$case->{'direction'} = 'out';
$case->{'status'} = 'pending';
my $json = encode_json($case);
my $curl = WWW::Curl::Easy->new;
$curl->setopt(CURLOPT_HEADER, 1);
$curl->setopt(CURLOPT_USERPWD, "$Username:$Password");
$curl->setopt(CURLOPT_SSL_VERIFYHOST, 1);
$curl->setopt(CURLOPT_URL, "$DeskSite$case_href/replies");
$curl->setopt(CURLOPT_CUSTOMREQUEST, 'POST');
$curl->setopt(CURLOPT_POSTFIELDS, $json);
my $response;
$curl->setopt(CURLOPT_WRITEDATA, \$response);
my $retcode = $curl->perform;
my $reply_href = 0;
if ($retcode == 0) {
my $header_size = $curl->getinfo(CURLINFO_HEADER_SIZE);
my $header = substr($response, 0, $header_size);
my $body = substr($response, $header_size);
my $status = $curl->getinfo(CURLINFO_HTTP_CODE);
$json = decode_json($body);
if($status == 201) {
$reply_href = $json->{_links}->{self}->{href};
}
}
return($reply_href);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment