forked from kkdai/LINE-Bot-ChatSummarizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
152 lines (129 loc) · 4.7 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
"github.com/line/line-bot-sdk-go/v7/linebot"
gpt3 "github.com/sashabaranov/go-gpt3"
)
var bot *linebot.Client
var client *gpt3.Client
var summaryQueue GroupStorage
func main() {
var err error
summaryQueue = make(GroupStorage)
bot, err = linebot.New(os.Getenv("ChannelSecret"), os.Getenv("ChannelAccessToken"))
log.Println("Bot:", bot, " err:", err)
port := os.Getenv("PORT")
apiKey := os.Getenv("ChatGptToken")
if apiKey != "" {
client = gpt3.NewClient(apiKey)
}
http.HandleFunc("/callback", callbackHandler)
addr := fmt.Sprintf(":%s", port)
http.ListenAndServe(addr, nil)
}
func callbackHandler(w http.ResponseWriter, r *http.Request) {
events, err := bot.ParseRequest(r)
if err != nil {
if err == linebot.ErrInvalidSignature {
w.WriteHeader(400)
} else {
w.WriteHeader(500)
}
return
}
for _, event := range events {
if event.Type == linebot.EventTypeMessage {
switch message := event.Message.(type) {
// Handle only on text message
case *linebot.TextMessage:
// 預設訊息
reply := "msg ID:" + message.ID + ":" + "Get:" + message.Text + " , \n OK!"
// 如果聊天機器人在群組中,開始儲存訊息。
if event.Source.GroupID != "" {
// 先取得使用者 Display Name (也就是顯示的名稱)
userName := event.Source.UserID
userProfile, err := bot.GetProfile(event.Source.UserID).Do()
if err == nil {
userName = userProfile.DisplayName
}
// event.Source.GroupID 就是聊天群組的 ID,並且透過聊天群組的 ID 來放入 Map 之中。
q := summaryQueue[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)
}
// Directly to ChatGPT
if strings.Contains(message.Text, ":gpt") {
reply = CompleteContext(message.Text)
if _, err = bot.ReplyMessage(event.ReplyToken, linebot.NewTextMessage(reply)).Do(); err != nil {
log.Print(err)
}
} else if strings.EqualFold(message.Text, ":list_all") {
q := summaryQueue[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"))
}
if _, err = bot.ReplyMessage(event.ReplyToken, linebot.NewTextMessage(reply)).Do(); err != nil {
log.Print(err)
}
} else if strings.EqualFold(message.Text, ":sum_all") {
// 把聊天群組裡面的訊息都捲出來(依照先後順序)
oriContext := ""
q := summaryQueue[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"))
}
// 取得使用者暱稱
userName := event.Source.UserID
userProfile, err := bot.GetProfile(event.Source.UserID).Do()
if err == nil {
userName = userProfile.DisplayName
}
// 訊息內先回,再來總結。
if _, err = bot.ReplyMessage(event.ReplyToken, linebot.NewTextMessage("好的,總結文字已經發給您了"+userName)).Do(); err != nil {
log.Print(err)
}
// 就是請 ChatGPT 幫你總結
oriContext = fmt.Sprintf("幫我總結 `%s`", oriContext)
reply = CompleteContext(oriContext)
// 因為 ChatGPT 可能會很慢,所以這邊後來用 SendMsg 來發送私訊給使用者。
if _, err = bot.PushMessage(event.Source.UserID, linebot.NewTextMessage(reply)).Do(); err != nil {
log.Print(err)
}
}
// Handle only on Sticker message
case *linebot.StickerMessage:
var kw string
for _, k := range message.Keywords {
kw = kw + "," + k
}
outStickerResult := fmt.Sprintf("收到貼圖訊息: %s, pkg: %s kw: %s text: %s", message.StickerID, message.PackageID, kw, message.Text)
if _, err = bot.ReplyMessage(event.ReplyToken, linebot.NewTextMessage(outStickerResult)).Do(); err != nil {
log.Print(err)
}
}
}
}
}