Created
October 21, 2014 23:08
-
-
Save tstachl/9a2bae185a63d953736f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 = '[email protected]'; | |
my $Password = 'password'; | |
my $Customer = 1231231234; | |
my $case_href = CreateCase('Subject', 'Body', 5, 'new', $Customer, '[email protected]'); | |
my $reply_href = CreateReply($case_href, 'Reply Subject', 'Reply Body', '[email protected]'); | |
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'} = '[email protected]'; | |
$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'} = '[email protected]'; | |
$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