mirror of
https://github.com/cjdenio/jia.git
synced 2024-11-21 23:23:39 +00:00
Create savepoint
This commit is contained in:
commit
8b6f5d360c
7 changed files with 159 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
.env
|
21
LICENSE.txt
Normal file
21
LICENSE.txt
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2020 Victor Truong
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
5
README.md
Normal file
5
README.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
# 🦉 Jia
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
[MIT License](LICENSE.txt)
|
78
cmd/jia.go
Normal file
78
cmd/jia.go
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
|
||||||
|
"github.com/ifvictr/jia/pkg/jia"
|
||||||
|
"github.com/joho/godotenv"
|
||||||
|
"github.com/slack-go/slack"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
godotenv.Load()
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
fmt.Println("Starting Jia…")
|
||||||
|
config := jia.NewConfig()
|
||||||
|
|
||||||
|
api := slack.New(config.BotToken)
|
||||||
|
rtm := api.NewRTM()
|
||||||
|
|
||||||
|
lastValidNumber := 0
|
||||||
|
var lastSender string
|
||||||
|
|
||||||
|
go rtm.ManageConnection()
|
||||||
|
|
||||||
|
for event := range rtm.IncomingEvents {
|
||||||
|
switch ev := event.Data.(type) {
|
||||||
|
case *slack.MessageEvent:
|
||||||
|
// Ignore messages that aren't in the target channel, or are non-user messages
|
||||||
|
if ev.Channel != config.ChannelId || ev.User == "USLACKBOT" || ev.User == "" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Attempt to extract a positive number from the beginning of a string
|
||||||
|
countPattern := regexp.MustCompile(`^\d+`)
|
||||||
|
matchedNumber, err := strconv.Atoi(countPattern.FindString(ev.Text))
|
||||||
|
log.Println(matchedNumber)
|
||||||
|
|
||||||
|
// Ignore messages that don't have numbers
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Failed to retrieve number, skipping…")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reject if sender also sent the previous message
|
||||||
|
if ev.User == lastSender {
|
||||||
|
api.AddReaction("bangbang", slack.ItemRef{
|
||||||
|
Channel: ev.Channel,
|
||||||
|
Timestamp: ev.Timestamp,
|
||||||
|
})
|
||||||
|
api.PostEphemeral(ev.Channel, ev.User, slack.MsgOptionText("You counted consecutively! That’s not allowed.", false))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ignore numbers that aren't in order
|
||||||
|
if matchedNumber != lastValidNumber+1 {
|
||||||
|
api.AddReaction("bangbang", slack.ItemRef{
|
||||||
|
Channel: ev.Channel,
|
||||||
|
Timestamp: ev.Timestamp,
|
||||||
|
})
|
||||||
|
api.PostEphemeral(ev.Channel, ev.User, slack.MsgOptionText("You counted incorrectly! The next valid number is "+strconv.Itoa(lastValidNumber+1), false))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
// Finally!
|
||||||
|
lastValidNumber = matchedNumber
|
||||||
|
lastSender = ev.User
|
||||||
|
api.AddReaction("+1", slack.ItemRef{
|
||||||
|
Channel: ev.Channel,
|
||||||
|
Timestamp: ev.Timestamp,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
10
go.mod
Normal file
10
go.mod
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
module github.com/ifvictr/jia
|
||||||
|
|
||||||
|
go 1.14
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/gorilla/websocket v1.4.2 // indirect
|
||||||
|
github.com/joho/godotenv v1.3.0
|
||||||
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
|
github.com/slack-go/slack v0.6.5
|
||||||
|
)
|
18
go.sum
Normal file
18
go.sum
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
|
||||||
|
github.com/gorilla/websocket v1.2.0 h1:VJtLvh6VQym50czpZzx07z/kw9EgAxI3x1ZB8taTMQQ=
|
||||||
|
github.com/gorilla/websocket v1.2.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
|
||||||
|
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
|
||||||
|
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||||
|
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
|
||||||
|
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
|
||||||
|
github.com/pkg/errors v0.8.0 h1:WdK/asTD0HN+q6hsWO3/vpuAkAr+tw6aNJNDFFf0+qw=
|
||||||
|
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
|
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||||
|
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||||
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/slack-go/slack v0.6.4 h1:cxOqFgM5RW6mdEyDqAJutFk3qiORK9oHRKi5bPqkY9o=
|
||||||
|
github.com/slack-go/slack v0.6.4/go.mod h1:sGRjv3w+ERAUMMMbldHObQPBcNSyVB7KLKYfnwUFBfw=
|
||||||
|
github.com/slack-go/slack v0.6.5 h1:IkDKtJ2IROJNoe3d6mW870/NRKvq2fhLB/Q5XmzWk00=
|
||||||
|
github.com/slack-go/slack v0.6.5/go.mod h1:FGqNzJBmxIsZURAxh2a8D21AnOVvvXZvGligs4npPUM=
|
||||||
|
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
26
pkg/jia/config.go
Normal file
26
pkg/jia/config.go
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
package jia
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
BotToken string
|
||||||
|
ChannelId string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewConfig() *Config {
|
||||||
|
return &Config{
|
||||||
|
BotToken: getEnv("SLACK_CLIENT_BOT_TOKEN", ""),
|
||||||
|
ChannelId: getEnv("SLACK_CHANNEL_ID", ""),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func getEnv(key string, defaultValue string) string {
|
||||||
|
value, exists := os.LookupEnv(key)
|
||||||
|
if exists {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
|
||||||
|
return defaultValue
|
||||||
|
}
|
Loading…
Reference in a new issue