4. Publish a Twitter bot

A constant stream of wisdom is promised to all the followers of @CodeQuotesBot 😉

I have wanted to create a bot for quite a while, but wasn't sure whether to make a Discord or Twitter bot

I decided on Twitter, as it was more in the spirit of "building in the open" as Discord is so often private/gated communities

As I wanted to publish a quote roughly about once a minute, I knew I would quickly run out of quotes so I decided to integrate two API's in the cloud function for this project

I haven't personally looked through every single quote of these two API's - so it's a little annoying I can't veto any/check their quality

I checked the first 5 or so and they all seemed high quality/not problematic - but will keep an eye on them

Glad I was able to code this up, and ship it super quickly, and learned a lot about triggering cloud functions!

Firebase's error messages are truly pathetic and I spent ages debugging why an error was occurring, see if you can spot what I missed in the below cloud function

			
exports.scheduledFunctionCrontab = functions.pubsub.schedule('* * * * *').onRun(context => {
	const { refreshToken } = (await dbRef.get()).data()
			
	const {
		client: refreshedClient,
		accessToken,
		refreshToken: newRefreshToken,
	} = await twitterClient.refreshOAuth2Token(refreshToken)
			
	await dbRef.set({ accessToken, refreshToken: newRefreshToken })
			
	if (Math.round(Math.random())) {
		axios.get('https://programming-quotes-api.herokuapp.com/Quotes/random')
		.then(async axiosResp => {
			const { data } = await refreshedClient.v2.tweet(
				`${axiosResp.data.en} - ${axiosResp.data.author}`
			)
			return null
		})
	} else {
		axios.get('http://quotes.stormconsultancy.co.uk/random.json')
		.then(async axiosResp => {
			const { data } = await refreshedClient.v2.tweet(
				`${axiosResp.data.quote} - ${axiosResp.data.author}`
			)
			return null
		})
	}
	
		return null
	})
			
		
Check your answer The top function onRun(context => { uses an await - and needs to have async appended

None of the Firebase error messages were helpful in finding this error!

If you are reading this after the time I wrote this article (July 2022) I may turn off the bot from posting (or reduce its frequency)

I will end up paying for using these cloud functions soon - and now I have successfully built, tested and deployed everything - I don't feel the need to support it long term

Onto project five ✔️