Weixin OpenAI is a cloud-based Natural Language Processing (NLP) service that easily creates a natural conversational layer over your data.
Wechaty Weixin OpenAI plugin enables your bot with Weixin OpenAI skills
Wechaty Weixin OpenAI Plugin helps you to answer questions in WeChat with the power of https://openai.weixin.qq.com.
- Node.js v12+
- Wechaty v0.40+
- This Weixin OpenAI Plugin
- Registration of Weixin OpenAI platform
- A bot in Weixin OpenAI platform
import { Message, Wechaty } from 'wechaty'
import {
WechatyWeixinOpenAI,
AIBotRequestResponse,
SentimentData
} from 'wechaty-weixin-openai'
const config = {
mention: true, // default true: require at the bot in room.
room: true,
contact: true, // enable direct message.
/**
* Weixin OpenAI config
*/
token: 'your-valuable-token',
encodingAESKey: 'very-secret-encoding-key',
/**
* Decide whether the `preAnswerHook` function have the
* `sentiment` argument passed.
*/
includeSentiment: true,
/**
* No answer from Weixin OpenAI will call the below callback function
*/
noAnswerHook: (message: Message) => { console.log(`No Answer Message: ${message}`) }
/**
* Will be called before the answer really replied by the bot
* In the hook function, the answer that going to be answered
* and sentiment data will be passed into the function
*
* If the function returns false, this function will block
* further process of the message. With this, you can get the
* answer from the Weixin OpenAI and decide you want to use
* it with your own logic
*/
preAnswerHook: (
message: Message,
answer: AIBotRequestResponse,
sentiment?: SentimentData,
) => {
console.log(`PreAnswerHook() with message: ${message}, answer: ${answer} and sentiment: ${sentiment}`)
}
}
const WeixinOpenAIPlugin = WechatyWeixinOpenAI(config)
const wechaty = new Wechaty()
wechaty.use(WeixinOpenAIPlugin)
config.token
: Token for Weixin OpenAIconfig.encodingAESKey
: Encoding AES key for Weixin OpenAI
config.language
: If set to a language ('chinese', 'english', etc), then the plugin will only reply message text in that specified language. (default: match all languages)
config.contact
: Whether to allow direct message to be sync with ticket reply.false
to deny all,true
for allow all; Supports contact id(string
) and contact name(RegExp
). You can also mix them in array.config.room
: The room id of your service WeChat room.config.skipMessage
: If set it tostring
orRegExp
, then the message text that match the config will not be processed by the plugin. Array supported.
config.noAnswerHook
: This is an optional argument, if this function is defined, when Weixin OpenAI returns an answer that actually matched nothing, this function will be called, which allows you to do other logic when the Weixin OpenAI bot can not answer the question automatically for you, such as push your own contact card or create a room with the bot and you together.config.preAnswerHook
: This hook function is called before the real answer action is taken. In this hook function, you can get the message and the answer returned from Weixin OpenAI. You can add your own logic to determine whether this message should be answered by Weixin OpenAI automatically or not. Returningfalse
in the hook function will prevent further code to be executed.
config.includeSentiment
: This will decide whether you want to append sentiment data analyzed by Weixin OpenAI into thepreAnswerHook
function.
The following two environment variables will be used if the required information is not provided by the config.
process.env.WECHATY_WEIXIN_OPENAI_TOKEN
will be used if the config.token
is not provided.
process.env.WECHATY_WEIXIN_OPENAI_ENCODING_AES_KEY
will be used if the config.encodingAESKey
is not provided.
- Init code base from wechaty-plugin-qnamaker
- Weixin Open AI integration
- Add aibot api integration with Weixin OpenAI
- Add `noAnswer
Yuan Gao (高原), CTO & Co-founder of Juzi.bot, <[email protected]>
- Code & Docs © 2020 Yuan Gao <[email protected]>
- Code released under the Apache-2.0 License
- Docs released under Creative Commons