|
async function getDailyQuote(config: any) { |
|
const pref_mode = (config.mode) ? config.mode : 'random' // Available modes: [today, random, author (premium only)]. |
|
// For premium subscriptions |
|
const pref_author = (config.author) ? config.author : null // Available authors: https://premium.zenquotes.io/available-authors/ |
|
const pref_key = (config.key) ? config.key : null // https://premium.zenquotes.io/ |
|
|
|
const zenQuotesAPI = `https://zenquotes.io/api/` |
|
const getDailyQuoteURL = (pref_mode == 'author' && pref_author && pref_key) ? `${zenQuotesAPI}quotes/${pref_mode}/${pref_author}/${pref_key}` : `${zenQuotesAPI}${pref_mode}` |
|
const response = await fetch(getDailyQuoteURL) |
|
if (response != null) { |
|
const data = await response.json() |
|
return data |
|
} else { |
|
return 'Error in zenquotes.io Daily Quote lookup.' |
|
} |
|
} |
|
|
|
async function getDailyImage() { |
|
const query = 'nature' |
|
const pexelsAPI = 'https://api.pexels.com/v1' |
|
const pexelsAPIKey = '<secret>' |
|
const randomPage = Math.floor(Math.random() * (100 - 1) + 1) |
|
const getDailyImageURL = `${pexelsAPI}/search?query=${query}&per_page=80&page=${randomPage}` |
|
const response = await fetch(getDailyImageURL, { |
|
method: 'GET', |
|
headers: { |
|
'Authorization': `Bearer ${pexelsAPIKey}` |
|
} |
|
}) |
|
if (response != null) { |
|
const data = await response.json() |
|
return data |
|
} else { |
|
return 'Error in Pexels image lookup.' |
|
} |
|
} |
|
|
|
async function main() { |
|
let quoteParams = { |
|
'mode': 'random', |
|
'author': null, |
|
'key': null |
|
} |
|
const dailyQuote = await getDailyQuote(quoteParams) |
|
const dailyImage = await getDailyImage() |
|
const webhookURL = '<secret>' |
|
const colors = [ |
|
'16670074', |
|
'16768298', |
|
'8280063', |
|
'4968442', |
|
'714881' |
|
] |
|
|
|
return await fetch(webhookURL, { |
|
method: 'POST', |
|
headers: { |
|
'Content-Type': 'application/json' |
|
}, |
|
body: JSON.stringify({embeds: [{ |
|
title: dailyQuote[0].a, |
|
image: { |
|
url: dailyImage.photos[Math.floor(Math.random() * (79 - 1) + 1)].src.large |
|
}, |
|
author: { |
|
name: '💭 Daily Quote', |
|
}, |
|
color: colors[Math.floor(Math.random() * colors.length)], |
|
description: dailyQuote[0].q, |
|
}]}), |
|
}) |
|
} |
|
|
|
export default main() |