diff --git a/Makefile b/Makefile
index 51e0fa4..12bda87 100644
--- a/Makefile
+++ b/Makefile
@@ -17,7 +17,7 @@ lint:
.PHONY: install
install:
- echo install scala
+ sbt
.PHONY: test
test:
@@ -25,7 +25,7 @@ test:
.PHONY: bot
bot:
- scala examples/ding-dong-bot.scala
+ sbt
.PHONY: version
version:
diff --git a/README.md b/README.md
index 6799fcd..51f8723 100644
--- a/README.md
+++ b/README.md
@@ -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
@@ -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")
+ run
```
## The World's Shortest Scala ChatBot: 6 lines of Code
diff --git a/build.sbt b/build.sbt
new file mode 100644
index 0000000..251d2c8
--- /dev/null
+++ b/build.sbt
@@ -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)
\ No newline at end of file
diff --git a/examples/ding-dong-bot.scala b/examples/ding-dong-bot.scala
deleted file mode 100755
index f1448c8..0000000
--- a/examples/ding-dong-bot.scala
+++ /dev/null
@@ -1,36 +0,0 @@
-package wechaty
-
-import wechaty.puppet.schemas.Message.MessageType
-
-/**
- *
- * @author Jun Tsai
- * @since 2020-06-02
- */
-object DingDongBot {
- def main(args: Array[String]): Unit = {
- val option = new WechatyOptions
- 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` != MessageType.MessageTypeText || message.payload.text != "ding" ){
- println("Message discarded because it does not match ding")
- }else {
- message.say("dong")
- println("dong")
- }
- })
-
-
- bot.start()
-
- Thread.currentThread().join()
- }
-}
diff --git a/src/main/scala/ding-dong-bot.scala b/src/main/scala/ding-dong-bot.scala
new file mode 100644
index 0000000..f7cf6c1
--- /dev/null
+++ b/src/main/scala/ding-dong-bot.scala
@@ -0,0 +1,68 @@
+import org.apache.commons.lang3.StringUtils
+import wechaty.{Wechaty, WechatyOptions}
+
+/**
+ *
+ * @author Jun Tsai
+ * @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!")
+ }
+ }
+
+
+}