WeChat Bot SDK for Personal Account, Powered by TypeScript, Docker, and 💖
Switch branches/tags
Clone or download
windmemory and zixia Upgrade wechaty-puppet-padchat version in puppet config (#1643)
* update wechaty-puppet-padchat stable version to 0.18.1

* 0.23.19
Latest commit 2f61e02 Nov 15, 2018
Permalink
Failed to load latest commit information.
.circleci better circlerci steps Jul 3, 2018
.github add search first Aug 19, 2018
.vscode delete eslint because it not be used anymore Jul 1, 2018
bin fix pre-install puppet fail problem Aug 18, 2018
docs docs: Fixed FriendshipType enum typo (#1620) Oct 10, 2018
examples upgrade memory card for a better logic Aug 12, 2018
scripts fix pack testing Sep 15, 2018
src Upgrade wechaty-puppet-padchat version in puppet config (#1643) Nov 15, 2018
tests fix pack testing Sep 15, 2018
.codeclimate.yml make sure the port is not same as orignal Nov 3, 2016
.dockerignore ignore memory-card Aug 10, 2018
.editorconfig Makefile support for editorconfig Oct 4, 2018
.gitignore ignore /*.jpg Jun 13, 2018
.markdownlintrc lint markdown Apr 15, 2018
.travis.yml use travis env settings on web console Aug 29, 2018
AUTHORS add @lijiarui to AUTHOR file Dec 5, 2017
CHANGELOG.md update CHANGELOG for v0.20 Aug 18, 2018
CODEOWNERS Split PuppetPuppeteer Out (#1371) Jun 30, 2018
CODE_OF_CONDUCT.md Create CODE_OF_CONDUCT.md (#608) Jun 26, 2017
CONTRIBUTING.md wechaty -> Chatie May 1, 2018
Dockerfile fix docker image Aug 9, 2018
Dockerfile.alpine wechaty -> Chatie May 1, 2018
Dockerfile.onbuild fix chown: invalid user: ‘bot’ (#1089) Feb 4, 2018
LICENSE code clean Apr 20, 2018
README.md docs: fixed typo (#1625) Oct 16, 2018
TODO.md fix todo Jun 29, 2018
app.json fix heroku deploy config Jun 21, 2018
appveyor.yml fix appveyor win32 ci Jul 11, 2018
package.json Upgrade wechaty-puppet-padchat version in puppet config (#1643) Nov 15, 2018
shippable.yml enable shippable for all version branches Aug 18, 2018
tsconfig.json add some useful typescript v2.9 settings Jun 24, 2018
tslint.json throw error for use promise as boolean (#1604) Oct 4, 2018
wercker.yml add wercker ci for docker image deploy Mar 19, 2018

README.md

WECHATY

Wechaty

CONNECTING CHATBOTS

Wechaty is a Bot SDK for Wechat Personal Account which can help you create a bot in 6 lines of javascript, with cross-platform support include Linux, Windows, Darwin(OSX/Mac) and Docker.

NPM Version Downloads GitHub stars Docker Pulls TypeScript Greenkeeper badge

:octocat: https://github.com/chatie/wechaty
🐞 https://github.com/chatie/wechaty/issues
📖 https://github.com/chatie/wechaty/wiki
🐳 https://hub.docker.com/r/zixia/wechaty

VOICE OF THE DEVELOPER

"Wechaty is a great solution, I believe there would be much more users recognize it." link
-- @Gcaufy, Tencent Engineer, Author of WePY

"太好用,好用的想哭"
-- @xinbenlv, Google Engineer, Founder of HaoShiYou.org

"最好的微信开发库" link
-- @Jarvis, Baidu Engineer

"Wechaty让运营人员更多的时间思考如何进行活动策划、留存用户,商业变现" link
-- @lijiarui, CEO of BotOrange.

"If you know js ... try Chatie/wechaty, it's easy to use."
-- @Urinx Uri Lee, Author of WeixinBot(Python)

See more at Wiki:Voice Of Developer

The World's Shortest ChatBot Code: 6 lines of JavaScript

const { Wechaty } = require('wechaty') // import { Wechaty } from 'wechaty'

Wechaty.instance() // Global Instance
.on('scan', (qrcode, status) => console.log(`Scan QR Code to login: ${status}\nhttps://api.qrserver.com/v1/create-qr-code/?data=${encodeURIComponent(qrcode)}`))
.on('login',            user => console.log(`User ${user} logined`))
.on('message',       message => console.log(`Message: ${message}`))
.start()

Notice: Wechaty requires Node.js version >= 10

This bot can log all messages to the console after login by scan.

You can find more examples from Wiki and Example Directory.

REQUIREMENTS

  1. Node.js 10 or above
  2. Global Network Connection

GETTING STARTED

node

We have a Wechaty starter repository for beginners with the simplest setting. It will be just work out-of-the-box after you clone & npm install & npm start.

If you are new to Wechaty and want to try it the first time, we'd like to strong recommend you starting from this repository, and using it as your starter template for your project.

Otherwise, please saved the above The World's Shortest ChatBot Code: 6 lines of JavaScript example to a file named mybot.js before you can use either NPM or Docker to run it.

1. NPM

NPM Version npm (tag) Downloads

npm init
npm install wechaty

# create your first mybot.js file, you can copy/paste from the above "The World's Shortest ChatBot Code: 6 lines of JavaScript"
# then:
node mybot.js

2. Docker

Docker Pulls Docker Layers

Wechaty Docker supports both JavaScript and TypeScript. To use TypeScript just write in TypeScript and save with extension name .ts, no need to compile because we use ts-node to run it.

2.1. Run JavaScript

# for JavaScript
docker run -ti --rm --volume="$(pwd)":/bot zixia/wechaty mybot.js

2.2. Run TypeScript

# for TypeScript
docker run -ti --rm --volume="$(pwd)":/bot zixia/wechaty mybot.ts

Learn more about Wechaty Docker at Wiki:Docker.

API

Read the Full Documentation at Wechaty Official API Reference

1 Class Wechaty

Main bot class.

A Bot is a Wechaty instance that control a specific wechaty-puppet.

Wechaty API Description
event login emit after bot login full successful
event logout emit after the bot log out
event friendship emit when someone sends bot a friend request
event message emit when there's a new message
event room-join emit when anyone join any room
event room-topic emit when someone change room topic
event room-leave emit when anyone leave the room
event room-invite emit when there is a room invitation
event scan emit when the bot needs to show you a QR Code for scanning
method start(): Promise<void> start the bot
method stop(): Promise<void> stop the bot
method logonoff(): boolean bot login status
method logout(): Promise<void> logout the bot
method userSelf(): ContactSelf get the login-ed bot contact
method say(text: string): Promise<void> let bot say text to itself

2 Class Contact

All wechat contacts(friends/non-friends) will be encapsulated as a Contact.

Contact API Description
static find(query: string): Promise<null | Contact> find contact by name or alias, if the result more than one, return the first one.
static findAll(query: string): Promise<Contact[]> find contact by name or alias
static load(query: string): Contact get contact by id
property id: readonly string get contact id
method sync(): Promise<void> force reload data for contact , sync data from lowlevel API again
method say(text: string): Promise<void> send text, Contact, or file to contact
method self(): boolean check if contact is self
method name(): string get the name from a contact
method alias(): Promise<string> get the alias for a contact
method alias(newAlias: string): Promise<void> set or delete the alias for a contact
method friend(): boolean check if contact is friend
method type(): ContactType return the type of the Contact
method province(): string get the region 'province' from a contact
method city(): string get the region 'city' from a contact
method avatar(): Promise<FileBox> get avatar picture file stream
method gender(): ContactGender get gender from a contact

2.1 Class ContactSelf

Class ContactSelf is extended from Contact.

ContactSelf API Description
method avatar(file: FileBox): Promise<void> set avatar for bot
method qrcode(): Promise<string> get qrcode for bot
method signature(text: string): Promise<void> set signature for bot

2.2 Class Friendship

Send, receive friend request, and friend confirmation events.

Friendship API Description
static add(contact: Contact, hello?: string): Promise<void> send a friend invitation to contact
method accept(): Promise<void> accept Friend Request
method hello(): string get the hello string from a friendship invitation
method contact(): Contact get the contact from friendship
method type(): FriendshipType return the Friendship Type(unknown, confirm, receive, verify)

3 Class Message

All wechat messages will be encapsulated as a Message.

Message API Description
static find(query: string): Promise<null | Message> find message in cache and return the first one
static findAll(query: string): Promise<Message[]> find messages in cache, return a message list
method from(): Contact get the sender from a message
method to(): Contact get the destination of the message
method room(): null | Room get the room from the message.(If the message is not in a room, then will return null)
method text(): string get the text content of the message
method say(text: string): Promise<void> reply a Text, Media File , or contact message to the sender.
method type(): MessageType get the type from the message
method self(): boolean check if a message is sent by self
method mention(): Contact[] get message mentioned contactList.
method mentionSelf(): boolean check if a message is mention self
method forward(to: Contact): Promise<void> Forward the received message
method age(): number the number of seconds since it has been created
method date(): Date the time it was created
method toFileBox(): Promise<FileBox> extract the Media File from the Message, and put it into the FileBox.
method toContact(): Promise<Contact> get Share Card of the Message

4 Class Room

All wechat rooms(groups) will be encapsulated as a Room.

Room API Description
static create(contactList: Contact[], topic?: string): Promise<Room> create a new room
static find(query: string): Promise<null | Room> Try to find a room by filter. If get many, return the first one.
static findAll(query: string): Promise<Room[]> Find all contacts in a room
static load(query: string): Room load room by room id
property id: readonly string
event join emit when anyone join any room
event topic emit when someone change room topic
event leave emit when anyone leave the room
event invite emit when receive a room invitation
method sync(): <Promise<void> force reload data for room, sync data from lowlevel API again.
method say(text: string): Promise<void> Send text,media file, contact card, or text with mention @mention contact inside Room
method add(contact: Contact): Promise<void> Add contact in a room
method del(contact: Contact): Promise<void> Delete a contact from the room
method quit(): Promise<void> Bot quit the room itself
method topic(): Promise<string> GET topic from the room
method topic(newTopic: string): Promise<void> SET topic from the room
method announce(text: string): Promise<void> SET/GET announce from the room
method qrcode(): Promise<string> Get QR Code of the Room from the room, which can be used as scan and join the room.
method alias(contact: Contact): Promise<string> Return contact's roomAlias in the room
method [`roomAlias(contact: Contact): Promise<string null>`](https://chatie.io/wechaty/#Room+roomAlias)
method has(contact: Contact): Promise<boolean> Check if the room has member contact
method memberAll(query?: string): Promise<Contact[]> Find all contacts or with specific name in a room
method [`member(query: string): Promise<null Contact>`](https://chatie.io/wechaty/#Room+member)
method memberList():Promise<Contact[]> get all room member from the room
method owner(): null | Contact Get room's owner from the room.

4.1 Class RoomInvitation

Accept room invitation

RoomInvitation API Description
method accept(): Promise<void> accept Room Invitation
method inviter(): Contact get the inviter from room invitation
method roomTopic(): Promise<string> get the room topic from room invitation
method date(): Promise<Date> the time it was created
method age(): Promise<number> the number of seconds since it has been created

TEST

Ubuntu Linux/Mac Build Status Travis CentOS Linux Build Status CircleCI Windows Build Status Docker Build Status

Coverage Status Known Vulnerabilities

Wechaty is fully automatically tested by unit and integration tests, with Continious Integration & Continious Deliver(CI/CD) support powered by CI like Travis, Shippable and Appveyor.

To test Wechaty, run:

npm test

Get to know more about the tests from Wiki:Tests

RELEASE NOTES

POWERED BY WECHATY

Powered by Wechaty Donate Wechaty

Wechaty Badge

[![Powered by Wechaty](https://img.shields.io/badge/Powered%20By-Wechaty-blue.svg)](https://github.com/chatie/wechaty)

Get more embed html/markdown code from Wiki:PoweredByWechaty

Projects Using Wechaty

  1. 一个用CNN深度神剧网络给图片评分的wechaty项目
  2. Relay between Telegram and WeChat
  3. A chat bot managing the HaoShiYou wechat groups run by volunteers of haoshiyou.org
  4. An interactive chat bot to manage a TODO list
  5. Forward WeChat messages to telegram

Pull Request is welcome to add yours!

Learn more about Projects Using Wechaty at Wiki:PoweredByWechaty

FIND A GOOD SERVER

The best practice for running Wechaty Docker/NPM is using a VPS(Virtual Private Server) outside of China, which can save you hours of time because npm install and docker pull will run smoothly without any problem.

The following VPS providers are used by the Wechaty team, and they worked perfectly in production. You can use the following link to get one in minutes. Also, doing this can support Wechaty because you are referred by us.

Location Price Ram Payment Provider
Singapore $5 512MB Paypal DigitalOcean
Japan $5 1GB Paypal Linode
Korea $10 1GB Alipay, Paypal Netdedi

SEE ALSO

CONTRIBUTING

Issue Stats Issue Stats Join the chat at https://gitter.im/zixia/wechaty

How to contribute

Why contribute?

As an open source product, Chatie thrives from contributions of community members. Whatever your skill set is, there is a lot you can do to help us make Chatie better! So start forking!

Not sure where to start?

It's a myth that writing code is the only way to contribute to open source. Chatie community is open to new ideas and there are so many different ways to make valuable contributions. We have some ideas of how you can get started!

1. Contribute Code

  • Check out GitHub issues with the tags good first issue, pull request welcome or help wanted
  • Write code examples for documentation
  • Report a bug and work on resolving it
  • Collaborate with others on building new features

Tips: If you want to add new features or change the API, please submit an issue first to make sure no one else is already working on the same thing and discuss the implementation and API details with maintainers and users by creating an issue. When everything is settled down, you can submit a pull request.

Make sure to add tests for your features and bugfixes and update the documentation (see below) before submitting your code!

2. Contribute Support

  • Fix typos in documentation
  • Translate documentation to your local language
  • Write tutorials and blog posts, see more: https://github.com/chatie/blog
  • Answer questions on the Wechaty Developers Home or Github issues
  • Organize Chatie meetups or user groups in your area

Contact rui@chatie.io to learn more

Contributors List

https://github.com/Chatie/wechaty/wiki/Contributors

Join Us

Wechaty is used in many ChatBot projects by hundreds of developers. If you want to talk with other developers, just scan the following QR Code in WeChat with secret code wechaty, join our Wechaty Developers' Home.

Wechaty Developers' Home

Scan now, because other Wechaty developers want to talk with you too! (secret code: wechaty)

AUTHOR

Huan LI <zixia@zixia.net>

profile for zixia on Stack Exchange, a network of free, community-driven Q&A sites

My Story

My daily life/work depends on too much chat on wechat.

  • I almost have 14,000 wechat friends in May 2014, before wechat restricts a total number of friends to 5,000.
  • I almost have 400 wechat rooms, and most of them have more than 400 members.

Can you imagine that? I'm dying...

So a tireless bot working for me 24x7 on wechat, monitoring/filtering the most important message is badly needed. For example, it highlights discussion which contains the KEYWORDS which I want to follow up(especially in a noisy room). ;-)

At last, It's built for my personal study purpose of Automatically Testing.

COPYRIGHT & LICENSE

  • Code & Docs © 2016-2018 Huan LI <zixia@zixia.net>
  • Code released under the Apache-2.0 License
  • Docs released under Creative Commons