Preface:
This is the final article for the BUILD WITH AI (BWAI) WORKSHOP, which is a collaboration with the Google Developer Group on 04/18. After all, the workshop is in the evening (the slides can be done on the spot, but the article can't be written on the spot XD).
Please remember, if you want to know the following related knowledge:
- How to apply for a LINE Developer account, and how to create a LINE OA (ChatBot) Channel? (Example: Image Recognition LINEBot)
- How to use Google Cloud Functions to operate Firebase Realtime Database using Google Credential (Example: Chatbot with Long Memory)
This article will focus on the following parts:
- Rewriting the two example programs Business Card Helper (old version using Notion) and Receipt Helper (Python old version) into Golang.
- Sharing some things to pay attention to when using Golang to operate the Firebase Realtime Database.
- Finally, sharing some experiences and future improvement space for Gemini-Vision.
Article List:
- [BwAI workshop][Golang] LINE OA + CloudFunction + GeminiPro + Firebase = Travel Helper LINE Chatbot (1): Scenery Recognition Helper
- [BwAI workshop][Golang] LINE OA + CloudFunction + GeminiPro + Firebase = Travel Helper LINE Chatbot (2): Firebase Database gives LINEBot a super long memory
Code List:
- Business Card Helper (old version using Golang + Notion)
- Receipt Helper (Python old version)
- Image Recognition LINEBot
- Chatbot with Long Memory
- Business Card Helper (New version: Golang + Firebase RealtimeDB + Cloud Functions)
- Receipt Helper (New version: Python -> Golang + Firebase DB + Cloud Functions)
Preparation
- LINE Developer Account: You only need a LINE account to apply for a developer account.
- Google Cloud Functions: The deployment platform for Go code, generating the webhook address for LINEBot.
- Firebase: Create a Realtime database, LINE Bot can remember your previous conversations, and even answer many interesting questions.
- Google AI Studio: You can get the Gemini Key here.
Make sure you already have the environment from the first two articles, and that the Cloud Functions has already connected to LINE Bot.
Business Card Helper Import:
- Open the
function.gofile in the code: https://github.com/kkdai/linebot-cf-namecard. - Copy it to the environment that has already been set up in Cloud Functions.
- Remember to create a new set in Firebase Realtime Database -
namecard
- Deploy (Ta-da)
Business Card Helper Code Modification
First, after changing the DB from Notion to Firebase Realtime Database. First, you need to define the relevant data structure.
// Person defines the structure of the JSON data
type Person struct {
Name string `json:"name"`
Title string `json:"title"`
Address string `json:"address"`
Email string `json:"email"`
Phone string `json:"phone"`
Company string `json:"company"`
}
For the data writing part, we use Push in Firebase Database. The relevant official instructions are as follows:
Add to a list of data in the database. Every time you push a new node onto a list, your database generates a unique key, like messages/users/<unique-user-id>/<username>
That is to say, the data is stored in a way similar to the following:
Before storing, it's very simple, no additional data conversion is needed. Just Push it directly, and a unique key value will be added in front.
const DBCardPath = "namecard"
.....
// Insert the person data into firebase
userPath := fmt.Sprintf("%s/%s", DBCardPath, uID)
_, err = fireDB.NewRef(userPath).Push(ctx, person)
if err != nil {
log.Println("Error inserting data into firebase:", err)
}
But when retrieving it earlier, it will be more complicated. Because you need to grab the entire JSON and process it. The following changes are needed.
// Load all cards from firebase
var People map[string]Person
err = fireDB.NewRef(userPath).Get(ctx, &People)
if err != nil {
fmt.Println("load memory failed, ", err)
}
// Marshall data to JSON
jsonData, err := json.Marshal(People)
if err != nil {
fmt.Println("Error marshalling data to JSON:", err)
}
By using a string key map var People map[string]Person to handle this data format, and then convert it directly to JSON. This way, you can grab the data completely and turn it into a way that Gemini can read and process.
Results after improving the Business Card Helper
Code: Business Card Helper (Go Cloud Functions version): https://github.com/kkdai/linebot-cf-namecard
Here are some main modifications and optimizations:
- Because of using Firebase Database, the entire JSON is thrown to LLM, making it smarter.
Travel Helper Import
- Open the
function.gofile in the code: https://github.com/kkdai/linebot-cf-receipt. - Copy it to the environment that has already been set up in Cloud Functions.
- Create a Firebase Database set:
receipt - Deploy (Ta-da)
Related Modifications
-
Summary:
Complete Source Code
You can find the relevant open source code here:
- Business Card Helper (Go Cloud Functions version): https://github.com/kkdai/linebot-cf-namecard
- Receipt Helper (Go Cloud Functions version): https://github.com/kkdai/linebot-cf-receipt





Top comments (0)