Integrate Wechat Personal Account to Microsoft BotFramework with a BotBuilder Wechaty Connector

本文介绍了一种将个人微信账号连接到Microsoft Bot Framework的方法,通过创建botbuilder-wechaty-connector实现。此连接器简化了开发过程,使得开发者能够轻松地为个人微信账号构建聊天机器人。

BotFramework

Microsoft BotFramework is a robust platform that supports almost all kinds of the instance messengers like Facebook messenger, Skype, Slack, and backed by the powerful Cognitive Services like LUIS.ai, QnAMaker.ai, Azure Machine Learning. BotBuilder is a powerful SDK provides all we need when we are developing a ChatBot. I started learning them about 18 months ago, and I love them very much.

However, it does not support Wechat Individual Account, so I decided to make some fun on it: connect Wechat PERSONAL Account to the BotBuilder by creating a new Connector: botbuilder-wechaty-connector.

Update

The Connector is a bridge between the BotBuilder and the Instance Messenger, in our case, Wechat.

There’s some document at Bot Builder for Node.js Libraries - Interface IConnector with two examples: ChatConnector and ConsoleConnector. However, I found the most useful example is a Wechat Official Account Connector: https://github.com/jyfcrw/botbuilder-wechat-connector whose code is concise and clean, so I decided to start from here.

It’s not very difficult for me to replace the two NPM module wechat & wechat-api to wechaty, and rewrite all the code to TypeScript, then add linting & testing, setup DevOps CI/CD to publish on NPM. It takes me about three hours to finish them.

Contract to the Wechat Official Account, Personal Account will be very easy to start because you only need to scan the QR code on the phone to log in instead of register an official account with APP_ID & APP_SECRET.

The following code simple setup a connector between BotBuilder and Wechat, ask and answer elementary questions:

  1. What’s your name?
  2. OK, ${your name}
  3. What’s your age?
  4. All right, ${your age}
  5. How are you, ${your name}
import * as builder from 'botbuilder'

import { WechatyConnector } from 'botbuilder-wechaty-connector'

// Create wechat personal account connector
const wechatyConnector = new WechatyConnector()

const bot = new builder.UniversalBot(wechatyConnector)

// Bot dialogs
bot.dialog('/', [
  function (session) {
      if (session.userData && session.userData.name) {
        session.send('How are you, ' + session.userData.name)
      } else {
        builder.Prompts.text(session, 'Whats your name?')
      }
  },
  function (session, results) {
      session.userData.name = results.response
      session.send('OK, ' + session.userData.name)
      builder.Prompts.text(session, 'Whats your age?')
  },
  function (session, results) {
      session.userData.age = results.response
      session.send('All right, ' + results.response)
  },
])

wechatyConnector.listen()

The above example is borrowed from botbuilder-wechat-connector.

Hope you will like it and could make fun of it too, cheers!

See Also

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值