mirror of
				https://github.com/lingble/chatwoot.git
				synced 2025-10-31 19:17:48 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			95 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
			
		
		
	
	
			95 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			JavaScript
		
	
	
	
	
	
| /* eslint no-console: 0 */
 | |
| /* global axios */
 | |
| import ApiClient from '../ApiClient';
 | |
| 
 | |
| export const buildCreatePayload = ({
 | |
|   message,
 | |
|   isPrivate,
 | |
|   contentAttributes,
 | |
|   echoId,
 | |
|   files,
 | |
|   ccEmails = '',
 | |
|   bccEmails = '',
 | |
|   templateParams,
 | |
| }) => {
 | |
|   let payload;
 | |
|   if (files && files.length !== 0) {
 | |
|     payload = new FormData();
 | |
|     if (message) {
 | |
|       payload.append('content', message);
 | |
|     }
 | |
|     files.forEach(file => {
 | |
|       payload.append('attachments[]', file);
 | |
|     });
 | |
|     payload.append('private', isPrivate);
 | |
|     payload.append('echo_id', echoId);
 | |
|     payload.append('cc_emails', ccEmails);
 | |
|     payload.append('bcc_emails', bccEmails);
 | |
|   } else {
 | |
|     payload = {
 | |
|       content: message,
 | |
|       private: isPrivate,
 | |
|       echo_id: echoId,
 | |
|       content_attributes: contentAttributes,
 | |
|       cc_emails: ccEmails,
 | |
|       bcc_emails: bccEmails,
 | |
|       template_params: templateParams,
 | |
|     };
 | |
|   }
 | |
|   return payload;
 | |
| };
 | |
| 
 | |
| class MessageApi extends ApiClient {
 | |
|   constructor() {
 | |
|     super('conversations', { accountScoped: true });
 | |
|   }
 | |
| 
 | |
|   create({
 | |
|     conversationId,
 | |
|     message,
 | |
|     private: isPrivate,
 | |
|     contentAttributes,
 | |
|     echo_id: echoId,
 | |
|     files,
 | |
|     ccEmails = '',
 | |
|     bccEmails = '',
 | |
|     templateParams,
 | |
|   }) {
 | |
|     return axios({
 | |
|       method: 'post',
 | |
|       url: `${this.url}/${conversationId}/messages`,
 | |
|       data: buildCreatePayload({
 | |
|         message,
 | |
|         isPrivate,
 | |
|         contentAttributes,
 | |
|         echoId,
 | |
|         files,
 | |
|         ccEmails,
 | |
|         bccEmails,
 | |
|         templateParams,
 | |
|       }),
 | |
|     });
 | |
|   }
 | |
| 
 | |
|   delete(conversationID, messageId) {
 | |
|     return axios.delete(`${this.url}/${conversationID}/messages/${messageId}`);
 | |
|   }
 | |
| 
 | |
|   getPreviousMessages({ conversationId, before }) {
 | |
|     return axios.get(`${this.url}/${conversationId}/messages`, {
 | |
|       params: { before },
 | |
|     });
 | |
|   }
 | |
| 
 | |
|   translateMessage(conversationId, messageId, targetLanguage) {
 | |
|     return axios.post(
 | |
|       `${this.url}/${conversationId}/messages/${messageId}/translate`,
 | |
|       {
 | |
|         target_language: targetLanguage,
 | |
|       }
 | |
|     );
 | |
|   }
 | |
| }
 | |
| 
 | |
| export default new MessageApi();
 | 
