first commit
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
Security: Sync from Public / sync-from-public (push) Has been cancelled
Test: Benchmark Nightly / build (push) Has been cancelled
Test: Benchmark Nightly / Notify Cats on failure (push) Has been cancelled
CI: Python / Checks (push) Has been cancelled
Test: Evals Python / Workflow Comparison Python (push) Has been cancelled
Util: Check Docs URLs / check-docs-urls (push) Has been cancelled
Test: Visual Storybook / Cloudflare Pages (push) Has been cancelled
Test: E2E Performance / build-and-test-performance (push) Has been cancelled
Test: Workflows Nightly / Run Workflow Tests (push) Has been cancelled
Util: Cleanup CI Docker Images / Delete stale CI images (push) Has been cancelled
Test: Benchmark Destroy Env / build (push) Has been cancelled
Util: Update Node Popularity / update-popularity (push) Has been cancelled
Test: E2E Coverage Weekly / Coverage Tests (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
import type { INodeProperties } from 'n8n-workflow';
|
||||
|
||||
export const directMessageOperations: INodeProperties[] = [
|
||||
{
|
||||
displayName: 'Operation',
|
||||
name: 'operation',
|
||||
type: 'options',
|
||||
noDataExpression: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['directMessage'],
|
||||
},
|
||||
},
|
||||
options: [
|
||||
{
|
||||
name: 'Create',
|
||||
value: 'create',
|
||||
description: 'Create a direct message',
|
||||
action: 'Create a direct message',
|
||||
},
|
||||
],
|
||||
default: 'create',
|
||||
},
|
||||
];
|
||||
|
||||
export const directMessageFields: INodeProperties[] = [
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* directMessage:create */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
{
|
||||
displayName: 'User ID',
|
||||
name: 'userId',
|
||||
type: 'string',
|
||||
required: true,
|
||||
default: '',
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: ['create'],
|
||||
resource: ['directMessage'],
|
||||
},
|
||||
},
|
||||
description: 'The ID of the user who should receive the direct message',
|
||||
},
|
||||
{
|
||||
displayName: 'Text',
|
||||
name: 'text',
|
||||
type: 'string',
|
||||
required: true,
|
||||
default: '',
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: ['create'],
|
||||
resource: ['directMessage'],
|
||||
},
|
||||
},
|
||||
description:
|
||||
'The text of your Direct Message. URL encode as necessary. Max length of 10,000 characters.',
|
||||
},
|
||||
{
|
||||
displayName: 'Additional Fields',
|
||||
name: 'additionalFields',
|
||||
type: 'collection',
|
||||
placeholder: 'Add Field',
|
||||
default: {},
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: ['create'],
|
||||
resource: ['directMessage'],
|
||||
},
|
||||
},
|
||||
options: [
|
||||
{
|
||||
displayName: 'Attachment',
|
||||
name: 'attachment',
|
||||
type: 'string',
|
||||
default: 'data',
|
||||
description:
|
||||
'Name of the binary property which contain data that should be added to the direct message as attachment',
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
@@ -0,0 +1,176 @@
|
||||
import type {
|
||||
IDataObject,
|
||||
IExecuteFunctions,
|
||||
IHookFunctions,
|
||||
ILoadOptionsFunctions,
|
||||
JsonObject,
|
||||
IRequestOptions,
|
||||
IHttpRequestMethods,
|
||||
} from 'n8n-workflow';
|
||||
import { NodeApiError, NodeOperationError, sleep } from 'n8n-workflow';
|
||||
|
||||
export async function twitterApiRequest(
|
||||
this: IExecuteFunctions | ILoadOptionsFunctions | IHookFunctions,
|
||||
method: IHttpRequestMethods,
|
||||
resource: string,
|
||||
body: IDataObject = {},
|
||||
qs: IDataObject = {},
|
||||
uri?: string,
|
||||
option: IDataObject = {},
|
||||
) {
|
||||
let options: IRequestOptions = {
|
||||
method,
|
||||
body,
|
||||
qs,
|
||||
url: uri || `https://api.twitter.com/1.1${resource}`,
|
||||
json: true,
|
||||
};
|
||||
try {
|
||||
if (Object.keys(option).length !== 0) {
|
||||
options = Object.assign({}, options, option);
|
||||
}
|
||||
if (Object.keys(body).length === 0) {
|
||||
delete options.body;
|
||||
}
|
||||
if (Object.keys(qs).length === 0) {
|
||||
delete options.qs;
|
||||
}
|
||||
return await this.helpers.requestOAuth1.call(this, 'twitterOAuth1Api', options);
|
||||
} catch (error) {
|
||||
throw new NodeApiError(this.getNode(), error as JsonObject);
|
||||
}
|
||||
}
|
||||
|
||||
export async function twitterApiRequestAllItems(
|
||||
this: IExecuteFunctions | ILoadOptionsFunctions,
|
||||
propertyName: string,
|
||||
method: IHttpRequestMethods,
|
||||
endpoint: string,
|
||||
body: IDataObject = {},
|
||||
query: IDataObject = {},
|
||||
) {
|
||||
const returnData: IDataObject[] = [];
|
||||
|
||||
let responseData;
|
||||
|
||||
query.count = 100;
|
||||
|
||||
do {
|
||||
responseData = await twitterApiRequest.call(this, method, endpoint, body, query);
|
||||
query.since_id = responseData.search_metadata.max_id;
|
||||
returnData.push.apply(returnData, responseData[propertyName] as IDataObject[]);
|
||||
} while (responseData.search_metadata?.next_results);
|
||||
|
||||
return returnData;
|
||||
}
|
||||
|
||||
export function chunks(buffer: Buffer, chunkSize: number) {
|
||||
const result = [];
|
||||
const len = buffer.length;
|
||||
let i = 0;
|
||||
|
||||
while (i < len) {
|
||||
result.push(buffer.slice(i, (i += chunkSize)));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function uploadAttachments(
|
||||
this: IExecuteFunctions,
|
||||
binaryProperties: string[],
|
||||
i: number,
|
||||
) {
|
||||
const uploadUri = 'https://upload.twitter.com/1.1/media/upload.json';
|
||||
|
||||
const media: IDataObject[] = [];
|
||||
|
||||
for (const binaryPropertyName of binaryProperties) {
|
||||
let attachmentBody = {};
|
||||
let response: IDataObject = {};
|
||||
|
||||
const binaryData = this.helpers.assertBinaryData(i, binaryPropertyName);
|
||||
const dataBuffer = await this.helpers.getBinaryDataBuffer(i, binaryPropertyName);
|
||||
|
||||
const isAnimatedWebp = dataBuffer.toString().indexOf('ANMF') !== -1;
|
||||
const isImage = binaryData.mimeType.includes('image');
|
||||
|
||||
if (isImage && isAnimatedWebp) {
|
||||
throw new NodeOperationError(
|
||||
this.getNode(),
|
||||
'Animated .webp images are not supported use .gif instead',
|
||||
{ itemIndex: i },
|
||||
);
|
||||
}
|
||||
|
||||
if (isImage) {
|
||||
const form = {
|
||||
media_data: binaryData.data,
|
||||
};
|
||||
|
||||
response = await twitterApiRequest.call(this, 'POST', '', {}, {}, uploadUri, {
|
||||
form,
|
||||
});
|
||||
|
||||
media.push(response);
|
||||
} else {
|
||||
// https://developer.twitter.com/en/docs/media/upload-media/api-reference/post-media-upload-init
|
||||
attachmentBody = {
|
||||
command: 'INIT',
|
||||
total_bytes: dataBuffer.byteLength,
|
||||
media_type: binaryData.mimeType,
|
||||
};
|
||||
|
||||
response = await twitterApiRequest.call(this, 'POST', '', {}, {}, uploadUri, {
|
||||
form: attachmentBody,
|
||||
});
|
||||
|
||||
const mediaId = response.media_id_string;
|
||||
|
||||
// break the data on 5mb chunks (max size that can be uploaded at once)
|
||||
|
||||
const binaryParts = chunks(dataBuffer, 5242880);
|
||||
|
||||
let index = 0;
|
||||
|
||||
for (const binaryPart of binaryParts) {
|
||||
//https://developer.twitter.com/en/docs/media/upload-media/api-reference/post-media-upload-append
|
||||
|
||||
attachmentBody = {
|
||||
name: binaryData.fileName,
|
||||
command: 'APPEND',
|
||||
media_id: mediaId,
|
||||
media_data: Buffer.from(binaryPart).toString('base64'),
|
||||
segment_index: index,
|
||||
};
|
||||
|
||||
response = await twitterApiRequest.call(this, 'POST', '', {}, {}, uploadUri, {
|
||||
form: attachmentBody,
|
||||
});
|
||||
|
||||
index++;
|
||||
}
|
||||
|
||||
//https://developer.twitter.com/en/docs/media/upload-media/api-reference/post-media-upload-finalize
|
||||
|
||||
attachmentBody = {
|
||||
command: 'FINALIZE',
|
||||
media_id: mediaId,
|
||||
};
|
||||
|
||||
response = await twitterApiRequest.call(this, 'POST', '', {}, {}, uploadUri, {
|
||||
form: attachmentBody,
|
||||
});
|
||||
|
||||
// data has not been uploaded yet, so wait for it to be ready
|
||||
if (response.processing_info) {
|
||||
const { check_after_secs } = response.processing_info as IDataObject;
|
||||
await sleep((check_after_secs as number) * 1000);
|
||||
}
|
||||
|
||||
media.push(response);
|
||||
}
|
||||
|
||||
return media;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,438 @@
|
||||
import type { INodeProperties } from 'n8n-workflow';
|
||||
|
||||
export const tweetOperations: INodeProperties[] = [
|
||||
{
|
||||
displayName: 'Operation',
|
||||
name: 'operation',
|
||||
type: 'options',
|
||||
noDataExpression: true,
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: ['tweet'],
|
||||
},
|
||||
},
|
||||
options: [
|
||||
{
|
||||
name: 'Create',
|
||||
value: 'create',
|
||||
description: 'Create or reply a tweet',
|
||||
action: 'Create a tweet',
|
||||
},
|
||||
{
|
||||
name: 'Delete',
|
||||
value: 'delete',
|
||||
description: 'Delete a tweet',
|
||||
action: 'Delete a tweet',
|
||||
},
|
||||
{
|
||||
name: 'Like',
|
||||
value: 'like',
|
||||
description: 'Like a tweet',
|
||||
action: 'Like a tweet',
|
||||
},
|
||||
{
|
||||
name: 'Retweet',
|
||||
value: 'retweet',
|
||||
description: 'Retweet a tweet',
|
||||
action: 'Retweet a tweet',
|
||||
},
|
||||
{
|
||||
name: 'Search',
|
||||
value: 'search',
|
||||
description: 'Search tweets',
|
||||
action: 'Search for tweets',
|
||||
},
|
||||
],
|
||||
default: 'create',
|
||||
},
|
||||
];
|
||||
|
||||
export const tweetFields: INodeProperties[] = [
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* tweet:create */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
{
|
||||
displayName: 'Text',
|
||||
name: 'text',
|
||||
type: 'string',
|
||||
required: true,
|
||||
default: '',
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: ['create'],
|
||||
resource: ['tweet'],
|
||||
},
|
||||
},
|
||||
description:
|
||||
'The text of the status update. URL encode as necessary. t.co link wrapping will affect character counts.',
|
||||
},
|
||||
{
|
||||
displayName: 'Additional Fields',
|
||||
name: 'additionalFields',
|
||||
type: 'collection',
|
||||
placeholder: 'Add Field',
|
||||
default: {},
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: ['create'],
|
||||
resource: ['tweet'],
|
||||
},
|
||||
},
|
||||
options: [
|
||||
{
|
||||
displayName: 'Attachments',
|
||||
name: 'attachments',
|
||||
type: 'string',
|
||||
default: 'data',
|
||||
description:
|
||||
'Name of the binary properties which contain data which should be added to tweet as attachment. Multiple ones can be comma-separated.',
|
||||
},
|
||||
{
|
||||
displayName: 'Display Coordinates',
|
||||
name: 'displayCoordinates',
|
||||
type: 'boolean',
|
||||
default: false,
|
||||
description:
|
||||
'Whether or not to put a pin on the exact coordinates a Tweet has been sent from',
|
||||
},
|
||||
{
|
||||
displayName: 'In Reply to Tweet',
|
||||
name: 'inReplyToStatusId',
|
||||
type: 'string',
|
||||
default: '',
|
||||
description: 'The ID of an existing status that the update is in reply to',
|
||||
},
|
||||
{
|
||||
displayName: 'Location',
|
||||
name: 'locationFieldsUi',
|
||||
type: 'fixedCollection',
|
||||
placeholder: 'Add Location',
|
||||
default: {},
|
||||
description: 'Subscriber location information.n',
|
||||
options: [
|
||||
{
|
||||
name: 'locationFieldsValues',
|
||||
displayName: 'Location',
|
||||
values: [
|
||||
{
|
||||
displayName: 'Latitude',
|
||||
name: 'latitude',
|
||||
type: 'string',
|
||||
required: true,
|
||||
description: 'The location latitude',
|
||||
default: '',
|
||||
},
|
||||
{
|
||||
displayName: 'Longitude',
|
||||
name: 'longitude',
|
||||
type: 'string',
|
||||
required: true,
|
||||
description: 'The location longitude',
|
||||
default: '',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
displayName: 'Possibly Sensitive',
|
||||
name: 'possiblySensitive',
|
||||
type: 'boolean',
|
||||
default: false,
|
||||
description:
|
||||
'Whether you are uploading Tweet media that might be considered sensitive content such as nudity, or medical procedures',
|
||||
},
|
||||
],
|
||||
},
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* tweet:delete */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
{
|
||||
displayName: 'Tweet ID',
|
||||
name: 'tweetId',
|
||||
type: 'string',
|
||||
required: true,
|
||||
default: '',
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: ['delete'],
|
||||
resource: ['tweet'],
|
||||
},
|
||||
},
|
||||
description: 'The ID of the tweet to delete',
|
||||
},
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* tweet:search */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
{
|
||||
displayName: 'Search Text',
|
||||
name: 'searchText',
|
||||
type: 'string',
|
||||
required: true,
|
||||
default: '',
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: ['search'],
|
||||
resource: ['tweet'],
|
||||
},
|
||||
},
|
||||
description:
|
||||
'A UTF-8, URL-encoded search query of 500 characters maximum, including operators. Queries may additionally be limited by complexity. Check the searching examples <a href="https://developer.twitter.com/en/docs/tweets/search/guides/standard-operators">here</a>.',
|
||||
},
|
||||
{
|
||||
displayName: 'Return All',
|
||||
name: 'returnAll',
|
||||
type: 'boolean',
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: ['search'],
|
||||
resource: ['tweet'],
|
||||
},
|
||||
},
|
||||
default: false,
|
||||
description: 'Whether to return all results or only up to a given limit',
|
||||
},
|
||||
{
|
||||
displayName: 'Limit',
|
||||
name: 'limit',
|
||||
type: 'number',
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: ['search'],
|
||||
resource: ['tweet'],
|
||||
returnAll: [false],
|
||||
},
|
||||
},
|
||||
typeOptions: {
|
||||
minValue: 1,
|
||||
},
|
||||
default: 50,
|
||||
description: 'Max number of results to return',
|
||||
},
|
||||
{
|
||||
displayName: 'Additional Fields',
|
||||
name: 'additionalFields',
|
||||
type: 'collection',
|
||||
placeholder: 'Add Field',
|
||||
default: {},
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: ['search'],
|
||||
resource: ['tweet'],
|
||||
},
|
||||
},
|
||||
options: [
|
||||
{
|
||||
displayName: 'Include Entities',
|
||||
name: 'includeEntities',
|
||||
type: 'boolean',
|
||||
default: false,
|
||||
description: 'Whether the entities node will be included',
|
||||
},
|
||||
{
|
||||
displayName: 'Language Name or ID',
|
||||
name: 'lang',
|
||||
type: 'options',
|
||||
typeOptions: {
|
||||
loadOptionsMethod: 'getLanguages',
|
||||
},
|
||||
default: '',
|
||||
description:
|
||||
'Restricts tweets to the given language, given by an ISO 639-1 code. Language detection is best-effort. Choose from the list, or specify an ID using an <a href="https://docs.n8n.io/code/expressions/">expression</a>.',
|
||||
},
|
||||
{
|
||||
displayName: 'Location',
|
||||
name: 'locationFieldsUi',
|
||||
type: 'fixedCollection',
|
||||
placeholder: 'Add Location',
|
||||
default: {},
|
||||
description: 'Subscriber location information.n',
|
||||
options: [
|
||||
{
|
||||
name: 'locationFieldsValues',
|
||||
displayName: 'Location',
|
||||
values: [
|
||||
{
|
||||
displayName: 'Latitude',
|
||||
name: 'latitude',
|
||||
type: 'string',
|
||||
required: true,
|
||||
description: 'The location latitude',
|
||||
default: '',
|
||||
},
|
||||
{
|
||||
displayName: 'Longitude',
|
||||
name: 'longitude',
|
||||
type: 'string',
|
||||
required: true,
|
||||
description: 'The location longitude',
|
||||
default: '',
|
||||
},
|
||||
{
|
||||
displayName: 'Radius',
|
||||
name: 'radius',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'Milles',
|
||||
value: 'mi',
|
||||
},
|
||||
{
|
||||
name: 'Kilometers',
|
||||
value: 'km',
|
||||
},
|
||||
],
|
||||
required: true,
|
||||
description:
|
||||
'Returns tweets by users located within a given radius of the given latitude/longitude',
|
||||
default: '',
|
||||
},
|
||||
{
|
||||
displayName: 'Distance',
|
||||
name: 'distance',
|
||||
type: 'number',
|
||||
typeOptions: {
|
||||
minValue: 0,
|
||||
},
|
||||
required: true,
|
||||
default: '',
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
displayName: 'Result Type',
|
||||
name: 'resultType',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'Mixed',
|
||||
value: 'mixed',
|
||||
description: 'Include both popular and real time results in the response',
|
||||
},
|
||||
{
|
||||
name: 'Recent',
|
||||
value: 'recent',
|
||||
description: 'Return only the most recent results in the response',
|
||||
},
|
||||
{
|
||||
name: 'Popular',
|
||||
value: 'popular',
|
||||
description: 'Return only the most popular results in the response',
|
||||
},
|
||||
],
|
||||
default: 'mixed',
|
||||
description: 'Specifies what type of search results you would prefer to receive',
|
||||
},
|
||||
{
|
||||
displayName: 'Tweet Mode',
|
||||
name: 'tweetMode',
|
||||
type: 'options',
|
||||
options: [
|
||||
{
|
||||
name: 'Compatibility',
|
||||
value: 'compat',
|
||||
},
|
||||
{
|
||||
name: 'Extended',
|
||||
value: 'extended',
|
||||
},
|
||||
],
|
||||
default: 'compat',
|
||||
description:
|
||||
'When the extended mode is selected, the response contains the entire untruncated text of the Tweet',
|
||||
},
|
||||
{
|
||||
displayName: 'Until',
|
||||
name: 'until',
|
||||
type: 'dateTime',
|
||||
default: '',
|
||||
description: 'Returns tweets created before the given date',
|
||||
},
|
||||
],
|
||||
},
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* tweet:like */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
{
|
||||
displayName: 'Tweet ID',
|
||||
name: 'tweetId',
|
||||
type: 'string',
|
||||
required: true,
|
||||
default: '',
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: ['like'],
|
||||
resource: ['tweet'],
|
||||
},
|
||||
},
|
||||
description: 'The ID of the tweet',
|
||||
},
|
||||
{
|
||||
displayName: 'Additional Fields',
|
||||
name: 'additionalFields',
|
||||
type: 'collection',
|
||||
placeholder: 'Add Field',
|
||||
default: {},
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: ['like'],
|
||||
resource: ['tweet'],
|
||||
},
|
||||
},
|
||||
options: [
|
||||
{
|
||||
displayName: 'Include Entities',
|
||||
name: 'includeEntities',
|
||||
type: 'boolean',
|
||||
default: false,
|
||||
description: 'Whether the entities will be omitted',
|
||||
},
|
||||
],
|
||||
},
|
||||
|
||||
/* -------------------------------------------------------------------------- */
|
||||
/* tweet:retweet */
|
||||
/* -------------------------------------------------------------------------- */
|
||||
{
|
||||
displayName: 'Tweet ID',
|
||||
name: 'tweetId',
|
||||
type: 'string',
|
||||
required: true,
|
||||
default: '',
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: ['retweet'],
|
||||
resource: ['tweet'],
|
||||
},
|
||||
},
|
||||
description: 'The ID of the tweet',
|
||||
},
|
||||
{
|
||||
displayName: 'Additional Fields',
|
||||
name: 'additionalFields',
|
||||
type: 'collection',
|
||||
placeholder: 'Add Field',
|
||||
default: {},
|
||||
displayOptions: {
|
||||
show: {
|
||||
operation: ['retweet'],
|
||||
resource: ['tweet'],
|
||||
},
|
||||
},
|
||||
options: [
|
||||
{
|
||||
displayName: 'Trim User',
|
||||
name: 'trimUser',
|
||||
type: 'boolean',
|
||||
default: false,
|
||||
description:
|
||||
'Whether each tweet returned in a timeline will include a user object including only the status authors numerical ID',
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
@@ -0,0 +1,25 @@
|
||||
import type { IDataObject } from 'n8n-workflow';
|
||||
|
||||
export interface ITweet {
|
||||
auto_populate_reply_metadata?: boolean;
|
||||
display_coordinates?: boolean;
|
||||
lat?: number;
|
||||
long?: number;
|
||||
media_ids?: string;
|
||||
possibly_sensitive?: boolean;
|
||||
status: string;
|
||||
in_reply_to_status_id?: string;
|
||||
}
|
||||
|
||||
export interface ITweetCreate {
|
||||
type: 'message_create';
|
||||
message_create: {
|
||||
target: {
|
||||
recipient_id: string;
|
||||
};
|
||||
message_data: {
|
||||
text: string;
|
||||
attachment?: IDataObject;
|
||||
};
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,334 @@
|
||||
import ISO6391 from 'iso-639-1';
|
||||
import type {
|
||||
IDataObject,
|
||||
IExecuteFunctions,
|
||||
ILoadOptionsFunctions,
|
||||
INodeExecutionData,
|
||||
INodePropertyOptions,
|
||||
INodeType,
|
||||
INodeTypeBaseDescription,
|
||||
INodeTypeDescription,
|
||||
JsonObject,
|
||||
} from 'n8n-workflow';
|
||||
import { NodeConnectionTypes } from 'n8n-workflow';
|
||||
|
||||
import { directMessageFields, directMessageOperations } from './DirectMessageDescription';
|
||||
import {
|
||||
twitterApiRequest,
|
||||
twitterApiRequestAllItems,
|
||||
uploadAttachments,
|
||||
} from './GenericFunctions';
|
||||
import { tweetFields, tweetOperations } from './TweetDescription';
|
||||
import type { ITweet, ITweetCreate } from './TweetInterface';
|
||||
|
||||
export class TwitterV1 implements INodeType {
|
||||
description: INodeTypeDescription;
|
||||
|
||||
constructor(baseDecription: INodeTypeBaseDescription) {
|
||||
this.description = {
|
||||
...baseDecription,
|
||||
version: 1,
|
||||
description: 'Consume Twitter API',
|
||||
subtitle: '={{$parameter["operation"] + ":" + $parameter["resource"]}}',
|
||||
defaults: {
|
||||
name: 'Twitter',
|
||||
},
|
||||
inputs: [NodeConnectionTypes.Main],
|
||||
outputs: [NodeConnectionTypes.Main],
|
||||
credentials: [
|
||||
{
|
||||
name: 'twitterOAuth1Api',
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
properties: [
|
||||
{
|
||||
displayName: 'Resource',
|
||||
name: 'resource',
|
||||
type: 'options',
|
||||
noDataExpression: true,
|
||||
options: [
|
||||
{
|
||||
name: 'Direct Message',
|
||||
value: 'directMessage',
|
||||
},
|
||||
{
|
||||
name: 'Tweet',
|
||||
value: 'tweet',
|
||||
},
|
||||
],
|
||||
default: 'tweet',
|
||||
},
|
||||
// DIRECT MESSAGE
|
||||
...directMessageOperations,
|
||||
...directMessageFields,
|
||||
// TWEET
|
||||
...tweetOperations,
|
||||
...tweetFields,
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
methods = {
|
||||
loadOptions: {
|
||||
// Get all the available languages to display them to user so that they can
|
||||
// select them easily
|
||||
async getLanguages(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
|
||||
const returnData: INodePropertyOptions[] = [];
|
||||
const languages = ISO6391.getAllNames();
|
||||
for (const language of languages) {
|
||||
const languageName = language;
|
||||
const languageId = ISO6391.getCode(language);
|
||||
returnData.push({
|
||||
name: languageName,
|
||||
value: languageId,
|
||||
});
|
||||
}
|
||||
return returnData;
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
const items = this.getInputData();
|
||||
const returnData: INodeExecutionData[] = [];
|
||||
const length = items.length;
|
||||
let responseData;
|
||||
const resource = this.getNodeParameter('resource', 0);
|
||||
const operation = this.getNodeParameter('operation', 0);
|
||||
for (let i = 0; i < length; i++) {
|
||||
try {
|
||||
if (resource === 'directMessage') {
|
||||
//https://developer.twitter.com/en/docs/twitter-api/v1/direct-messages/sending-and-receiving/api-reference/new-event
|
||||
if (operation === 'create') {
|
||||
const userId = this.getNodeParameter('userId', i) as string;
|
||||
const text = this.getNodeParameter('text', i) as string;
|
||||
const additionalFields = this.getNodeParameter('additionalFields', i);
|
||||
const body: ITweetCreate = {
|
||||
type: 'message_create',
|
||||
message_create: {
|
||||
target: {
|
||||
recipient_id: userId,
|
||||
},
|
||||
message_data: {
|
||||
text,
|
||||
attachment: {},
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
if (additionalFields.attachment) {
|
||||
const attachment = additionalFields.attachment as string;
|
||||
|
||||
const attachmentProperties: string[] = attachment.split(',').map((propertyName) => {
|
||||
return propertyName.trim();
|
||||
});
|
||||
|
||||
const medias = await uploadAttachments.call(this, attachmentProperties, i);
|
||||
body.message_create.message_data.attachment = {
|
||||
type: 'media',
|
||||
//@ts-ignore
|
||||
media: { id: medias[0].media_id_string },
|
||||
};
|
||||
} else {
|
||||
delete body.message_create.message_data.attachment;
|
||||
}
|
||||
|
||||
responseData = await twitterApiRequest.call(
|
||||
this,
|
||||
'POST',
|
||||
'/direct_messages/events/new.json',
|
||||
{ event: body },
|
||||
);
|
||||
|
||||
responseData = responseData.event;
|
||||
}
|
||||
}
|
||||
if (resource === 'tweet') {
|
||||
// https://developer.twitter.com/en/docs/tweets/post-and-engage/api-reference/post-statuses-update
|
||||
if (operation === 'create') {
|
||||
const text = this.getNodeParameter('text', i) as string;
|
||||
const additionalFields = this.getNodeParameter('additionalFields', i);
|
||||
const body: ITweet = {
|
||||
status: text,
|
||||
};
|
||||
|
||||
if (additionalFields.inReplyToStatusId) {
|
||||
body.in_reply_to_status_id = additionalFields.inReplyToStatusId as string;
|
||||
body.auto_populate_reply_metadata = true;
|
||||
}
|
||||
|
||||
if (additionalFields.attachments) {
|
||||
const attachments = additionalFields.attachments as string;
|
||||
|
||||
const attachmentProperties: string[] = attachments.split(',').map((propertyName) => {
|
||||
return propertyName.trim();
|
||||
});
|
||||
|
||||
const medias = await uploadAttachments.call(this, attachmentProperties, i);
|
||||
|
||||
body.media_ids = (medias as IDataObject[])
|
||||
.map((media: IDataObject) => media.media_id_string)
|
||||
.join(',');
|
||||
}
|
||||
|
||||
if (additionalFields.possiblySensitive) {
|
||||
body.possibly_sensitive = additionalFields.possiblySensitive as boolean;
|
||||
}
|
||||
|
||||
if (additionalFields.displayCoordinates) {
|
||||
body.display_coordinates = additionalFields.displayCoordinates as boolean;
|
||||
}
|
||||
|
||||
if (additionalFields.locationFieldsUi) {
|
||||
const locationUi = additionalFields.locationFieldsUi as IDataObject;
|
||||
if (locationUi.locationFieldsValues) {
|
||||
const values = locationUi.locationFieldsValues as IDataObject;
|
||||
body.lat = parseFloat(values.latitude as string);
|
||||
body.long = parseFloat(values.longitude as string);
|
||||
}
|
||||
}
|
||||
|
||||
responseData = await twitterApiRequest.call(
|
||||
this,
|
||||
'POST',
|
||||
'/statuses/update.json',
|
||||
{},
|
||||
body as unknown as IDataObject,
|
||||
);
|
||||
}
|
||||
// https://developer.twitter.com/en/docs/twitter-api/v1/tweets/post-and-engage/api-reference/post-statuses-destroy-id
|
||||
if (operation === 'delete') {
|
||||
const tweetId = this.getNodeParameter('tweetId', i) as string;
|
||||
|
||||
responseData = await twitterApiRequest.call(
|
||||
this,
|
||||
'POST',
|
||||
`/statuses/destroy/${tweetId}.json`,
|
||||
{},
|
||||
{},
|
||||
);
|
||||
}
|
||||
// https://developer.twitter.com/en/docs/tweets/search/api-reference/get-search-tweets
|
||||
if (operation === 'search') {
|
||||
const q = this.getNodeParameter('searchText', i) as string;
|
||||
const returnAll = this.getNodeParameter('returnAll', i);
|
||||
const additionalFields = this.getNodeParameter('additionalFields', i);
|
||||
const qs: IDataObject = {
|
||||
q,
|
||||
};
|
||||
|
||||
if (additionalFields.includeEntities) {
|
||||
qs.include_entities = additionalFields.includeEntities as boolean;
|
||||
}
|
||||
|
||||
if (additionalFields.resultType) {
|
||||
qs.response_type = additionalFields.resultType as string;
|
||||
}
|
||||
|
||||
if (additionalFields.until) {
|
||||
qs.until = additionalFields.until as string;
|
||||
}
|
||||
|
||||
if (additionalFields.lang) {
|
||||
qs.lang = additionalFields.lang as string;
|
||||
}
|
||||
|
||||
if (additionalFields.locationFieldsUi) {
|
||||
const locationUi = additionalFields.locationFieldsUi as IDataObject;
|
||||
if (locationUi.locationFieldsValues) {
|
||||
const values = locationUi.locationFieldsValues as IDataObject;
|
||||
qs.geocode = `${values.latitude as string},${values.longitude as string},${
|
||||
values.distance
|
||||
}${values.radius}`;
|
||||
}
|
||||
}
|
||||
|
||||
qs.tweet_mode = additionalFields.tweetMode || 'compat';
|
||||
|
||||
if (returnAll) {
|
||||
responseData = await twitterApiRequestAllItems.call(
|
||||
this,
|
||||
'statuses',
|
||||
'GET',
|
||||
'/search/tweets.json',
|
||||
{},
|
||||
qs,
|
||||
);
|
||||
} else {
|
||||
qs.count = this.getNodeParameter('limit', 0);
|
||||
responseData = await twitterApiRequest.call(
|
||||
this,
|
||||
'GET',
|
||||
'/search/tweets.json',
|
||||
{},
|
||||
qs,
|
||||
);
|
||||
responseData = responseData.statuses;
|
||||
}
|
||||
}
|
||||
//https://developer.twitter.com/en/docs/twitter-api/v1/tweets/post-and-engage/api-reference/post-favorites-create
|
||||
if (operation === 'like') {
|
||||
const tweetId = this.getNodeParameter('tweetId', i) as string;
|
||||
const additionalFields = this.getNodeParameter('additionalFields', i);
|
||||
|
||||
const qs: IDataObject = {
|
||||
id: tweetId,
|
||||
};
|
||||
|
||||
if (additionalFields.includeEntities) {
|
||||
qs.include_entities = additionalFields.includeEntities as boolean;
|
||||
}
|
||||
|
||||
responseData = await twitterApiRequest.call(
|
||||
this,
|
||||
'POST',
|
||||
'/favorites/create.json',
|
||||
{},
|
||||
qs,
|
||||
);
|
||||
}
|
||||
//https://developer.twitter.com/en/docs/twitter-api/v1/tweets/post-and-engage/api-reference/post-statuses-retweet-id
|
||||
if (operation === 'retweet') {
|
||||
const tweetId = this.getNodeParameter('tweetId', i) as string;
|
||||
const additionalFields = this.getNodeParameter('additionalFields', i);
|
||||
|
||||
const qs: IDataObject = {
|
||||
id: tweetId,
|
||||
};
|
||||
|
||||
if (additionalFields.trimUser) {
|
||||
qs.trim_user = additionalFields.trimUser as boolean;
|
||||
}
|
||||
|
||||
responseData = await twitterApiRequest.call(
|
||||
this,
|
||||
'POST',
|
||||
`/statuses/retweet/${tweetId}.json`,
|
||||
{},
|
||||
qs,
|
||||
);
|
||||
}
|
||||
}
|
||||
const executionData = this.helpers.constructExecutionMetaData(
|
||||
this.helpers.returnJsonArray(responseData as IDataObject[]),
|
||||
{ itemData: { item: i } },
|
||||
);
|
||||
returnData.push(...executionData);
|
||||
} catch (error) {
|
||||
if (this.continueOnFail()) {
|
||||
const executionErrorData = {
|
||||
json: {
|
||||
error: (error as JsonObject).message,
|
||||
},
|
||||
};
|
||||
returnData.push(executionErrorData);
|
||||
continue;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
return [returnData];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user