# Get ticket messages

## getTicketMessages()

This will return all tickets sorted by the last changed date. hostware uses this information to check if any tickets have new admin answers, which are then imported using another function (TODO).

### Parameters

<table><thead><tr><th width="142">Type</th><th width="166">Name</th><th>Description</th></tr></thead><tbody><tr><td>mixed</td><td>ticketId</td><td>The ID of the ticked. The same you provided in getTickets()</td></tr></tbody></table>

### Response

Collection of TicketMessageResponse() classes. Each class resembles a ticket message contains the following attributes:

<table><thead><tr><th width="142">Type</th><th width="166">Name</th><th>Description</th></tr></thead><tbody><tr><td>mixed</td><td>messageId</td><td>The ID of the ticked. Usually integer or UUID.</td></tr><tr><td>string</td><td>message</td><td>The message of the ticket. You can pass the raw message, no parsing necessary.</td></tr><tr><td>Carbon</td><td>createdAt</td><td></td></tr><tr><td>Int or Null</td><td>adminId</td><td>The id of the hostware (!) admin. You have to search the admin in the database. For other integrations, they are searched by email.</td></tr><tr><td>Collection</td><td>attachments</td><td>Collection of TicketMessageAttachmentResponse(). See below</td></tr></tbody></table>

### TicketMessageAttachmentResponse()

This contains an array of all attachments which are attached to a given message. This contains the name, size and url to download the file. Usually, each ticket provider returns this information in the api response.

<table><thead><tr><th width="142">Type</th><th width="166">Name</th><th>Description</th></tr></thead><tbody><tr><td>mixed</td><td>id</td><td>The ID of the attachment.</td></tr><tr><td>string</td><td>name</td><td>The file name.</td></tr><tr><td>int</td><td>Size</td><td>The size in bytes of the file.</td></tr><tr><td>String or Null</td><td>url</td><td>The download url of the attachment files.</td></tr></tbody></table>

### Example Code

```php
public function getTicketMessages(mixed $ticketId): Collection {
	$response = $this->makeRequest("conversations/{$ticketId}");
	$admins = $this->_getAdminMails();

	$messages = [];
	foreach ($response->_embedded->threads as $thread) {

		if (!in_array($thread->type, ["message", "customer"])) {
			continue;
		}

		$attachments = [];

		/**
		This code actually parses inline images from the message and attaches them as attachment. This is not necessary.
		*/
		$re = '/<img(?s).*src="(?<image_url>[A-Za-z0-9-_.:\/?=&;]*)"(?s).*alt="(?<image_name>[A-Za-z0-9-_.]*)">/m';
		$str = $thread->body;

		preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

		foreach ($matches as $index => $match) {
			$match['image_url'] = str_replace("&amp;", "&", $match['image_url']);

			$attachments[] = new TicketMessageAttachmentResponse(
				"INLINE-{$index}",
				$match['image_name'],
				-1,
				$match['image_url'],
			);
		}


		foreach ($thread->_embedded->attachments as $attachment) {
			$attachments[] = new TicketMessageAttachmentResponse(
				$attachment->id,
				$attachment->fileName,
				$attachment->size,
				$attachment->fileUrl,
			);
		}

		$adminId = $thread->createdBy->type === "user" ? ($admins->get($thread->createdBy->email, $admins->first())) : null;

		$messages[] = new TicketMessageResponse(
			$thread->id,
			$thread->body,
			Carbon::parse($thread->createdAt, "UTC")->setTimezone("Europe/Berlin"),
			$adminId,
			collect($attachments)
		);
	}

	return collect($messages);
}
```

### Testing

You can test this function when executing the console command from [get-tickets](https://docs.hostware.io/developer-documentation/module/support-integrations/get-tickets "mention").

The console command gets all the tickets. If the $lastChangedAt parameter from the ticket is different than saved in the hostware database, the getTicketMessages() is executed. You can manipulate the lastChangedAt to trigger the execution.

The function gets called synchronously, you can dump() and debug as you like.
