-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
127 lines (110 loc) · 3.67 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
// 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"
"io"
"log"
"net/http"
"net/url"
"os"
"strings"
"github.com/line/line-bot-sdk-go/v7/linebot"
)
var ImgSrv string
var bot *linebot.Client
// PetDB :
var PetDB *Pets
func main() {
var err error
ImgSrv = os.Getenv("IMG_SRV")
PetDB = NewPets()
bot, err = linebot.New(os.Getenv("ChannelSecret"), os.Getenv("ChannelAccessToken"))
log.Println("Bot:", bot, " err:", err)
http.HandleFunc("/callback", callbackHandler)
port := os.Getenv("PORT")
addr := fmt.Sprintf(":%s", port)
http.ListenAndServe(addr, nil)
}
func getSecureImageAddress(oriAdd string) string {
eURL := url.QueryEscape(oriAdd)
imgGetUrl := fmt.Sprintf("%surl?%s", ImgSrv, eURL)
log.Println("eURL:", eURL, " url:", imgGetUrl, " ImgApi:", ImgSrv)
response, err := http.Get(imgGetUrl)
if err != nil {
log.Println("Error while downloading:", err)
return ""
}
defer response.Body.Close()
if response.StatusCode != 200 {
return ""
}
totalBody, err := io.ReadAll(response.Body)
if err != nil {
log.Println("Error while parsing:", err)
return ""
}
log.Println("Got data:", string(totalBody))
return fmt.Sprintf("%simgs?%s.jpg", ImgSrv, string(totalBody))
}
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 {
switch event.Type {
case linebot.EventTypeUnsend:
log.Println("Unsend")
target := ""
if event.Source.GroupID != "" {
target = event.Source.GroupID
} else {
target = event.Source.RoomID
}
if _, err = bot.PushMessage(target, linebot.NewTextMessage("不要害羞地回收訊息,趕快打狗或是貓來看流浪動物。")).Do(); err != nil {
log.Print(err)
}
case linebot.EventTypeMessage:
switch message := event.Message.(type) {
case *linebot.TextMessage:
var pet *Pet
var sendr *linebot.Sender
log.Println(message.Text)
inText := strings.ToLower(message.Text)
if strings.Contains(inText, "狗") || strings.Contains(inText, "dog") {
pet = PetDB.GetNextDog()
sendr = linebot.NewSender("Brown", "https://stickershop.line-scdn.net/stickershop/v1/sticker/52002749/iPhone/sticker_key@2x.png")
} else if strings.Contains(inText, "貓") || strings.Contains(inText, "cat") {
pet = PetDB.GetNextCat()
sendr = linebot.NewSender("Sally", "https://stickershop.line-scdn.net/stickershop/v1/sticker/52002736/iPhone/sticker_key@2x.png")
}
if pet == nil {
pet = PetDB.GetNextPet()
}
out := pet.DisplayPet()
if len(pet.ImageName) > 0 {
if _, err = bot.ReplyMessage(event.ReplyToken, linebot.NewTextMessage(out).WithSender(sendr).AddEmoji(linebot.NewEmoji(1, "5ac1bfd5040ab15980c9b435", "086")), linebot.NewImageMessage(pet.ImageName, pet.ImageName).WithSender(sendr)).Do(); err != nil {
log.Print(err)
}
} else if _, err = bot.ReplyMessage(event.ReplyToken, linebot.NewTextMessage(out).WithSender(sendr).AddEmoji(linebot.NewEmoji(1, "5ac1bfd5040ab15980c9b435", "086"))).Do(); err != nil {
log.Print(err)
}
}
}
}
}