DEV Community

Evan Lin
Evan Lin

Posted on • Originally published at evanlin.com on

LINE OA Travel Assistant Chatbot (3): Integrating "Business Card Helper" and "Receipt Helper"

image-20240418150533579

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:

This article will focus on the following parts:

Article List:

Code List:

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.

image-20240418144300796

Business Card Helper Import:

  • Open the function.go file 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

image-20240418144648316

  • 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"`
}

Enter fullscreen mode Exit fullscreen mode

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>

Enter fullscreen mode Exit fullscreen mode

That is to say, the data is stored in a way similar to the following:

image-20240418145754861

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)
                }

Enter fullscreen mode Exit fullscreen mode

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)
                }

Enter fullscreen mode Exit fullscreen mode

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

image-20240418141427354

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.go file 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:

Top comments (0)