[solved] API | Format ticket body

Hey folks,

was anyone able to create a ticket via API with formatted text inside the body of the initial article (size, bold, italic, whatever)?

I tried a few things but none seemed to work. I’m just curious if it’s possible at all and maybe someone already successfully did it.

cheers

In my opinion it should be, would you mind sharing the article-code part of your API script so I can take a look?

Sure thing.

The script that launches the ticket looks like that (systemd service):

[Unit]
Description=periodische Tickets erstellen %I

[Service]
ExecStart=/usr/bin/curl -k -H "Authorization: Token token=THATSMYTOKEN" -H "Content-Type: application/json"  --data "@/home/zammad/periTickets/%i"  https://MYZAMMADSERER/api/v1/tickets
User=zammad

The file referrenced by %i is called by the service launching this script and looks f. e. like this:

{
 "title":"Some ticket for you",
 "group":"Helpdesk",
 "owner":"",
 "article":{
  "subject":"To-Do",
  "body":"## ToDo Helpdesk ##\n\n
1. Do step one\n
Tough stuff
2. Do step two\n
2.1 even tougher\n
2.2 maybe even complicated\n
2.3 worst case - annoying\n\n
3. Do step three\n
silly work\n\n
4. Do step four\n
finally put an end to it\n",
  "type":"note",
  "internal":false},
 "customer":"That one guy",
 "note":"Notiz",
 "tags":"",
}

So what silly me did is adding HTML code into that last file but it didn’t work. The line breaks work fine since they’re still interpreted by the shell.

What do i need to do now to tell zammad what text needs to be bold or italic etc?

cheers

Sooo i guess no one was able to do it and thus it’s not possible? :frowning:

Heya sorry for the delay,

my system throws the following error when using exact your code:

I, [2019-03-26T18:14:26.414002 #31512-17907880]  INFO -- : Started POST "/api/v1/tickets" for 666.666.666.666 at 2019-03-26 18:14:26 +0100
F, [2019-03-26T18:14:26.433990 #31512-17907880] FATAL -- :
F, [2019-03-26T18:14:26.434033 #31512-17907880] FATAL -- : ActionDispatch::Http::Parameters::ParseError (765: unexpected token at '{
        "title":"Some ticket for you",
        "group":"Sales",
        "owner":"",
        "article": {
                "subject":"To-Do",
                "body":"## ToDo Helpdesk ##\n\n
                1. Do step one\n
                Tough stuff
                2. Do step two\n
                2.1 even tougher\n
                2.2 maybe even complicated\n
                2.3 worst case - annoying\n\n
                3. Do step three\n
                silly work\n\n
                4. Do step four\n
                finally put an end to it\n",
                "type":"note",
                "internal":false
        },
        "customer":"That one guy",
        "note":"Notiz",
        "tags":"",
}'):
F, [2019-03-26T18:14:26.434065 #31512-17907880] FATAL -- :
F, [2019-03-26T18:14:26.434117 #31512-17907880] FATAL -- : actionpack (5.1.6.2) lib/action_dispatch/http/parameters.rb:115:in `rescue in parse_formatted_parameters'
actionpack (5.1.6.2) lib/action_dispatch/http/parameters.rb:109:in `parse_formatted_parameters'
actionpack (5.1.6.2) lib/action_dispatch/http/request.rb:361:in `block in POST'
rack (2.0.6) lib/rack/request.rb:59:in `fetch'
rack (2.0.6) lib/rack/request.rb:59:in `fetch_header'
actionpack (5.1.6.2) lib/action_dispatch/http/request.rb:360:in `POST'
actionpack (5.1.6.2) lib/action_dispatch/http/parameters.rb:53:in `parameters'
actionpack (5.1.6.2) lib/action_dispatch/http/filter_parameters.rb:41:in `filtered_parameters'
actionpack (5.1.6.2) lib/action_controller/metal/instrumentation.rb:21:in `process_action'
actionpack (5.1.6.2) lib/action_controller/metal/params_wrapper.rb:252:in `process_action'
activerecord (5.1.6.2) lib/active_record/railties/controller_runtime.rb:22:in `process_action'
actionpack (5.1.6.2) lib/abstract_controller/base.rb:124:in `process'
actionview (5.1.6.2) lib/action_view/rendering.rb:30:in `process'
actionpack (5.1.6.2) lib/action_controller/metal.rb:189:in `dispatch'
actionpack (5.1.6.2) lib/action_controller/metal.rb:253:in `dispatch'
actionpack (5.1.6.2) lib/action_dispatch/routing/route_set.rb:49:in `dispatch'
actionpack (5.1.6.2) lib/action_dispatch/routing/route_set.rb:31:in `serve'
actionpack (5.1.6.2) lib/action_dispatch/journey/router.rb:50:in `block in serve'
actionpack (5.1.6.2) lib/action_dispatch/journey/router.rb:33:in `each'
actionpack (5.1.6.2) lib/action_dispatch/journey/router.rb:33:in `serve'
actionpack (5.1.6.2) lib/action_dispatch/routing/route_set.rb:844:in `call'
omniauth (1.9.0) lib/omniauth/strategy.rb:192:in `call!'
omniauth (1.9.0) lib/omniauth/strategy.rb:169:in `call'
omniauth (1.9.0) lib/omniauth/strategy.rb:192:in `call!'
omniauth (1.9.0) lib/omniauth/strategy.rb:169:in `call'
omniauth (1.9.0) lib/omniauth/strategy.rb:192:in `call!'
omniauth (1.9.0) lib/omniauth/strategy.rb:169:in `call'
omniauth (1.9.0) lib/omniauth/strategy.rb:192:in `call!'
omniauth (1.9.0) lib/omniauth/strategy.rb:169:in `call'
omniauth (1.9.0) lib/omniauth/strategy.rb:192:in `call!'
omniauth (1.9.0) lib/omniauth/strategy.rb:169:in `call'
omniauth (1.9.0) lib/omniauth/strategy.rb:192:in `call!'
omniauth (1.9.0) lib/omniauth/strategy.rb:169:in `call'
omniauth (1.9.0) lib/omniauth/strategy.rb:192:in `call!'
omniauth (1.9.0) lib/omniauth/strategy.rb:169:in `call'
omniauth (1.9.0) lib/omniauth/strategy.rb:192:in `call!'
omniauth (1.9.0) lib/omniauth/strategy.rb:169:in `call'
omniauth (1.9.0) lib/omniauth/strategy.rb:192:in `call!'
omniauth (1.9.0) lib/omniauth/strategy.rb:169:in `call'
omniauth (1.9.0) lib/omniauth/builder.rb:64:in `call'
rack (2.0.6) lib/rack/etag.rb:25:in `call'
rack (2.0.6) lib/rack/conditional_get.rb:38:in `call'
rack (2.0.6) lib/rack/head.rb:12:in `call'
rack (2.0.6) lib/rack/session/abstract/id.rb:232:in `context'
rack (2.0.6) lib/rack/session/abstract/id.rb:226:in `call'
actionpack (5.1.6.2) lib/action_dispatch/middleware/cookies.rb:613:in `call'
actionpack (5.1.6.2) lib/action_dispatch/middleware/callbacks.rb:26:in `block in call'
activesupport (5.1.6.2) lib/active_support/callbacks.rb:97:in `run_callbacks'
actionpack (5.1.6.2) lib/action_dispatch/middleware/callbacks.rb:24:in `call'
actionpack (5.1.6.2) lib/action_dispatch/middleware/debug_exceptions.rb:59:in `call'
actionpack (5.1.6.2) lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'
railties (5.1.6.2) lib/rails/rack/logger.rb:36:in `call_app'
railties (5.1.6.2) lib/rails/rack/logger.rb:24:in `block in call'
activesupport (5.1.6.2) lib/active_support/tagged_logging.rb:69:in `block in tagged'
activesupport (5.1.6.2) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (5.1.6.2) lib/active_support/tagged_logging.rb:69:in `tagged'
railties (5.1.6.2) lib/rails/rack/logger.rb:24:in `call'
actionpack (5.1.6.2) lib/action_dispatch/middleware/remote_ip.rb:79:in `call'
actionpack (5.1.6.2) lib/action_dispatch/middleware/request_id.rb:25:in `call'
rack (2.0.6) lib/rack/method_override.rb:22:in `call'
rack (2.0.6) lib/rack/runtime.rb:22:in `call'
activesupport (5.1.6.2) lib/active_support/cache/strategy/local_cache_middleware.rb:27:in `call'
actionpack (5.1.6.2) lib/action_dispatch/middleware/executor.rb:12:in `call'
actionpack (5.1.6.2) lib/action_dispatch/middleware/static.rb:125:in `call'
rack (2.0.6) lib/rack/sendfile.rb:111:in `call'
railties (5.1.6.2) lib/rails/engine.rb:522:in `call'
railties (5.1.6.2) lib/rails/railtie.rb:185:in `public_send'
railties (5.1.6.2) lib/rails/railtie.rb:185:in `method_missing'
puma (3.11.0) lib/puma/configuration.rb:225:in `call'
puma (3.11.0) lib/puma/server.rb:624:in `handle_request'
puma (3.11.0) lib/puma/server.rb:438:in `process_client'
puma (3.11.0) lib/puma/server.rb:302:in `block in run'
puma (3.11.0) lib/puma/thread_pool.rb:120:in `block in spawn_thread'
logging (2.2.2) lib/logging/diagnostic_context.rb:474:in `block in create_with_logging_context'

So I went to search what the heck Zammad hates about parsing in that part…

Zammad parser absolutely hates you trying to use several lines… you might want to put everything in one :smiley:

So:

{
	"group": "Sales",
	"title": "Some ticket for you",
	"owner": "",
	"customer_id": 12,
	"note": "Notiz",
	"tags": null,
	"article": {
		"subject": "To-Do",
		"body": "## ToDo Helpdesk ##\n\n 1. Do step one\n Tough stuff 2. Do step two\n 2.1 even tougher\n 2.2 maybe even complicated\n 2.3 worst case - annoying\n\n 3. Do step three\n silly work\n\n 4. Do step four\n finally put an end to it\n",
		"type": "note",
		"internal": false
	}
}

Result looks like this:

When taking a look into the API-Documentation, you could also use HTML if needed, but keep it in one line. :slight_smile:

Uhm that’s strange because the line break is working perfectly fine for me.
The only thing i’m not able to do is to make it look like that:


1. Do step one
Tough stuff

2. Do step two
2.1 even tougher
2.2 maybe even complicated
2.3 worst case - annoying

3. Do step three
silly work

4. Do step four
finally put an end to it


That’s why i asked explicitly about bold/italic/size etc. :see_no_evil:

Not working for me :frowning:. I also had a look at https://docs.zammad.org/en/latest/api-intro.html#request-format before but didn’t find anything about HTML formatted requests. Is this the wrong documentation i’m looking at?

Nevertheless, I tried with the following:

{
 "title":"Ticketname",
 "group":"Helpdesk",
 "owner":"",
 "article":{
  "subject":"To-Do",
  "body":"Bitte den <b>wichtigen</b> Stuff erledigen",
  "type":"note",
  "internal":true},
 "customer":"cronjob",
 "note":"Notiz",
 "tags":"",
 "location":""
}

And i got this:

Try to add "content_type":"text/html" to the article JSON, that’s what the UI does. It’s easy to see when you use the browser devtools while creating a ticket. The request from my test in the browser:

{
   "customer_id" : "3259",
   "followup" : null,
   "pending_time" : null,
   "title" : "test",
   "id" : "c-4140",
   "state_id" : "2",
   "tags" : "",
   "group_id" : "2",
   "priority_id" : "2",
   "test" : "",
   "article" : {
      "sender_id" : 2,
      "to" : "Support",
      "body" : "<b>bold</b><div><br></div>",
      "form_id" : "678624002",
      "content_type" : "text/html",
      "type_id" : 5,
      "from" : "Martin von Wittich <redacted>"
   },
   "owner_id" : "3"
}
2 Likes

Smart! Work’s like a charm.
Thanks a lot!

1 Like

In case anyone wants to do the same, here’s our “final version”.

The script that launches the ticket (systemd service) had to be tweaked a little and now looks like that:

[Unit]
Description=periodische Tickets erstellen %I

[Service]
ExecStart=/usr/bin/curl -k -H "Authorization: Token token=THATSMYTOKEN" -H "Content-Type: application/json"  --data '@/home/zammad/periTickets/%i'  https://MYZAMMADSERER/api/v1/tickets
User=zammad

Changed: the quotes after the --data param have been changed to apostrophes so the html code gets interpreted correctly.

The file referrenced by %i now looks like this:

 {
  "title":"Some ticket for you",
  "group":"Helpdesk",
  "owner":"",
  "article":{
   "subject":"To-Do",
   "body":"<div><h1>## ToDo Helpdesk ##</h1></div>
 <div><h3><b>1. Do step one</b></h3></div>
 <div>Tough stuff, download <a href=\"https://myfancydownload.url\">this</a></div>
 <div><br></div>
 <div><h3><b>2. Do step two</b></h3></div>
 <div>2.1 even tougher</div>
 <div>2.2 maybe even compicated</div>
 <div>2.3 worst case - annyoing</div>
 <div><br></div>
 <div><h3><b>3. Do step three</b></h3></div>
 <div>silly work</div>
 <div>inform that guy <a href=\"mailto:that@guy.mail\">with this mail</a></div>
 <div><br></div>
 <div><h3><b>4. Do step four</b></h3></div>
 <div>finally put an end to it</div>
 <div><i>be gentle :)</i></div>",
 "content_type":"text/html",
 "type":"note",
 "internal":false},
 "customer":"that one guy",
 "note":"Notiz",
 "tags":""
 }

This results in a ticket looking like this:

The only thing not working (yet) is the mailto link.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.