World's Shortest Chatbot
World's Shortest Chatbot is the very first example showcasing how easy it is to get started with Wechaty in minimum 6 lines of code.
Requirements
Getting started
You should have Node.js
installed on your system. If you do not have Node.js
installed (or have a version below 12), then you need to install the latest version of Node.js
by following the links below:
Node.js installation docs
Installation guide for
Node.js
on other platforms can be found here.
Usage
- TypeScript
- JavaScript
- Python
- Go
- Java
- PHP
- Scala
- C#
- Rust
- OpenAPI
import { Wechaty } from 'wechaty'
async function main () {
const bot = new Wechaty()
bot
.on('scan', (qrcode, status) => console.log(`Scan QR Code to login: ${status}\nhttps://wechaty.js.org/qrcode/${encodeURIComponent(qrcode)}`))
.on('login', user => console.log(`User ${user} logged in`))
.on('message', message => console.log(`Message: ${message}`))
await bot.start()
}
main()
.catch(console.error)
import { Wechaty } from 'wechaty'
async function main () {
const bot = new Wechaty()
bot
.on('scan', (qrcode, status) => console.log(`Scan QR Code to login: ${status}\nhttps://wechaty.js.org/qrcode/${encodeURIComponent(qrcode)}`))
.on('login', user => console.log(`User ${user} logged in`))
.on('message', message => console.log(`Message: ${message}`))
await bot.start()
}
main()
.catch(console.error)
from wechaty import Wechaty
import asyncio
async def main():
bot = Wechaty()
bot.on('scan', lambda status, qrcode, data: print('Scan QR Code to login: {}\nhttps://wechaty.js.org/qrcode/{}'.format(status, qrcode)))
bot.on('login', lambda user: print('User {} logged in'.format(user)))
bot.on('message', lambda message: print('Message: {}'.format(message)))
await bot.start()
asyncio.run(main())
package main
import (
"fmt"
"github.com/wechaty/go-wechaty/wechaty"
)
func main() {
_ = wechaty.NewWechaty().
OnScan(func(qrCode, status string) {
fmt.Printf("Scan QR Code to login: %s\nhttps://wechaty.js.org/qrcode/%s\n", status, qrCode)
}).
OnLogin(func(user string) { fmt.Printf("User %s logged in\n", user) }).
OnMessage(func(message string) { fmt.Printf("Message: %s\n", message) }).
Start()
}
package io.github.wechaty;
class Bot{
public static void main(String args[]){
Wechaty bot = Wechaty.instance()
.onScan((qrcode, statusScanStatus, data) -> System.out.println(QrcodeUtils.getQr(qrcode)))
.onLogin(user -> System.out.println("User logged in :" + user))
.onMessage(message -> System.out.println("Message:" + message))
.start(true);
}
}
$wechaty = \IO\Github\Wechaty\Wechaty::getInstance($token, $endPoint);
$wechaty->onScan(function($qrcode, $status, $data) {
$qr = \IO\Github\Wechaty\Util\QrcodeUtils::getQr($qrcode);
echo "$qr\n\nOnline Image: https://wechaty.js.org/qrcode/$qrcode\n";
})->onLogin(function(\IO\Github\Wechaty\User\ContactSelf $user) {
})->onMessage(function(\IO\Github\Wechaty\User\Message $message) {
$message->say("hello from PHP7.4");
})->start();
package wechaty
object DingDongBot {
def main(args: Array[String]): Unit = {
Wechaty.instance()
.onScan(payload => { println("Scan QR Code to login: %s\nhttps://wechaty.js.org/qrcode/%s\n".format(payload.status, payload.qrcode)) })
.onLogin(payload => { println("User %s logged in\n".format(payload.id)) })
.onMessage(message => { println(message) })
.start()
Thread.currentThread().join()
}
}
var wechaty = new Wechaty(options, logger).onScan((qrcode, status) => {
Console.WriteLine($"Scan QR Code to login: {status} https://wechaty.js.org/qrcode/{(qrcode)}`");
}).OnLogin( user => {
Console.WriteLine("User {user} logined");
}).OnMessage( message => {
Console.WriteLine($"Message: {message}");
}).Start();
let bot = Wechaty::new(PuppetService::new(options).await.unwrap());
bot.on_scan(handle_scan)
.on_login(handle_login)
.on_logout(handle_logout)
.on_message(handle_message)
.start()
.await;
# To be add: curl ...
For building a bot with Wechaty, you have to follow the steps below:
- Import wechaty.
- Create a function
main
and initialize a bot by providing it a name. - Assign proper functions to call when an event is triggered.
- When
scan
is triggered, it generates QR code. login
will display{user} logged in
if the user has logged in.message
will display message on console.
- When
- Finally, start the bot with
bot.start
.