Skip to content

Commit

Permalink
build(release): compiled action for 1.5.1
Browse files Browse the repository at this point in the history
[skip ci]
  • Loading branch information
semantic-release-bot committed Feb 7, 2020
1 parent 2f0baf7 commit e3ac7a5
Showing 1 changed file with 67 additions and 44 deletions.
111 changes: 67 additions & 44 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26784,7 +26784,7 @@ const Twitter = __webpack_require__(918);

const parseTweetFileContent = __webpack_require__(401);

async function tweet({ twitterCredentials }, tweetFile) {
function tweet({ twitterCredentials }, tweetFile) {
const client = new Twitter(twitterCredentials);

const tweet = parseTweetFileContent(tweetFile.text);
Expand All @@ -26798,68 +26798,91 @@ async function tweet({ twitterCredentials }, tweetFile) {
throw new Error(`TWITTER_ACCOUNT_ID environment variable must be set`);
}

const { card_uri } = await createPollCard(client, {
/* istanbul ignore if */
if (!process.env.TWITTER_USERNAME) {
throw new Error(`TWITTER_USERNAME environment variable must be set`);
}

return createTweetWithPollCard(client, {
name: tweetFile.filename,
text: tweet.text,
pollOptions: tweet.poll
});
return createTweet(client, { status: tweet.text, card_uri });
}

function createPollCard(
function createTweetWithPollCard(
client,
{
name,
text,
pollOptions: [first_choice, second_choice, third_choice, fourth_choice]
}
) {
return new Promise((resolve, reject) => {
client.post(
`https://ads-api.twitter.com/6/accounts/${process.env.TWITTER_ACCOUNT_ID}/cards/poll`,
// https://developer.twitter.com/en/docs/accounts-and-users/follow-search-get-users/api-reference/get-users-lookup
// We need the twitter user ID for the request to create the tweet later
client.get(
"users/lookup",
{
name,
duration_in_minutes: 1440, // two days
first_choice,
second_choice,
third_choice,
fourth_choice
screen_name: process.env.TWITTER_USERNAME
},
(error, result) => {
/* istanbul ignore if */
if (error) {
return reject(error);
}

resolve(result.data);

// result looks like this:
// {
// request: {
// params: {
// name: 'foo-bar-1',
// first_choice: 'foo',
// third_choice: 'daz',
// fourth_choice: 'daz',
// second_choice: 'bar',
// duration_in_minutes: 120
// }
// },
// data: {
// name: 'foo-bar-1',
// start_time: '2020-01-07T22:42:50Z',
// first_choice: 'foo',
// third_choice: 'daz',
// fourth_choice: 'daz',
// second_choice: 'bar',
// end_time: '2020-01-08T00:42:50Z',
// id: '8r115',
// created_at: '2020-01-07T22:42:50Z',
// duration_in_minutes: '120',
// card_uri: 'card://1214678808315351040',
// updated_at: '2020-01-07T22:42:50Z',
// deleted: false,
// card_type: 'TEXT_POLLS'
// }
// }
/* istanbul ignore if */
if (!result[0]) {
return reject(
`Twitter account for username "${process.env.TWITTER_USERNAME}" not found`
);
}

const userId = result[0].id;

// https://developer.twitter.com/en/docs/ads/creatives/api-reference/poll#post-accounts-account-id-cards-poll
client.post(
`https://ads-api.twitter.com/6/accounts/${process.env.TWITTER_ACCOUNT_ID}/cards/poll`,
{
name,
duration_in_minutes: 1440, // two days
first_choice,
second_choice,
third_choice,
fourth_choice,
text
},
(error, result) => {
/* istanbul ignore if */
if (error) {
return reject(error);
}

const card_uri = result.data.card_uri;

// https://developer.twitter.com/en/docs/ads/creatives/api-reference/tweets#post-accounts-account-id-tweet
client.post(
`https://ads-api.twitter.com/6/accounts/${process.env.TWITTER_ACCOUNT_ID}/cards/poll`,
{
as_user_id: userId,
text,
card_uri
},
(error, result) => {
/* istanbul ignore if */
if (error) {
return reject(error);
}

resolve({
text,
url: `https://twitter.com/${process.env.TWITTER_USERNAME}/status/${result.data.id_str}`
});
}
);
}
);
}
);
});
Expand All @@ -26873,7 +26896,7 @@ function createTweet(client, options) {
}

resolve({
text: tweet,
text: options.status,
url: `https://twitter.com/${result.user.screen_name}/status/${result.id_str}`
});
});
Expand Down

0 comments on commit e3ac7a5

Please sign in to comment.