Skip to content

Commit

Permalink
Merge pull request #39 from ichisuke55/fix-image-context
Browse files Browse the repository at this point in the history
fix: skip image only message
  • Loading branch information
ichisuke55 authored May 20, 2024
2 parents 8dc6814 + f2c045c commit bd4fa7c
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 30 deletions.
22 changes: 22 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: lint
on:
pull_request:
paths:
- "**.go"
- .github/workflows/lint.yml

jobs:
golangci-lint:
name: golangci-lint
runs-on: ubuntu-latest
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v4

- name: Run golangci-lint
uses: reviewdog/action-golangci-lint@v2
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
golangci_lint_flags: "--config=./.golangci.yml ./..."
fail_on_error: true
reporter: "github-pr-review"
21 changes: 21 additions & 0 deletions .golanci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
linters-settings:
govet:
check-shadowing: false
gocyclo:
min-complexity: 30
misspell:
locale: US

linters:
disable-all: true
enable:
- goimports
- errcheck
- gocognit
- gocritic
- gocyclo
- gofmt
- govet
- misspell
- staticcheck
- whitespace
72 changes: 42 additions & 30 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"log"
"os"
"reflect"
"regexp"
"unicode"

Expand Down Expand Up @@ -93,7 +94,7 @@ func main() {
if err != nil {
log.Fatal("cannot initialize logger")
}
defer logger.Sync()
defer logger.Sync() //nolint:errcheck

// Set Slack API client
client := slack.New(
Expand Down Expand Up @@ -123,7 +124,6 @@ func main() {
eventAPIEvent, ok := event.Data.(slackevents.EventsAPIEvent)
if !ok {
logger.Info("Ignored event", zap.Any("event", event))
continue
}
// Prevent from sending duplicate message
socketClient.Ack(*event.Request)
Expand All @@ -132,41 +132,53 @@ func main() {
case slackevents.CallbackEvent:
switch evt := eventAPIEvent.InnerEvent.Data.(type) {
case *slackevents.MessageEvent:
if evt.BotID == "" {
logger.Debug("original text", zap.String("message", evt.Text))
// if URL contains in message, trancate it
message, err := trancateText(evt.Text)
// skip bot message
if evt.BotID != "" {
logger.Debug("this message sent by bot.")
continue
}

logger.Debug("original text", zap.String("message", evt.Text))
logger.Debug("event message information", zap.Any("type", reflect.TypeOf(evt)), zap.Any("info", evt))

// skip image only, or delete image message
if evt.Text == "" {
logger.Debug("skip translate message, because of image only.")
continue
}

// if URL contains in message, trancate it
message, err := trancateText(evt.Text)
if err != nil {
logger.Error("failed to trancate text", zap.Error(err))
}

// if only english in message
match := isJapanese(message)
if match {
// translate text via GoogleTranslate API
translatedMessage, err := translateText(conf.ProjectID, "ja-jp", "en-us", message)
if err != nil {
logger.Error("failed to trancate text", zap.Error(err))
return
logger.Error("failed to translate", zap.Error(err))
}
// if only english in message
match := isJapanese(message)
if match {
// translate text via GoogleTranslate API
translatedMessage, err := translateText(conf.ProjectID, "ja-jp", "en-us", message)
logger.Info("translated result", zap.String("originalText", message), zap.String("translatedText", translatedMessage))
if err != nil {
logger.Error("failed to translate", zap.Error(err))
return
}
// post slack message
_, _, err = client.PostMessage(
evt.Channel,
slack.MsgOptionText(translatedMessage, false),
)
if err != nil {
logger.Error("failed to post slack message", zap.Error(err))
return
}

logger.Info("translate success!", zap.String("originalText", message), zap.String("translatedText", translatedMessage))

// post slack message
_, _, err = client.PostMessage(
evt.Channel,
slack.MsgOptionText(translatedMessage, false),
)
if err != nil {
logger.Error("failed to post slack message", zap.Error(err))
}
}
}

}

}
}
}()
socketClient.Run()
if err = socketClient.Run(); err != nil {
logger.Error("failed to reconnect to slack", zap.Error(err))
}
}

0 comments on commit bd4fa7c

Please sign in to comment.