mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 18:12:04 +00:00
feat(Twitter Node): Node overhaul (#4788)
* First node set up. * Progress: all Resources and Operations updated * Upsates to all resources. * Updated tooltip for Tweet > Search > Tweet fields. * Upodate to resource locator items in User > Search. * Added e.g. to placeholders and minor copy tweaks. * Fixed Operations sorting. * Added a couple of operations back. * Removed 'Authorized API Call'. * Remove authorization header when empty * Import pkce * Add OAuth2 with new grant type to Twitter * Add pkce logic auto assign authorization code if pkce not defined * Add pkce to ui and interfaces * Fix scopes for Oauth2 twitter * Deubg + pass it through header * Add debug console, add airtable cred * Remove all console.logs, make PKCE in th body only when it exists * Remove invalid character ~ * Remove more console.logs * remove body inside query * Remove useless grantype check * Hide oauth2 twitter waiting for overhaul * Remove redundant header removal * Remove more console.logs * Add V2 twitter * Add V1 * Fix description V1, V2 * Fix description for V1 * Add Oauth2 request * Add user lookup * Add search username by ID * Search tweet feat * Wip create tweet * Generic function for returning ID * Add like and retweet feat * Add delete tweet feat * Fix Location tweets * Fix type * Feat List add members * Add scopes for dm and list * Add direct message feature * Improve response data * Fix regex * Add unit test to Twitter v2 * Fix unit test * Remove console.logs * Remove more console.logs * Handle @ in username * Minor copy tweaks. * Add return all logic * Add error for api permission error * Update message api error * Add error for date error * Add notice for TwitterOAuth2 api link * Fix display names location * fix List RLC * Fix like endpoint * Fix error message check * fix(core): Fix OAuth2 callback for grantType=clientCredentials * Improve fix for callback * update pnpm * Fix iso time for end time * sync oauth2Credential * remove unused codeVerifer in Server.ts * Add location and attachments notice * Add notice to oauth1 * Improve notice for twitter * moved credentials notice to TwitterOAuth1Api.credentials.ts --------- Co-authored-by: agobrech <ael.gobrecht@gmail.com> Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in> Co-authored-by: Marcus <marcus@n8n.io>
This commit is contained in:
365
packages/nodes-base/nodes/Twitter/V2/TwitterV2.node.ts
Normal file
365
packages/nodes-base/nodes/Twitter/V2/TwitterV2.node.ts
Normal file
@@ -0,0 +1,365 @@
|
||||
import type {
|
||||
IDataObject,
|
||||
IExecuteFunctions,
|
||||
ILoadOptionsFunctions,
|
||||
INodeExecutionData,
|
||||
INodeParameterResourceLocator,
|
||||
INodePropertyOptions,
|
||||
INodeType,
|
||||
INodeTypeBaseDescription,
|
||||
INodeTypeDescription,
|
||||
JsonObject,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { directMessageOperations, directMessageFields } from './DirectMessageDescription';
|
||||
import { listOperations, listFields } from './ListDescription';
|
||||
import { tweetFields, tweetOperations } from './TweetDescription';
|
||||
import { userOperations, userFields } from './UserDescription';
|
||||
|
||||
import ISO6391 from 'iso-639-1';
|
||||
import {
|
||||
returnId,
|
||||
returnIdFromUsername,
|
||||
twitterApiRequest,
|
||||
twitterApiRequestAllItems,
|
||||
} from './GenericFunctions';
|
||||
import { DateTime } from 'luxon';
|
||||
|
||||
export class TwitterV2 implements INodeType {
|
||||
description: INodeTypeDescription;
|
||||
|
||||
constructor(baseDescription: INodeTypeBaseDescription) {
|
||||
this.description = {
|
||||
...baseDescription,
|
||||
version: 2,
|
||||
description:
|
||||
'Post, like, and search tweets, send messages, search users, and add users to lists',
|
||||
subtitle: '={{$parameter["operation"] + ":" + $parameter["resource"]}}',
|
||||
defaults: {
|
||||
name: 'Twitter',
|
||||
},
|
||||
inputs: ['main'],
|
||||
outputs: ['main'],
|
||||
credentials: [
|
||||
{
|
||||
name: 'twitterOAuth2Api',
|
||||
required: true,
|
||||
},
|
||||
],
|
||||
properties: [
|
||||
{
|
||||
displayName: 'Resource',
|
||||
name: 'resource',
|
||||
type: 'options',
|
||||
noDataExpression: true,
|
||||
options: [
|
||||
{
|
||||
name: 'Direct Message',
|
||||
value: 'directMessage',
|
||||
description: 'Send a direct message to a user',
|
||||
},
|
||||
{
|
||||
name: 'List',
|
||||
value: 'list',
|
||||
description: 'Add a user to a list',
|
||||
},
|
||||
{
|
||||
name: 'Tweet',
|
||||
value: 'tweet',
|
||||
description: 'Create, like, search, or delete a tweet',
|
||||
},
|
||||
{
|
||||
name: 'User',
|
||||
value: 'user',
|
||||
description: 'Search users by username',
|
||||
},
|
||||
],
|
||||
default: 'tweet',
|
||||
},
|
||||
// DIRECT MESSAGE
|
||||
...directMessageOperations,
|
||||
...directMessageFields,
|
||||
// LIST
|
||||
...listOperations,
|
||||
...listFields,
|
||||
// TWEET
|
||||
...tweetOperations,
|
||||
...tweetFields,
|
||||
// USER
|
||||
...userOperations,
|
||||
...userFields,
|
||||
],
|
||||
};
|
||||
}
|
||||
|
||||
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 === 'user') {
|
||||
if (operation === 'searchUser') {
|
||||
const me = this.getNodeParameter('me', i, false) as boolean;
|
||||
if (me) {
|
||||
responseData = await twitterApiRequest.call(this, 'GET', '/users/me', {});
|
||||
} else {
|
||||
const userRlc = this.getNodeParameter(
|
||||
'user',
|
||||
i,
|
||||
undefined,
|
||||
{},
|
||||
) as INodeParameterResourceLocator;
|
||||
if (userRlc.mode === 'username') {
|
||||
userRlc.value = (userRlc.value as string).includes('@')
|
||||
? (userRlc.value as string).replace('@', '')
|
||||
: userRlc.value;
|
||||
responseData = await twitterApiRequest.call(
|
||||
this,
|
||||
'GET',
|
||||
`/users/by/username/${userRlc.value}`,
|
||||
{},
|
||||
);
|
||||
} else if (userRlc.mode === 'id') {
|
||||
responseData = await twitterApiRequest.call(
|
||||
this,
|
||||
'GET',
|
||||
`/users/${userRlc.value}`,
|
||||
{},
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (resource === 'tweet') {
|
||||
if (operation === 'search') {
|
||||
const searchText = this.getNodeParameter('searchText', i, '', {});
|
||||
const returnAll = this.getNodeParameter('returnAll', i);
|
||||
const { sortOrder, startTime, endTime, tweetFieldsObject } = this.getNodeParameter(
|
||||
'additionalFields',
|
||||
i,
|
||||
{},
|
||||
) as {
|
||||
sortOrder: string;
|
||||
startTime: string;
|
||||
endTime: string;
|
||||
tweetFieldsObject: string[];
|
||||
};
|
||||
const qs: IDataObject = {
|
||||
query: searchText,
|
||||
};
|
||||
if (endTime) {
|
||||
const endTimeISO = DateTime.fromISO(endTime).toISO();
|
||||
qs.end_time = endTimeISO;
|
||||
}
|
||||
if (sortOrder) {
|
||||
qs.sort_order = sortOrder;
|
||||
}
|
||||
if (startTime) {
|
||||
const startTimeISO8601 = DateTime.fromISO(startTime).toISO();
|
||||
qs.start_time = startTimeISO8601;
|
||||
}
|
||||
if (tweetFieldsObject) {
|
||||
if (tweetFieldsObject.length > 0) {
|
||||
qs['tweet.fields'] = tweetFieldsObject.join(',');
|
||||
}
|
||||
}
|
||||
if (returnAll) {
|
||||
responseData = await twitterApiRequestAllItems.call(
|
||||
this,
|
||||
'data',
|
||||
'GET',
|
||||
'/tweets/search/recent',
|
||||
{},
|
||||
qs,
|
||||
);
|
||||
} else {
|
||||
const limit = this.getNodeParameter('limit', i);
|
||||
qs.max_results = limit;
|
||||
responseData = await twitterApiRequest.call(
|
||||
this,
|
||||
'GET',
|
||||
'/tweets/search/recent',
|
||||
{},
|
||||
qs,
|
||||
);
|
||||
}
|
||||
}
|
||||
if (operation === 'create') {
|
||||
const text = this.getNodeParameter('text', i, '', {});
|
||||
const { location, attachments, inQuoteToStatusId, inReplyToStatusId } =
|
||||
this.getNodeParameter('additionalFields', i, {}) as {
|
||||
location: string;
|
||||
attachments: string;
|
||||
inQuoteToStatusId: INodeParameterResourceLocator;
|
||||
inReplyToStatusId: INodeParameterResourceLocator;
|
||||
};
|
||||
const body: IDataObject = {
|
||||
text,
|
||||
};
|
||||
if (location) {
|
||||
body.geo = { place_id: location };
|
||||
}
|
||||
if (attachments) {
|
||||
body.media = { media_ids: [attachments] };
|
||||
}
|
||||
if (inQuoteToStatusId) {
|
||||
body.quote_tweet_id = returnId(inQuoteToStatusId);
|
||||
}
|
||||
if (inReplyToStatusId) {
|
||||
const inReplyToStatusIdValue = { in_reply_to_tweet_id: returnId(inReplyToStatusId) };
|
||||
body.reply = inReplyToStatusIdValue;
|
||||
}
|
||||
responseData = await twitterApiRequest.call(this, 'POST', '/tweets', body);
|
||||
}
|
||||
if (operation === 'delete') {
|
||||
const tweetRLC = this.getNodeParameter(
|
||||
'tweetDeleteId',
|
||||
i,
|
||||
'',
|
||||
{},
|
||||
) as INodeParameterResourceLocator;
|
||||
const tweetId = returnId(tweetRLC);
|
||||
responseData = await twitterApiRequest.call(this, 'DELETE', `/tweets/${tweetId}`, {});
|
||||
}
|
||||
if (operation === 'like') {
|
||||
const tweetRLC = this.getNodeParameter(
|
||||
'tweetId',
|
||||
i,
|
||||
'',
|
||||
{},
|
||||
) as INodeParameterResourceLocator;
|
||||
const tweetId = returnId(tweetRLC);
|
||||
const body: IDataObject = {
|
||||
tweet_id: tweetId,
|
||||
};
|
||||
const user = (await twitterApiRequest.call(this, 'GET', '/users/me', {})) as {
|
||||
id: string;
|
||||
};
|
||||
responseData = await twitterApiRequest.call(
|
||||
this,
|
||||
'POST',
|
||||
`/users/${user.id}/likes`,
|
||||
body,
|
||||
);
|
||||
}
|
||||
if (operation === 'retweet') {
|
||||
const tweetRLC = this.getNodeParameter(
|
||||
'tweetId',
|
||||
i,
|
||||
'',
|
||||
{},
|
||||
) as INodeParameterResourceLocator;
|
||||
const tweetId = returnId(tweetRLC);
|
||||
const body: IDataObject = {
|
||||
tweet_id: tweetId,
|
||||
};
|
||||
const user = (await twitterApiRequest.call(this, 'GET', '/users/me', {})) as {
|
||||
id: string;
|
||||
};
|
||||
responseData = await twitterApiRequest.call(
|
||||
this,
|
||||
'POST',
|
||||
`/users/${user.id}/retweets`,
|
||||
body,
|
||||
);
|
||||
}
|
||||
}
|
||||
if (resource === 'list') {
|
||||
if (operation === 'add') {
|
||||
const userRlc = this.getNodeParameter(
|
||||
'user',
|
||||
i,
|
||||
'',
|
||||
{},
|
||||
) as INodeParameterResourceLocator;
|
||||
const userId =
|
||||
userRlc.mode !== 'username'
|
||||
? returnId(userRlc)
|
||||
: await returnIdFromUsername.call(this, userRlc);
|
||||
const listRlc = this.getNodeParameter(
|
||||
'list',
|
||||
i,
|
||||
'',
|
||||
{},
|
||||
) as INodeParameterResourceLocator;
|
||||
const listId = returnId(listRlc);
|
||||
responseData = await twitterApiRequest.call(this, 'POST', `/lists/${listId}/members`, {
|
||||
user_id: userId,
|
||||
});
|
||||
}
|
||||
}
|
||||
if (resource === 'directMessage') {
|
||||
if (operation === 'create') {
|
||||
const userRlc = this.getNodeParameter(
|
||||
'user',
|
||||
i,
|
||||
'',
|
||||
{},
|
||||
) as INodeParameterResourceLocator;
|
||||
const user = await returnIdFromUsername.call(this, userRlc);
|
||||
const text = this.getNodeParameter('text', i, '', {});
|
||||
const { attachments } = this.getNodeParameter('additionalFields', i, {}, {}) as {
|
||||
attachments: number;
|
||||
};
|
||||
const body: IDataObject = {
|
||||
text,
|
||||
};
|
||||
|
||||
if (attachments) {
|
||||
body.attachments = [{ media_id: attachments }];
|
||||
}
|
||||
|
||||
responseData = await twitterApiRequest.call(
|
||||
this,
|
||||
'POST',
|
||||
`/dm_conversations/with/${user}/messages`,
|
||||
body,
|
||||
);
|
||||
}
|
||||
}
|
||||
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 this.prepareOutputData(returnData);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user