Skip to content

Commit

Permalink
Merge pull request #194 from snow-actions/in_reply_to_status_id
Browse files Browse the repository at this point in the history
Input: in_reply_to_status_id
  • Loading branch information
SnowCait committed May 1, 2022
2 parents 2884c6e + c1de735 commit b424721
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 28 deletions.
23 changes: 13 additions & 10 deletions README.md
Expand Up @@ -32,26 +32,29 @@ steps:
### Optional

```yml
env:
CONSUMER_API_KEY: ${{ secrets.TWITTER_CONSUMER_API_KEY }}
CONSUMER_API_SECRET_KEY: ${{ secrets.TWITTER_CONSUMER_API_SECRET_KEY }}
ACCESS_TOKEN: ${{ secrets.TWITTER_ACCESS_TOKEN }}
ACCESS_TOKEN_SECRET: ${{ secrets.TWITTER_ACCESS_TOKEN_SECRET }}
steps:
- uses: actions/checkout@v3
- name: Tweet
id: tweet
- name: Tweet summary
id: summary
uses: snow-actions/tweet@v1.1.3
env:
CONSUMER_API_KEY: ${{ secrets.TWITTER_CONSUMER_API_KEY }}
CONSUMER_API_SECRET_KEY: ${{ secrets.TWITTER_CONSUMER_API_SECRET_KEY }}
ACCESS_TOKEN: ${{ secrets.TWITTER_ACCESS_TOKEN }}
ACCESS_TOKEN_SECRET: ${{ secrets.TWITTER_ACCESS_TOKEN_SECRET }}
with:
status: |
Released ${{ github.event.release.name }}
${{ github.event.release.html_url }}
media_paths: |
1st.png
2nd.png
- run: echo "${TWEET_ID}"
env:
TWEET_ID: ${{ fromJSON(steps.tweet.outputs.response).id_str }}
- name: Tweet details
uses: snow-actions/tweet@v1.1.3
with:
status: |
Additional information
in_reply_to_status_id: ${{ fromJSON(steps.summary.outputs.response).id_str }}
```

## Environments
Expand Down
6 changes: 4 additions & 2 deletions __tests__/main.test.ts
Expand Up @@ -3,8 +3,10 @@ import * as process from 'process'
import * as cp from 'child_process'
import * as path from 'path'

test('tweet timestamp', async () => {
const response = await tweet(Date.now().toString())
test('tweet', async () => {
const text = Date.now().toString()
const response = await tweet(text)
await tweet(`in reply to ${text}`, [], JSON.parse(response).id_str)
})

// shows how the runner will run a javascript action with env / stdout protocol
Expand Down
4 changes: 4 additions & 0 deletions action.yml
Expand Up @@ -12,6 +12,10 @@ inputs:
* Image 5MB
* GIF 15MB
* Video 15MB
required: false
in_reply_to_status_id:
description: The ID of an existing status that the update is in reply to.
required: false
outputs:
response:
description: 'Response JSON'
Expand Down
9 changes: 8 additions & 1 deletion src/main.ts
Expand Up @@ -13,7 +13,14 @@ async function run(): Promise<void> {
.map(mediaPath => path.join(process.cwd(), mediaPath))
)
core.debug(`Media IDs: ${mediaIds.join(', ')}`)
const response = await tweet(core.getInput('status'), mediaIds)

const inReplyToStatusId = core.getInput('in_reply_to_status_id')

const response = await tweet(
core.getInput('status'),
mediaIds,
inReplyToStatusId
)
core.setOutput('response', response)
} catch (error) {
if (error instanceof Error) core.setFailed(error.message)
Expand Down
24 changes: 9 additions & 15 deletions src/tweet.ts
@@ -1,15 +1,11 @@
import Twitter from 'twitter'
import {isString} from 'util'

export async function tweet(
status: string,
mediaIds: string[] = []
mediaIds: string[] = [],
inReplyToStatusId: string = ''
): Promise<string> {
return new Promise((resolve, reject) => {
if (!isString(status)) {
throw new Error('status not a string')
}

const consumer_key = process.env.CONSUMER_API_KEY as string
const consumer_secret = process.env.CONSUMER_API_SECRET_KEY as string
const access_token_key = process.env.ACCESS_TOKEN as string
Expand All @@ -21,15 +17,13 @@ export async function tweet(
access_token_key,
access_token_secret
})
const parameters =
mediaIds.length > 0
? {
status,
media_ids: mediaIds.join(',')
}
: {
status
}
let parameters: {[key: string]: any} = {status}
if (mediaIds.length > 0) {
parameters['media_ids'] = mediaIds.join(',')
}
if (inReplyToStatusId != '') {
parameters['in_reply_to_status_id'] = inReplyToStatusId
}
client.post('statuses/update', parameters, (errors, data, response) => {
if (errors) {
reject(errors)
Expand Down

0 comments on commit b424721

Please sign in to comment.