2023-01-27 03:22:44 -08:00
import type {
2020-11-10 10:08:48 -08:00
IDataObject ,
2023-03-06 08:33:32 -08:00
IExecuteFunctions ,
2020-11-10 10:08:48 -08:00
INodeExecutionData ,
INodeType ,
INodeTypeDescription ,
} from 'n8n-workflow' ;
2024-08-29 06:55:53 -07:00
import { NodeConnectionType } from 'n8n-workflow' ;
2020-11-10 10:08:48 -08:00
2022-08-17 08:50:24 -07:00
import { lineApiRequest } from './GenericFunctions' ;
2020-11-10 10:08:48 -08:00
2022-08-17 08:50:24 -07:00
import { notificationFields , notificationOperations } from './NotificationDescription' ;
2020-11-10 10:08:48 -08:00
export class Line implements INodeType {
description : INodeTypeDescription = {
displayName : 'Line' ,
name : 'line' ,
2022-06-20 07:54:01 -07:00
// eslint-disable-next-line n8n-nodes-base/node-class-description-icon-not-svg
2020-11-10 10:08:48 -08:00
icon : 'file:line.png' ,
group : [ 'input' ] ,
version : 1 ,
subtitle : '={{$parameter["operation"] + ": " + $parameter["resource"]}}' ,
2021-07-03 05:40:16 -07:00
description : 'Consume Line API' ,
2020-11-10 10:08:48 -08:00
defaults : {
name : 'Line' ,
} ,
2024-08-29 06:55:53 -07:00
inputs : [ NodeConnectionType . Main ] ,
outputs : [ NodeConnectionType . Main ] ,
2020-11-10 10:08:48 -08:00
credentials : [
{
name : 'lineNotifyOAuth2Api' ,
required : true ,
displayOptions : {
show : {
2022-08-17 08:50:24 -07:00
resource : [ 'notification' ] ,
2020-11-10 10:08:48 -08:00
} ,
} ,
} ,
] ,
properties : [
2024-10-14 09:05:09 -07:00
{
displayName :
'End of service: LINE Notify will be discontinued from April 1st 2025, You can find more information <a href="https://notify-bot.line.me/closing-announce" target="_blank">here</a>' ,
name : 'notice' ,
type : 'notice' ,
default : '' ,
} ,
2020-11-10 10:08:48 -08:00
{
displayName : 'Resource' ,
name : 'resource' ,
type : 'options' ,
2022-05-20 14:47:24 -07:00
noDataExpression : true ,
2020-11-10 10:08:48 -08:00
options : [
{
name : 'Notification' ,
value : 'notification' ,
} ,
] ,
default : 'notification' ,
} ,
. . . notificationOperations ,
. . . notificationFields ,
] ,
} ;
async execute ( this : IExecuteFunctions ) : Promise < INodeExecutionData [ ] [ ] > {
const items = this . getInputData ( ) ;
2022-08-30 08:55:33 -07:00
const returnData : INodeExecutionData [ ] = [ ] ;
2022-04-22 09:29:51 -07:00
const length = items . length ;
2020-11-10 10:08:48 -08:00
let responseData ;
2022-12-02 03:53:59 -08:00
const resource = this . getNodeParameter ( 'resource' , 0 ) ;
const operation = this . getNodeParameter ( 'operation' , 0 ) ;
2020-11-10 10:08:48 -08:00
for ( let i = 0 ; i < length ; i ++ ) {
2021-07-19 23:58:54 -07:00
try {
if ( resource === 'notification' ) {
//https://notify-bot.line.me/doc/en/
if ( operation === 'send' ) {
const message = this . getNodeParameter ( 'message' , i ) as string ;
2020-11-10 10:08:48 -08:00
2022-11-18 07:29:44 -08:00
const additionalFields = this . getNodeParameter ( 'additionalFields' , i ) ;
2020-11-10 10:08:48 -08:00
2021-07-19 23:58:54 -07:00
const body : IDataObject = {
message ,
} ;
2020-11-10 10:08:48 -08:00
2021-07-19 23:58:54 -07:00
Object . assign ( body , additionalFields ) ;
2020-11-10 10:08:48 -08:00
2021-07-19 23:58:54 -07:00
if ( body . hasOwnProperty ( 'notificationDisabled' ) ) {
2022-08-17 08:50:24 -07:00
body . notificationDisabled = body . notificationDisabled ? 'true' : 'false' ;
2020-11-10 10:08:48 -08:00
}
2020-11-10 10:09:13 -08:00
2021-07-19 23:58:54 -07:00
if ( body . stickerUi ) {
const sticker = ( body . stickerUi as IDataObject ) . stickerValue as IDataObject ;
if ( sticker ) {
body . stickerId = sticker . stickerId ;
body . stickerPackageId = sticker . stickerPackageId ;
2020-11-10 10:08:48 -08:00
}
2021-07-19 23:58:54 -07:00
delete body . stickerUi ;
}
2020-11-10 10:08:48 -08:00
2021-07-19 23:58:54 -07:00
if ( body . imageUi ) {
const image = ( body . imageUi as IDataObject ) . imageValue as IDataObject ;
if ( image && image . binaryData === true ) {
2023-03-06 08:33:32 -08:00
const binaryProperty = image . binaryProperty as string ;
const binaryData = this . helpers . assertBinaryData ( i , binaryProperty ) ;
const binaryDataBuffer = await this . helpers . getBinaryDataBuffer ( i , binaryProperty ) ;
2021-07-19 23:58:54 -07:00
body . imageFile = {
2021-08-20 09:08:40 -07:00
value : binaryDataBuffer ,
2021-07-19 23:58:54 -07:00
options : {
filename : binaryData.fileName ,
} ,
} ;
} else {
body . imageFullsize = image . imageFullsize ;
body . imageThumbnail = image . imageThumbnail ;
}
delete body . imageUi ;
2020-11-10 10:09:13 -08:00
}
2022-08-17 08:50:24 -07:00
responseData = await lineApiRequest . call (
this ,
'POST' ,
'' ,
{ } ,
{ } ,
'https://notify-api.line.me/api/notify' ,
{ formData : body } ,
) ;
2020-11-10 10:08:48 -08:00
}
}
2022-08-30 08:55:33 -07:00
const executionData = this . helpers . constructExecutionMetaData (
2023-02-27 19:39:43 -08:00
this . helpers . returnJsonArray ( responseData as IDataObject ) ,
2022-08-30 08:55:33 -07:00
{ itemData : { item : i } } ,
) ;
returnData . push ( . . . executionData ) ;
2021-07-19 23:58:54 -07:00
} catch ( error ) {
2024-08-30 00:59:30 -07:00
if ( this . continueOnFail ( ) ) {
2022-08-30 08:55:33 -07:00
const executionErrorData = this . helpers . constructExecutionMetaData (
this . helpers . returnJsonArray ( { error : error.message } ) ,
{ itemData : { item : i } } ,
) ;
returnData . push ( . . . executionErrorData ) ;
2021-07-19 23:58:54 -07:00
continue ;
}
throw error ;
}
2020-11-10 10:08:48 -08:00
}
2023-09-05 03:59:02 -07:00
return [ returnData ] ;
2020-11-10 10:08:48 -08:00
}
}