site logo

Example: Ticket & comment creation (Ruby)


Example: create / update a ticket and a comment  

This script shows how to:

  • connect to your helpdesk API with your access key
  • create and update a ticket
  • create a comment

 

#!/usr/bin/ruby
########################################
# $Id: create-update-ticket.rb,v 1.1 2007/08/15 17:04:25 d Exp $
#
# This is an example how to use the Mojo Helpdesk API
# Language: ruby
#
# This script shows how to:
# - connect to your helpdesk API with your access key
# - create and update a ticket.
# - create a comment
#
# You will need to adjust the URL and MYID values:
#
# URL - Base URL to your helpdesk.
#
# MYID - Your access key, as displayed on your "profile" page
#
########################################
# Feel free to use this code and modify to fit your needs.
# More information visit www.mojohelpdesk.com
########################################
require 'soap/wsdlDriver'
require 'open-uri'

########################################
# Your helpdesk URL
# URL = "http://supportxyz.mojohelpdeskcom
# MYID = "8b1459cfcb367d72419e2642e56fa3b72bc5c2a0"
#
# Replace the info below with your own:
########################################
########################################
URL = "http://yourhd.mojohelpdesk.com"
MYID = "find-your-key-in-your-user-profile"
########################################
begin
drv = SOAP::WSDLDriverFactory.new("#{URL}/backend/service.wsdl").create_rpc_driver
rescue
printf "Fatal error: Could not connect to server.wsdl or an error occured (%s). Check your URL and try again.\n", $!; exit(-1)
end

printf ">>> WSDL found.\n"
#
# Ticket creation
#
params = {
"user_id" => "1",
"title" => 'test ticket',
"description" => 'desc',
"ticket_queue_id" => '1',
"comment" => 'This is a comment at creation',
'priority_id'=>'40',
'send_email_notification' => 'true'
}

#
# Send the request and get the result
#
result = drv.CreateTicket("#{MYID}", params)

if result['successful'] != true
printf ">>> Error: %s\n", result['error_message']
exit(-1)
end

#
# The result is the newly created ticket
#
t = result.ticket
printf ">>> Ticket created, id: %d,title: %s\n",t['id'], t['title']


#
# Let's create a comment
#

printf ">>> Creating comment/__metadot__.\n"
c = {
"user_id" => "1",
"body" => 'we are cooking with gas',
'ticket_id'=> t['id'],
'time_spent' => "45"
}

res = drv.CreateComment("#{MYID}", c)
if res['successful'] == true
puts res.inspect
printf ">>> Comment creation successful\n"
else
printf ">>> Comment creation failed\n"
printf ">>> Error: %s\n", res['error_message']
end

#
# Updating a ticket
#
puts ">>> Updating ticket"
t['title'] = " MODIFIED TITLE "


res2 = drv.UpdateTicket("#{MYID}", t['id'], t)
if res2['successful'] == true
printf ">>> Ticket update successful\n"
puts res2.inspect
else
printf ">>> Ticket update failed\n"
printf ">>> Error: %s\n", res['error_message']
end


exit(0)