Skip to content

Commit

Permalink
Fix Twitter integration
Browse files Browse the repository at this point in the history
  • Loading branch information
glacials committed Apr 19, 2024
1 parent c60583d commit a4e94f8
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 2,620 deletions.
20 changes: 15 additions & 5 deletions firebase/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ To test locally, you need `firebase-tools` installed.

```sh
npm install -g firebase-tools
firebase functions:shell
```

To actually test tweeting and tooting,
Expand All @@ -35,7 +34,14 @@ mastodonbot (collection)
deleteme (document)
```

In the new Firebase shell:
Open a Firebase shell:

```sh
firebase use whats-in-standard-beta # Or your non-production project name
firebase functions:shell # Note that changes to .env require rerunning this
```

and invoke the function:

```sh
detectRotations()
Expand All @@ -50,10 +56,14 @@ call.
### Deploying

```sh
npm --prefix functions run build
firebase use whats-in-standard # Or your production project name
mv functions/.env ~/.env.firebase.tmp

# Make sure the following command prints "Now using project whats-in-standard"
firebase use whats-in-standard

firebase deploy
firebase use whats-in-standard-beta # Or your non-production project name
firebase use whats-in-standard-beta
mv ~/.env.firebase.tmp functions/.env
```

Then revert `.firebaserc`.
Expand Down
39 changes: 26 additions & 13 deletions firebase/functions/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ import functions from "firebase-functions";
import { error } from "firebase-functions/logger";

import { login as mastodonLogin } from "masto";
import { Client as TwitterClient } from "twitter-api-sdk";
import { TwitterApi } from "twitter-api-v2";

import CardSet from "./whatsinstandard/card/CardSet.js";

const MAX_TOOT_LENGTH = 500;
const MAX_TWEET_LENGTH = 280;

/**
* Tweets any new or removed sets.
* Tweets a post on Twitter containing added or removed sets.
* If there are none,
* no tweet is sent.
*
Expand All @@ -22,26 +22,39 @@ const MAX_TWEET_LENGTH = 280;
* @returns {Promise<void>} - A promise that resolves when the tweet is sent.
*/
export async function tweet(setDifferences) {
const twitterClient = new TwitterClient(process.env.TWITTER_BEARER_TOKEN);
const twitterClient = new TwitterApi({
appKey: process.env.TWITTER_API_KEY,
appSecret: process.env.TWITTER_API_KEY_SECRET,
accessToken: process.env.TWITTER_ACCESS_TOKEN,
accessSecret: process.env.TWITTER_ACCESS_TOKEN_SECRET,
}).readWrite;
const tweet = craftPost(setDifferences, MAX_TWEET_LENGTH);
if (tweet === null) {
return;
}

functions.logger.info(`Tweeting: ${tweet}`);
const response = twitterClient.tweets.createTweet({ text: tweet });
functions.logger.info(`Twitter response:`);
functions.logger.info(response);
try {
const response = await twitterClient.v2.tweet({ text: tweet });
functions.logger.info(`Twitter response:`);
functions.logger.info(response);
} catch (error) {
functions.logger.error(error);
}
}

/**
* Toots a message on Mastodon.
* Toots a post on Mastodon containing added or removed sets.
* If there are none,
* no toot is sent.
*
* @param {{ addedSets: Set<CardSet>; removedSets: Set<CardSet> }} setDifferences - The set differences object.
* @returns {Promise<void>} A promise that resolves when the toot is sent.
* @param {Object} setDifferences - The set differences object.
* @param {Set<CardSet>} setDifferences.addedSets - The added sets.
* @param {Set<CardSet>} setDifferences.removedSets - The removed sets.
* @returns {Promise<void>} - A promise that resolves when the toot is sent.
*/
export async function toot(setDifferences) {
console.log(`MASTODON_ACCESS_TOKEN=${process.env.MASTODON_ACCESS_TOKEN}`)
console.log(`MASTODON_ACCESS_TOKEN=${process.env.MASTODON_ACCESS_TOKEN}`);
const mastodonClient = await mastodonLogin({
url: process.env.MASTODON_SERVER_URL,
accessToken: process.env.MASTODON_ACCESS_TOKEN,
Expand Down Expand Up @@ -141,13 +154,13 @@ export async function diff(collection, apiSets) {
`Unchanged sets: ${Array.from(unchangedSetNames).join(", ")}`
);


if (initOnly) {
addedSets.clear();
functions.logger.info(`Too many added sets; assuming a new environment. Skipping posts.`);
functions.logger.info(
`Too many added sets; assuming a new environment. Skipping posts.`
);
}


return {
addedSets: addedSets,
removedSets: removedSets,
Expand Down
26 changes: 9 additions & 17 deletions firebase/functions/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions firebase/functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
"main": "index.js",
"dependencies": {
"firebase-admin": "^11.11.1",
"firebase-functions": "^4.8.0",
"firebase-functions": "^4.9.0",
"masto": "^5.11.4",
"twitter-api-sdk": "^1.2.1"
"twitter-api-v2": "^1.16.1"
},
"devDependencies": {
"firebase": "^10.8.1",
Expand Down
Loading

0 comments on commit a4e94f8

Please sign in to comment.