2020-06-01 17:42:38 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
2020-06-07 14:29:13 -07:00
|
|
|
ILoadOptionsFunctions,
|
2020-06-03 06:06:13 -07:00
|
|
|
} from 'n8n-core';
|
2020-06-01 17:42:38 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
IDataObject,
|
|
|
|
INodeExecutionData,
|
2020-10-01 05:01:39 -07:00
|
|
|
INodePropertyOptions,
|
2020-06-01 17:42:38 -07:00
|
|
|
INodeType,
|
|
|
|
INodeTypeDescription,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2020-11-03 14:01:38 -08:00
|
|
|
import {
|
|
|
|
directMessageFields,
|
|
|
|
directMessageOperations,
|
|
|
|
} from './DirectMessageDescription';
|
|
|
|
|
2020-06-01 17:42:38 -07:00
|
|
|
import {
|
|
|
|
tweetFields,
|
|
|
|
tweetOperations,
|
|
|
|
} from './TweetDescription';
|
|
|
|
|
|
|
|
import {
|
|
|
|
twitterApiRequest,
|
2020-06-07 14:29:13 -07:00
|
|
|
twitterApiRequestAllItems,
|
2020-11-03 14:01:38 -08:00
|
|
|
uploadAttachments,
|
2020-06-01 17:42:38 -07:00
|
|
|
} from './GenericFunctions';
|
|
|
|
|
|
|
|
import {
|
|
|
|
ITweet,
|
2020-06-03 06:06:13 -07:00
|
|
|
} from './TweetInterface';
|
2020-06-01 17:42:38 -07:00
|
|
|
|
2020-06-07 14:29:13 -07:00
|
|
|
const ISO6391 = require('iso-639-1');
|
2020-06-01 17:42:38 -07:00
|
|
|
|
|
|
|
export class Twitter implements INodeType {
|
|
|
|
description: INodeTypeDescription = {
|
2021-12-23 04:30:35 -08:00
|
|
|
displayName: 'Twitter',
|
2020-06-01 17:42:38 -07:00
|
|
|
name: 'twitter',
|
2021-06-12 12:00:37 -07:00
|
|
|
icon: 'file:twitter.svg',
|
2020-06-01 17:42:38 -07:00
|
|
|
group: ['input', 'output'],
|
|
|
|
version: 1,
|
|
|
|
description: 'Consume Twitter API',
|
|
|
|
subtitle: '={{$parameter["operation"] + ":" + $parameter["resource"]}}',
|
|
|
|
defaults: {
|
|
|
|
name: 'Twitter',
|
|
|
|
},
|
|
|
|
inputs: ['main'],
|
|
|
|
outputs: ['main'],
|
|
|
|
credentials: [
|
|
|
|
{
|
|
|
|
name: 'twitterOAuth1Api',
|
|
|
|
required: true,
|
2020-10-22 06:46:03 -07:00
|
|
|
},
|
2020-06-01 17:42:38 -07:00
|
|
|
],
|
|
|
|
properties: [
|
|
|
|
{
|
|
|
|
displayName: 'Resource',
|
|
|
|
name: 'resource',
|
|
|
|
type: 'options',
|
|
|
|
options: [
|
2020-11-03 14:01:38 -08:00
|
|
|
{
|
|
|
|
name: 'Direct Message',
|
|
|
|
value: 'directMessage',
|
|
|
|
},
|
2020-06-01 17:42:38 -07:00
|
|
|
{
|
|
|
|
name: 'Tweet',
|
|
|
|
value: 'tweet',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
default: 'tweet',
|
|
|
|
description: 'The resource to operate on.',
|
|
|
|
},
|
2020-11-03 14:01:38 -08:00
|
|
|
// DIRECT MESSAGE
|
|
|
|
...directMessageOperations,
|
|
|
|
...directMessageFields,
|
2020-06-01 17:42:38 -07:00
|
|
|
// TWEET
|
|
|
|
...tweetOperations,
|
|
|
|
...tweetFields,
|
|
|
|
],
|
|
|
|
};
|
|
|
|
|
2020-06-07 14:29:13 -07:00
|
|
|
methods = {
|
|
|
|
loadOptions: {
|
|
|
|
// Get all the available languages to display them to user so that he 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;
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-06-01 17:42:38 -07:00
|
|
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
|
|
|
const items = this.getInputData();
|
|
|
|
const returnData: IDataObject[] = [];
|
2022-04-22 09:29:51 -07:00
|
|
|
const length = items.length;
|
2020-06-01 17:42:38 -07:00
|
|
|
let responseData;
|
|
|
|
const resource = this.getNodeParameter('resource', 0) as string;
|
|
|
|
const operation = this.getNodeParameter('operation', 0) as string;
|
|
|
|
for (let i = 0; i < length; i++) {
|
2021-07-19 23:58:54 -07:00
|
|
|
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) as IDataObject;
|
|
|
|
const body: IDataObject = {
|
|
|
|
type: 'message_create',
|
|
|
|
message_create: {
|
|
|
|
target: {
|
|
|
|
recipient_id: userId,
|
|
|
|
},
|
|
|
|
message_data: {
|
|
|
|
text,
|
|
|
|
attachment: {},
|
|
|
|
},
|
2020-11-03 14:01:38 -08:00
|
|
|
},
|
2021-07-19 23:58:54 -07:00
|
|
|
};
|
2020-11-03 14:01:38 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.attachment) {
|
|
|
|
const attachment = additionalFields.attachment as string;
|
2020-11-03 14:01:38 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const attachmentProperties: string[] = attachment.split(',').map((propertyName) => {
|
|
|
|
return propertyName.trim();
|
|
|
|
});
|
2020-09-17 14:02:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const medias = await uploadAttachments.call(this, attachmentProperties, items, i);
|
|
|
|
//@ts-ignore
|
|
|
|
body.message_create.message_data.attachment = { type: 'media', media: { id: medias[0].media_id_string } };
|
|
|
|
} else {
|
|
|
|
//@ts-ignore
|
|
|
|
delete body.message_create.message_data.attachment;
|
|
|
|
}
|
2020-11-03 14:01:38 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await twitterApiRequest.call(this, 'POST', '/direct_messages/events/new.json', { event: body });
|
2020-06-03 06:06:13 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
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) as IDataObject;
|
|
|
|
const body: ITweet = {
|
|
|
|
status: text,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (additionalFields.inReplyToStatusId) {
|
|
|
|
body.in_reply_to_status_id = additionalFields.inReplyToStatusId as string;
|
|
|
|
body.auto_populate_reply_metadata = true;
|
|
|
|
}
|
2020-06-01 17:42:38 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.attachments) {
|
2020-11-03 14:01:59 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const attachments = additionalFields.attachments as string;
|
2020-06-03 06:06:13 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const attachmentProperties: string[] = attachments.split(',').map((propertyName) => {
|
|
|
|
return propertyName.trim();
|
|
|
|
});
|
2020-11-03 14:01:38 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const medias = await uploadAttachments.call(this, attachmentProperties, items, i);
|
2020-06-03 06:06:13 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
body.media_ids = (medias as IDataObject[]).map((media: IDataObject) => media.media_id_string).join(',');
|
2020-06-01 17:42:38 -07:00
|
|
|
}
|
2020-06-03 06:06:13 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.possiblySensitive) {
|
|
|
|
body.possibly_sensitive = additionalFields.possiblySensitive as boolean;
|
|
|
|
}
|
2021-02-06 07:08:47 -08:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.displayCoordinates) {
|
|
|
|
body.display_coordinates = additionalFields.displayCoordinates as boolean;
|
|
|
|
}
|
2020-06-07 14:29:13 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2020-06-07 14:29:13 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await twitterApiRequest.call(this, 'POST', '/statuses/update.json', {}, body as unknown as IDataObject);
|
2020-06-07 14:29:13 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
// 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;
|
2020-06-07 14:29:13 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await twitterApiRequest.call(this, 'POST', `/statuses/destroy/${tweetId}.json`, {}, {});
|
2020-06-07 14:29:13 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
// 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) as boolean;
|
|
|
|
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
|
|
|
|
const qs: IDataObject = {
|
|
|
|
q,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (additionalFields.includeEntities) {
|
|
|
|
qs.include_entities = additionalFields.includeEntities as boolean;
|
|
|
|
}
|
2020-06-07 14:29:13 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.resultType) {
|
|
|
|
qs.response_type = additionalFields.resultType as string;
|
2020-06-07 14:29:13 -07:00
|
|
|
}
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.until) {
|
|
|
|
qs.until = additionalFields.until as string;
|
|
|
|
}
|
2020-09-17 14:02:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.lang) {
|
|
|
|
qs.lang = additionalFields.lang as string;
|
|
|
|
}
|
2020-09-17 14:02:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
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}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 21:16:52 -07:00
|
|
|
qs.tweet_mode = additionalFields.tweetMode || 'compat';
|
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (returnAll) {
|
|
|
|
responseData = await twitterApiRequestAllItems.call(this, 'statuses', 'GET', '/search/tweets.json', {}, qs);
|
|
|
|
} else {
|
|
|
|
qs.count = this.getNodeParameter('limit', 0) as number;
|
|
|
|
responseData = await twitterApiRequest.call(this, 'GET', '/search/tweets.json', {}, qs);
|
|
|
|
responseData = responseData.statuses;
|
|
|
|
}
|
2020-09-17 14:02:23 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//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) as IDataObject;
|
2020-09-17 14:02:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
const qs: IDataObject = {
|
|
|
|
id: tweetId,
|
|
|
|
};
|
2020-09-17 14:02:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
if (additionalFields.includeEntities) {
|
|
|
|
qs.include_entities = additionalFields.includeEntities as boolean;
|
|
|
|
}
|
2020-09-17 14:02:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await twitterApiRequest.call(this, 'POST', '/favorites/create.json', {}, qs);
|
2020-09-17 14:02:23 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
//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) as IDataObject;
|
|
|
|
|
|
|
|
const qs: IDataObject = {
|
|
|
|
id: tweetId,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (additionalFields.trimUser) {
|
|
|
|
qs.trim_user = additionalFields.trimUser as boolean;
|
|
|
|
}
|
2020-09-17 14:02:23 -07:00
|
|
|
|
2021-07-19 23:58:54 -07:00
|
|
|
responseData = await twitterApiRequest.call(this, 'POST', `/statuses/retweet/${tweetId}.json`, {}, qs);
|
|
|
|
}
|
2020-09-17 14:02:23 -07:00
|
|
|
}
|
2021-07-19 23:58:54 -07:00
|
|
|
if (Array.isArray(responseData)) {
|
|
|
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
|
|
|
} else if (responseData !== undefined) {
|
|
|
|
returnData.push(responseData as IDataObject);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
if (this.continueOnFail()) {
|
|
|
|
returnData.push({ error: error.message });
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
throw error;
|
2020-06-01 17:42:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return [this.helpers.returnJsonArray(returnData)];
|
|
|
|
}
|
|
|
|
}
|