smartsupp create chat api

 avatar
unknown
typescript
2 years ago
3.9 kB
4
Indexable
import Oas from '@smartsupp/openapi-builder'

module.exports = Oas.operation('post', '/conversations', {
	operationId: 'create',
	summary: 'Create conversation',
	security: [{
		bearer: ['conversations:create'],
	}],
	tags: [
		'conversations',
	],
	requestBody: {
		required: true,
		content: {
			'application/json': {
				schema: bodySchema().toJson(),
				examples: {
					simple: {
						summary: 'Simple Request',
						value: {
							contact_id: 'ct42ngURIH2M',
						},
					},
					with_messages: {
						summary: 'With Imported Messages',
						description: 'Messages can be imported only as contact or bot.',
						value: {
							contact_id: 'ct42ngURIH2M',
							messages: [
								{
									created_at: '2022-06-20T23:19:42.457Z',
									sub_type: 'contact',
									content: {
										type: 'text',
										text: 'Hello',
									},
								},
								{
									created_at: '2022-06-20T23:22:42.457Z',
									sub_type: 'bot',
									content: {
										type: 'text',
										text: 'Hi, how can i help you?',
									},
								},
							],
						},
					},
					with_tags_and_variables: {
						summary: 'With Variables and Tags',
						value: {
							contact_id: 'ct42ngURIH2M',
							tags: [
								'R8jFuof8Zx',
							],
							variables: {
								foo_bar: 'This is Foo Bar',
							},
						},
					},
					with_assigned_agents: {
						summary: 'With Assigned Agents',
						value: {
							contact_id: 'ct42ngURIH2M',
							assigned_ids: ['301345'],
						},
					},
					with_assigned_group: {
						summary: 'With Assigned Group',
						value: {
							contact_id: 'ct42ngURIH2M',
							group_id: 'gr830dk20',
						},
					},
				},
			},
		},
	},
	responses: {
		200: {
			$ref: '#/components/responses/suc_conversation',
		},
		422: {
			$ref: '#/components/responses/err_unprocessable',
		},
		default: {
			$ref: '#/components/responses/err_general',
		},
	},
})

function bodySchema() {
	return Oas.schema({
		type: 'object',
		additionalProperties: false,
		properties: {
			contact_id: {
				$ref: '#/components/schemas/contact_id',
			},
			assigned_ids: {
				description: 'List assigned agents',
				type: 'array',
				items: {
					$ref: '#/components/schemas/agent_id',
				},
			},
			// TODO: solve group validation in router
			// group_id: {
			// 	description: 'If set than chat will be routed (or visible) only to agents assigned to specific group. Group ID you can retrieved from group\'s settings.',
			// 	anyOf: [{ $ref: '#/components/schemas/group_id' }],
			// 	nullable: true,
			// 	example: null,
			// },
			ext_id: {
				description: 'You custom id. Can be used to find conversation by your ID.',
				type: 'string',
				nullable: true,
				example: null,
			},
			variables: {
				description: 'Key-Value data stored within conversation. Variables are automatically displayed in dashboard.',
				type: 'object',
				additionalProperties: true,
				example: { foo: 'Foo', bar: 123 },
			},
			tags: {
				description: 'List of tags for this conversation.',
				type: 'array',
				items: {
					type: 'string',
				},
				example: ['R8jFuof8Zx'],
			},
			messages: {
				description: 'List of messages to be imported into conversation.',
				type: 'array',
				items: {
					type: 'object',
					additionalProperties: false,
					properties: {
						sub_type: {
							title: 'Message SubType',
							type: 'string',
							enum: ['contact', 'bot'],
						},
						content: {
							type: 'object',
							additionalProperties: false,
							properties: {
								text: {
									type: 'string',
									nullable: true,
								},
							},
							required: [
								'text',
							],
						},
						created_at: {
							type: 'string',
							format: 'date-time',
						},
					},
					required: [
						'sub_type',
						'content',
					],
				},
			},
		},
		required: [
			'contact_id',
		],
	})
}