Skip to content

Instantly share code, notes, and snippets.

@armanso
Last active April 19, 2020 18:12
Show Gist options
  • Save armanso/3931465e39d224cde2883110661d1fca to your computer and use it in GitHub Desktop.
Save armanso/3931465e39d224cde2883110661d1fca to your computer and use it in GitHub Desktop.
Index.ts
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
import axios from "axios"
import * as path from 'path'
import * as os from 'os'
import * as fs from 'fs'
import { PublishedReviews, Config } from 'app-reviews/lib/global-types';
import Reviews from 'app-reviews'
import serviceAccount from './google-services.json'
let firstTimeRunning = false
const bucketFilePath = `apps-reviews/published_reviews.json`
admin.initializeApp({
credential: admin.credential.cert(serviceAccount as admin.ServiceAccount),
storageBucket: "[project bucket name]"
});
const bucket = admin.storage().bucket()
const storePublishedReviewsList = async (reviews: PublishedReviews) => {
let tempPath = path.join(os.tmpdir(), '/temp.json');
fs.writeFileSync(tempPath, JSON.stringify(reviews), { flag: 'w' })
await bucket.upload(tempPath, {
destination: bucketFilePath
})
}
const retrivePublishedReviewsList = async (): Promise<PublishedReviews> => {
try {
let file = await bucket.file(bucketFilePath).download()
return JSON.parse(file.toString())
} catch {
// first time running
firstTimeRunning = true
return {}
}
}
const onNewMessageAvailable = async (messages: string[]) => {
if(firstTimeRunning)
return
var requets = messages.map(message => {
return axios.post(
"[slack webhook]",
message
)
})
await Promise.all(requets)
}
const config: Config = {
apps: [
{ id: "com.you.app", showAppIcon: true, publisherKey: "./api-key.json" },
{ id: "appstore-id", showAppIcon: true, regions: 'all' }
],
storePublishedReviewsList,
retrivePublishedReviewsList,
onNewMessageAvailable
}
exports.appReviews = functions.runWith({ timeoutSeconds: 5 * 60 })
.pubsub
.schedule('every 15 minutes').onRun(async context => {
await new Reviews(config).init()
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment