Skip to content

Commit

Permalink
Merge pull request #20 from kkdai/db_interfaces
Browse files Browse the repository at this point in the history
Init MemDB interfaces
  • Loading branch information
kkdai authored Jan 10, 2023
2 parents 9da3a1c + c87c766 commit a2f91b3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 10 deletions.
6 changes: 4 additions & 2 deletions data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package main

import "time"

type GroupDB interface {
ReadGroupInfo(string) GroupData
AppendGroupInfo(string, MsgDetail)
}
type MsgDetail struct {
MsgText string
UserName string
Time time.Time
}

type GroupData []MsgDetail

type GroupStorage map[string]GroupData
21 changes: 21 additions & 0 deletions dataMem.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package main

type GroupStorage map[string]GroupData

type MemDB struct {
db GroupStorage
}

func (mdb *MemDB) ReadGroupInfo(roomID string) GroupData {
return mdb.db[roomID]
}

func (mdb *MemDB) AppendGroupInfo(roomID string, m MsgDetail) {
mdb.db[roomID] = append(mdb.db[roomID], m)
}

func NewMemDB() *MemDB {
return &MemDB{
db: make(GroupStorage),
}
}
20 changes: 12 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ import (

var bot *linebot.Client
var client *gpt3.Client
var summaryQueue GroupStorage
var summaryQueue GroupDB

func main() {
var err error
summaryQueue = make(GroupStorage)
// 預設使用 Memory DB (for now)
summaryQueue = NewMemDB()

bot, err = linebot.New(os.Getenv("ChannelSecret"), os.Getenv("ChannelAccessToken"))
log.Println("Bot:", bot, " err:", err)

Expand Down Expand Up @@ -77,15 +79,17 @@ func callbackHandler(w http.ResponseWriter, r *http.Request) {
}

// event.Source.GroupID 就是聊天群組的 ID,並且透過聊天群組的 ID 來放入 Map 之中。
q := summaryQueue[event.Source.GroupID]
// q, _ := summaryQueue.ReadGroupInfo(event.Source.GroupID)
m := MsgDetail{
MsgText: message.Text,
UserName: userName,
Time: time.Now(),
}
log.Println("Save msg:", m)
summaryQueue[event.Source.GroupID] = append(q, m)
log.Println("All msg:", q)
summaryQueue.AppendGroupInfo(event.Source.GroupID, m)

// log.Println("Save msg:", m)
// summaryQueue[event.Source.GroupID] = append(q, m)
// log.Println("All msg:", q)
}

// Directly to ChatGPT
Expand All @@ -96,7 +100,7 @@ func callbackHandler(w http.ResponseWriter, r *http.Request) {
log.Print(err)
}
} else if strings.EqualFold(message.Text, ":list_all") {
q := summaryQueue[event.Source.GroupID]
q := summaryQueue.ReadGroupInfo(event.Source.GroupID)
for _, m := range q {
reply = reply + fmt.Sprintf("[%s]: %s . %s\n", m.UserName, m.MsgText, m.Time.Local().UTC().Format("2006-01-02 15:04:05"))
}
Expand All @@ -107,7 +111,7 @@ func callbackHandler(w http.ResponseWriter, r *http.Request) {
} else if strings.EqualFold(message.Text, ":sum_all") {
// 把聊天群組裡面的訊息都捲出來(依照先後順序)
oriContext := ""
q := summaryQueue[event.Source.GroupID]
q := summaryQueue.ReadGroupInfo(event.Source.GroupID)
for _, m := range q {
// [xxx]: 他講了什麼... 時間
oriContext = oriContext + fmt.Sprintf("[%s]: %s . %s\n", m.UserName, m.MsgText, m.Time.Local().UTC().Format("2006-01-02 15:04:05"))
Expand Down

0 comments on commit a2f91b3

Please sign in to comment.