Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9e34b33

Browse files
committed
init files
1 parent 9ea31cc commit 9e34b33

9 files changed

+439
-1
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,6 @@ typings/
5959

6060
# next.js build output
6161
.next
62+
.config/
63+
.node-gyp/
64+
*.memory-card.json

README.md

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,81 @@
1-
# docker-wechaty-getting-started
1+
# DOCKER-WECHATY-GETTING-STARTED
2+
3+
[![Powered by Wechaty](https://img.shields.io/badge/Powered%20By-Wechaty-blue.svg)](https://github.com/chatie/wechaty)
4+
[![Build Status](https://travis-ci.com/Chatie/docker-wechaty-getting-started.svg?branch=master)](https://travis-ci.com/Chatie/docker-wechaty-getting-started)
5+
26
Getting Started Template for Docker Users
7+
8+
## FEATURES
9+
10+
Use Docker to run Wechaty without any configuration.
11+
12+
## REQUIREMENTS
13+
14+
1. Docker Installed
15+
1. Good Global Internet Connection
16+
17+
## USAGE
18+
19+
### 1. Run Vanilla Javascript Example
20+
21+
```shell
22+
bash -x bin/docker-run-javascript-vanilla.sh
23+
```
24+
25+
### 2. Run ES6 Javascript Example
26+
27+
```shell
28+
bash -x bin/docker-run-javascript-es6.sh
29+
```
30+
31+
### 3. Run TypeScript Example
32+
33+
```shell
34+
bash -x bin/docker-run-typescript.sh
35+
```
36+
37+
## DOCKER
38+
39+
### Install Docker
40+
41+
Quick & easy install Docker via:
42+
43+
```shell
44+
curl -sSL https://get.docker.com | sh
45+
```
46+
47+
Or
48+
49+
```shell
50+
wget -qO- https://get.docker.com/ | sh
51+
```
52+
53+
Get to know more about Docker at: <https://www.docker.com/>
54+
55+
### Use Docker Registry China Mirror
56+
57+
Thanks Docker, there's a official registry mirror in China, and you can use it by adding `registry.docker-cn.com/` in front of the image name:
58+
59+
```diff
60+
- docker pull zixia/wechaty
61+
+ docker pull registry.docker-cn.com/zixia/wechaty
62+
```
63+
64+
See Also:
65+
66+
- https://www.docker-cn.com/registry-mirror
67+
- https://docs.docker.com/registry/recipes/mirror/#use-case-the-china-registry-mirror
68+
69+
## AUTHOR
70+
71+
[Huan LI](http://linkedin.com/in/zixia) \<[email protected]\>
72+
73+
<a href="https://stackexchange.com/users/265499">
74+
<img src="https://stackexchange.com/users/flair/265499.png" width="208" height="58" alt="profile for zixia on Stack Exchange, a network of free, community-driven Q&amp;A sites" title="profile for zixia on Stack Exchange, a network of free, community-driven Q&amp;A sites">
75+
</a>
76+
77+
## COPYRIGHT & LICENSE
78+
79+
- Code & Docs © 2018 Huan LI \<[email protected]\>
80+
- Code released under the Apache-2.0 License
81+
- Docs released under Creative Commons

bin/docker-run-javascript-es6.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
# import WECHATY_IMAGE from bin/docker-wechaty-image.sh
5+
source $(dirname $0)/docker-wechaty-image.env
6+
7+
docker pull "$WECHATY_IMAGE"
8+
9+
docker run \
10+
-t -i --rm \
11+
--name wechaty \
12+
-e WECHATY_LOG="$WECHATY_LOG" \
13+
-e WECHATY_PUPPET="$WECHATY_PUPPET" \
14+
-e WECHATY_TOKEN="$WECHATY_TOKEN" \
15+
--mount type=bind,source="$(pwd)",target=/bot \
16+
"$WECHATY_IMAGE" \
17+
src/javascript-es6.js
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
# import WECHATY_IMAGE from bin/docker-wechaty-image.sh
5+
source $(dirname $0)/docker-wechaty-image.env
6+
7+
docker pull "$WECHATY_IMAGE"
8+
9+
docker run \
10+
-t -i --rm \
11+
--name wechaty \
12+
-e WECHATY_LOG="$WECHATY_LOG" \
13+
-e WECHATY_PUPPET="$WECHATY_PUPPET" \
14+
-e WECHATY_TOKEN="$WECHATY_TOKEN" \
15+
--mount type=bind,source="$(pwd)",target=/bot \
16+
"$WECHATY_IMAGE" \
17+
src/javascript-vanilla.js

bin/docker-run-typescript.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
# import WECHATY_IMAGE from bin/docker-wechaty-image.sh
5+
source $(dirname $0)/docker-wechaty-image.env
6+
7+
docker pull "$WECHATY_IMAGE"
8+
9+
docker run \
10+
-t -i --rm \
11+
--name wechaty \
12+
-e WECHATY_LOG="$WECHATY_LOG" \
13+
-e WECHATY_PUPPET="$WECHATY_PUPPET" \
14+
-e WECHATY_TOKEN="$WECHATY_TOKEN" \
15+
--mount type=bind,source="$(pwd)",target=/bot \
16+
"$WECHATY_IMAGE" \
17+
src/typescript.ts

bin/docker-wechaty-image.env

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# WECHATY_IMAGE=zixia/wechaty:0.18
2+
3+
# Docker Registry Officla Mirror for China Users
4+
# https://www.docker-cn.com/registry-mirror
5+
# https://docs.docker.com/registry/recipes/mirror/#use-case-the-china-registry-mirror
6+
WECHATY_IMAGE=registry.docker-cn.com/zixia/wechaty:latest

src/javascript-es6.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* Wechaty - https://github.com/chatie/wechaty
3+
*
4+
* @copyright 2016-2018 Huan LI <[email protected]>
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
*/
19+
import {
20+
Wechaty,
21+
} from 'wechaty'
22+
23+
import { generate } from 'qrcode-terminal'
24+
25+
/**
26+
*
27+
* 1. Declare your Bot!
28+
*
29+
*/
30+
const bot = new Wechaty({
31+
name : 'javascript-es6',
32+
})
33+
34+
/**
35+
*
36+
* 2. Register event handlers for Bot
37+
*
38+
*/
39+
bot
40+
.on('login', onLogin)
41+
.on('scan', onScan)
42+
.on('error', onError)
43+
.on('message', onMessage)
44+
45+
/**
46+
*
47+
* 3. Start the bot!
48+
*
49+
*/
50+
bot.start()
51+
.catch(async e => {
52+
console.error('Bot start() fail:', e)
53+
await bot.stop()
54+
process.exit(-1)
55+
})
56+
57+
/**
58+
*
59+
* 4. You are all set. ;-]
60+
*
61+
*/
62+
63+
/**
64+
*
65+
* 5. Define Event Handler Functions for:
66+
* `scan`, `login`, `logout`, `error`, and `message`
67+
*
68+
*/
69+
function onScan (qrcode, status) {
70+
generate(qrcode, { small: true })
71+
72+
// Generate a QR Code online via
73+
// http://goqr.me/api/doc/create-qr-code/
74+
const qrcodeImageUrl = [
75+
'https://api.qrserver.com/v1/create-qr-code/?data=',
76+
encodeURIComponent(qrcode),
77+
].join('')
78+
79+
console.log(`[${status}] ${qrcodeImageUrl}\nScan QR Code above to log in: `)
80+
}
81+
82+
function onLogin (user) {
83+
console.log(`${user.name()} login`)
84+
bot.say('Wechaty login').catch(console.error)
85+
}
86+
87+
function onError (e) {
88+
console.error('Bot error:', e)
89+
}
90+
91+
/**
92+
*
93+
* 6. The most important handler is for:
94+
* dealing with Messages.
95+
*
96+
*/
97+
async function onMessage (msg) {
98+
console.log(msg.toString())
99+
}

src/javascript-vanilla.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* Wechaty - https://github.com/chatie/wechaty
3+
*
4+
* @copyright 2016-2018 Huan LI <[email protected]>
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*
18+
*/
19+
const {
20+
Wechaty,
21+
} = require('wechaty')
22+
23+
const qrTerm = require('qrcode-terminal')
24+
25+
/**
26+
*
27+
* 1. Declare your Bot!
28+
*
29+
*/
30+
const bot = new Wechaty({
31+
name : 'javascript-es6',
32+
})
33+
34+
/**
35+
*
36+
* 2. Register event handlers for Bot
37+
*
38+
*/
39+
bot
40+
.on('login', onLogin)
41+
.on('scan', onScan)
42+
.on('error', onError)
43+
.on('message', onMessage)
44+
45+
/**
46+
*
47+
* 3. Start the bot!
48+
*
49+
*/
50+
bot.start()
51+
.catch(async e => {
52+
console.error('Bot start() fail:', e)
53+
await bot.stop()
54+
process.exit(-1)
55+
})
56+
57+
/**
58+
*
59+
* 4. You are all set. ;-]
60+
*
61+
*/
62+
63+
/**
64+
*
65+
* 5. Define Event Handler Functions for:
66+
* `scan`, `login`, `logout`, `error`, and `message`
67+
*
68+
*/
69+
function onScan (qrcode, status) {
70+
generate(qrcode, { small: true })
71+
72+
// Generate a QR Code online via
73+
// http://goqr.me/api/doc/create-qr-code/
74+
const qrcodeImageUrl = [
75+
'https://api.qrserver.com/v1/create-qr-code/?data=',
76+
encodeURIComponent(qrcode),
77+
].join('')
78+
79+
console.log(`[${status}] ${qrcodeImageUrl}\nScan QR Code above to log in: `)
80+
}
81+
82+
function onLogin (user) {
83+
console.log(`${user.name()} login`)
84+
bot.say('Wechaty login').catch(console.error)
85+
}
86+
87+
function onError (e) {
88+
console.error('Bot error:', e)
89+
}
90+
91+
/**
92+
*
93+
* 6. The most important handler is for:
94+
* dealing with Messages.
95+
*
96+
*/
97+
async function onMessage (msg) {
98+
console.log(msg.toString())
99+
}

0 commit comments

Comments
 (0)