Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
アプリ内課金の発生時にSlack通知する
import * as functions from "firebase-functions";
import * as request from "request";
exports.sendSlackMessageOnPurchaseCoin = functions
.region("asia-northeast1")
.analytics.event("in_app_purchase_coins")
.onLog((event) => sentMessageToSlack(event, "コインが購入されました"));
exports.sendSlackMessageOnPurchaseSubscription = functions
.region("asia-northeast1")
.analytics.event("in_app_purchase_subscription")
.onLog((event) => sentMessageToSlack(event, "サブスクが購入されました"));
const sentMessageToSlack = (
event: functions.analytics.AnalyticsEvent,
message: string
): request.Request => {
const url = functions.config().slack.url;
const user = event.user;
const purchaseValue = event.valueInUSD; // Amount of the purchase in USD.
const userLanguage = user?.deviceInfo.userDefaultLanguage; // The user language in language-country format.
const screenName = event.params["firebase_screen_class"];
return request.post(url, {
json: {
text:
`${message}💰 $${purchaseValue} \n` +
"```\n" +
`at time: ${event.logTime} \n` +
`lang: ${userLanguage} \n` +
`country: ${event.user?.geoInfo.country} \n` +
`model: ${event.user?.deviceInfo?.mobileModelName} \n` +
`appId: ${event.user?.appInfo?.appId} \n` +
`appVersion: ${event.user?.appInfo?.appVersion} \n` +
`screen: ${screenName} \n` +
"```\n",
},
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment