Skip to content

Commit

Permalink
Add: giving a sheet system
Browse files Browse the repository at this point in the history
  • Loading branch information
takara2314 committed Jan 9, 2022
1 parent 687ba8b commit d1a1d3e
Show file tree
Hide file tree
Showing 8 changed files with 681 additions and 10 deletions.
24 changes: 20 additions & 4 deletions common/init.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
package common

import (
"context"
"io/ioutil"
"log"
"time"

vision "cloud.google.com/go/vision/apiv1"
"github.com/line/line-bot-sdk-go/linebot"
"gopkg.in/yaml.v3"
)

const (
Version = "4Q"
Version = "4Q"
ServiceURL = "https://downgraded-drkanji.appspot.com"
)

var (
Bot *linebot.Client
Quizzes QuizzesYaml
Bot *linebot.Client
VisionAPICtx context.Context
VisionAPI *vision.ImageAnnotatorClient

Quizzes QuizzesYaml
AntonymFormat []byte
HomonymFormat []byte
SynonymFormat []byte
Expand Down Expand Up @@ -69,18 +74,20 @@ func init() {
}
time.Local = loc

// Load a quiz file.
file, err := ioutil.ReadFile("./quizzes.yaml")
if err != nil {
log.Println(err)
panic(err)
}

// File data to a instance.
err = yaml.Unmarshal(file, &Quizzes)
if err != nil {
log.Println(err)
panic(err)
}

// Load quiz templates.
AntonymFormat, err = ioutil.ReadFile("./templates/Antonym.json")
if err != nil {
log.Println(err)
Expand Down Expand Up @@ -116,4 +123,13 @@ func init() {
log.Println(err)
panic(err)
}

// Load and authorize Google Vision API
VisionAPICtx = context.Background()

VisionAPI, err = vision.NewImageAnnotatorClient(VisionAPICtx)
if err != nil {
log.Println(err)
panic(err)
}
}
5 changes: 4 additions & 1 deletion common/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@ const (
MoreSynonyms = "ちなみに以下も類義語じゃぞ。"
MoreConfers = "ちなみに以下も参照できるぞ。"

DetectedText = "検出した文字"
DetectedNothing = "悪いが画像の中から、文字らしきものを見つけれないのう。"
YourAnswerCorrect = "正解じゃ!流石じゃのう。"
YourAnswerWrong = "違うのう… 答えを確認してみるんじゃ!"
UnknownMessage = "ワシは漢字の問題を出すぐらいしかできないのう… 年じゃからのう…"
UnknownMessage = "ワシは漢字の問題を出すぐらいしかできないのう… quiz と打つと問題を出すぞい。"
UnknownImage = "目が悪くてワシには見えないのう… 出した問題の答えなら見るが…"
InvalidCommand = "不正なコマンドです。"
RapidNotice = "連続投稿はやめてほしいのじゃ。やめるまで口を聞かんぞう。"
)
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ module downgraded-dr.kanji
go 1.15

require (
cloud.google.com/go/vision v1.0.0
github.com/gin-gonic/gin v1.7.1
github.com/golang/protobuf v1.5.2 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/line/line-bot-sdk-go v7.8.0+incompatible
github.com/stretchr/testify v1.6.1 // indirect
golang.org/x/sys v0.0.0-20210503080704-8803ae5d1324 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c
Expand Down
518 changes: 518 additions & 0 deletions go.sum

Large diffs are not rendered by default.

40 changes: 38 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package main

import (
"fmt"
"io"
"log"
"os"
"path/filepath"
"strconv"
"time"

"downgraded-dr.kanji/common"
"downgraded-dr.kanji/receive"
"downgraded-dr.kanji/state"
"downgraded-dr.kanji/utils"
"github.com/gin-gonic/gin"
"github.com/line/line-bot-sdk-go/linebot"
)
Expand All @@ -27,6 +31,7 @@ func main() {

router := gin.Default()
router.POST("/callback", callbackPOST)
router.GET("/sheet.png", sheetGET)

router.Run(":" + os.Getenv("PORT"))
}
Expand Down Expand Up @@ -95,9 +100,40 @@ func callbackPOST(c *gin.Context) {
receive.TextMessage(event, message.Text)

case *linebot.ImageMessage:
fmt.Printf("[IMAGE] %s %s\n", profile.DisplayName, message.OriginalContentURL)
receive.ImageMessage(event, message.OriginalContentURL)
// Create a temporality file.
filepath := filepath.Join(
os.TempDir(),
strconv.Itoa(utils.RandN(100000))+".png",
)
file, err := os.Create(filepath)
if err != nil {
log.Println(err)
panic(err)
}
defer file.Close()

// Get a image content.
content, err := common.Bot.GetMessageContent(message.ID).Do()
if err != nil {
log.Println(err)
panic(err)
}
defer content.Content.Close()

// Write the bytes to the field.
_, err = io.Copy(file, content.Content)
if err != nil {
log.Println(err)
panic(err)
}

fmt.Printf("[IMAGE] %s\n", profile.DisplayName)
receive.ImageMessage(event, filepath)
}
}
}
}

func sheetGET(c *gin.Context) {
c.File("./sheet.png")
}
88 changes: 86 additions & 2 deletions receive/imageMessage.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,101 @@
package receive

import (
"fmt"
"log"
"os"
"strings"

vision "cloud.google.com/go/vision/apiv1"
"downgraded-dr.kanji/common"
"downgraded-dr.kanji/state"
"downgraded-dr.kanji/utils"

"github.com/line/line-bot-sdk-go/linebot"
)

func ImageMessage(event *linebot.Event, message string) {
func ImageMessage(event *linebot.Event, filepath string) {
// May receive a quiz answer.
if state.States[event.Source.UserID].IsQuizzing {
state.States[event.Source.UserID].IsQuizzing = false

// Load a image file.
file, _ := os.Open(filepath)
defer file.Close()
image, _ := vision.NewImageFromReader(file)

// Detect texts from image.
texts, err := common.VisionAPI.DetectTexts(
common.VisionAPICtx,
image,
nil,
10,
)
if err != nil {
log.Println(err)
panic(err)
}

detectedText := texts[0].Description

// Trim unrelated chars
detectedText = strings.Replace(detectedText, " ", "", -1)
detectedText = strings.Replace(detectedText, " ", "", -1)
detectedText = strings.Replace(detectedText, "\n", "", -1)

fmt.Println("[TEXT DETECTED]", detectedText)

// Detected nothing
if detectedText == "" {
_, err := common.Bot.ReplyMessage(
event.ReplyToken,
linebot.NewTextMessage(common.DetectedNothing),
).Do()
if err != nil {
log.Println(err)
panic(err)
}
}

// It is the correct answer if contains $~.LastQuiz.Corrects.
if utils.StringSliceFind(
state.States[event.Source.UserID].LastQuiz.Corrects,
detectedText,
) != -1 {
// Correct answered.
_, err := common.Bot.ReplyMessage(
event.ReplyToken,
linebot.NewTextMessage(
fmt.Sprintf("%s: %s", common.DetectedText, detectedText),
),
linebot.NewTextMessage(common.YourAnswerCorrect),
).Do()
if err != nil {
log.Println(err)
panic(err)
}

} else {
// Wrong answered.
_, err := common.Bot.ReplyMessage(
event.ReplyToken,
linebot.NewTextMessage(
fmt.Sprintf("%s: %s", common.DetectedText, detectedText),
),
linebot.NewTextMessage(common.YourAnswerWrong),
).Do()
if err != nil {
log.Println(err)
panic(err)
}
}

return
}

_, err := common.Bot.ReplyMessage(
event.ReplyToken,
linebot.NewTextMessage("?"),
linebot.NewTextMessage(common.UnknownImage),
).Do()
if err != nil {
log.Println(err)
Expand Down
14 changes: 14 additions & 0 deletions receive/textMessage.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ func TextMessage(event *linebot.Event, message string) {
panic(err)
}

} else if message == "sheet" {
// Receive a text "sheet"
_, err := common.Bot.ReplyMessage(
event.ReplyToken,
linebot.NewImageMessage(
common.ServiceURL+"/sheet.png",
common.ServiceURL+"/sheet.png",
),
).Do()
if err != nil {
log.Println(err)
panic(err)
}

} else {
// Receive a else text of above.

Expand Down
Binary file added sheet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d1a1d3e

Please sign in to comment.