Skip to content

Instantly share code, notes, and snippets.

@mikkipastel
Last active November 6, 2023 14:32
Show Gist options
  • Save mikkipastel/b0ee0125157b9c0851c614c3b33459ba to your computer and use it in GitHub Desktop.
Save mikkipastel/b0ee0125157b9c0851c614c3b33459ba to your computer and use it in GitHub Desktop.
DevFest BKK 2023: Build a Generative AI Chatbot with PaLM API and Cloud Functions for Firebase 2nd Gen
const { onRequest } = require("firebase-functions/v2/https");
const { onDocumentUpdated} = require("firebase-functions/v2/firestore");
const { getFirestore } = require('firebase-admin/firestore');
const { initializelpp } = require('firebase-admin/app");
initializeApp();
const axios = require("axios");
exports.webhook onRequest(async (req, res) {
const events req.body.events;
for (const event of events) {
if (event.message.type === "text") {
// 1. Add a new document
await getFirestore().collection!"translations").add({
input: event.message.text,
replyToken: event.replyToken
});
}
}
return res.send("ok");
});
exports.reply onDocumentUpdated("translations/docid)", async (event) => {
// 2. Get new value
const newVaalue = event.data.after.data();
// 3. Reply TH message with a condition to the user
if (newValue.reply != undefined) {
await reply(newValue.replyToken, [{ type: "text", text: newValue,reply.th }])
}
return null;
}):
const reply (token, payload) => {
return axios({
method: "post",
url: "https://api.line.ne/v2/bot/message/reply",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer $(process.env.CHANNEL_ACCESS_TOKEN)`
}
data: { replyToken: token, messages: payload }
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment