Skip to content

Add build.sbt #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ lint:

.PHONY: install
install:
echo install scala
sbt

.PHONY: test
test:
echo test scala

.PHONY: bot
bot:
scala examples/ding-dong-bot.scala
sbt

.PHONY: version
version:
Expand Down
21 changes: 9 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ Wechaty is a RPA SDK for Wechat **Individual** Account that can help you create

## Requirements

1. Scala
1. sbt
https://www.scala-sbt.org/1.x/docs/Setup.html
2. Scala 2.12.x
https://www.scala-lang.org/download/scala2.html

## Quick Start

Expand All @@ -32,20 +35,14 @@ Wechaty is a RPA SDK for Wechat **Individual** Account that can help you create
make install
```

3. Set token for your bot

```sh
# it must be hostie token
export WECHATY_PUPPET=wechaty-puppet-hostie
export WECHATY_PUPPET_HOSTIE_TOKEN=your_token_at_here
```

4. Run the bot
4. Set token and Run the bot

```shell
make bot
# or
scala examples/ding-dong-bot.scala
# Make sure you are in the sbt console
# It must be hostie token
eval System.setProperty("WECHATY_PUPPET_HOSTIE_TOKEN", "your_token_at_here")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please help us to change the WECHATY_PUPPET_HOSTIE to WECHATY_PUPPET_SERVICE because we have renamed it for more than 6 months in our ecosystem.

run
```

## The World's Shortest Scala ChatBot: 6 lines of Code
Expand Down
10 changes: 10 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name := "scala-sbt-wechaty-getting-started"

version := "0.1"

scalaVersion := "2.12.14"

val wechaty = "io.github.wechaty" %% "wechaty" % "0.0.15"
val apacheCommons = "org.apache.commons" % "commons-text" % "1.9"

libraryDependencies ++= Seq(wechaty, apacheCommons)
36 changes: 0 additions & 36 deletions examples/ding-dong-bot.scala

This file was deleted.

68 changes: 68 additions & 0 deletions src/main/scala/ding-dong-bot.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import org.apache.commons.lang3.StringUtils
import wechaty.{Wechaty, WechatyOptions}

/**
*
* @author <a href="mailto:[email protected]">Jun Tsai</a>
* @since 2020-06-02
*/
object DingDongBot {
def main(args: Array[String]): Unit = {
val token: String = sys.props.getOrElse("WECHATY_PUPPET_HOSTIE_TOKEN",
throw new Exception(s"Error: WECHATY_PUPPET_HOSTIE_TOKEN is not found in the environment variable!" +
s"You need a TOKEN to run the Scala Wechaty." +
s"Please goto our README for details https://wechaty.js.org/docs/puppet-services/padlocal/"))
TokenValidator.isValidToken(token)

val option = new WechatyOptions

try {
val bot = Wechaty.instance(option)

bot
.onScan(payload => {
println("Scan QR Code to login: %s\nhttps://api.qrserver.com/v1/create-qr-code/?data=%s\n".format(payload.status, payload.qrcode))
})
.onLogin(payload => {
println("User %s logined\n".format(payload.id))
})
.onMessage(message => {
println(message)
if (message.payload.`type` != wechaty.puppet.schemas.Message.MessageType || message.payload.text != "ding") {
println("Message discarded because it does not match ding")
} else {
message.say("dong")
println("dong")
}
})


bot.start()

Thread.currentThread().join()


} catch {
case _: java.io.FileNotFoundException =>
System.out.println(s"TOKEN is NOT registered successfully, please refer to https://wechaty.js.org/docs/puppet-services/diy/")
}

}
}

object TokenValidator {
def isValidToken(token: String): Unit = {
import java.util.regex.Pattern
val p = Pattern.compile("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$")
val valid = p.matcher(token).matches()


if (valid) {
System.out.println("token format correct, creating Wechaty instance...")
} else {
throw new Exception(s"Error: Wrong token format, WECHATY_PUPPET_HOSTIE_TOKEN should be in UUID v4 format!")
}
}


}