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.
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 OpenAIconfig.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 to string
or RegExp
, 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. Returning false
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 the preAnswerHook
function.The following two environment variables will be used if the required information is not provided by the config.
WECHATY_WEIXIN_OPENAI_TOKEN
process.env.WECHATY_WEIXIN_OPENAI_TOKEN
will be used if the config.token
is not provided.
WECHATY_WEIXIN_OPENAI_ENCODING_AES_KEY
process.env.WECHATY_WEIXIN_OPENAI_ENCODING_AES_KEY
will be used if the config.encodingAESKey
is not provided.
Yuan Gao (高原), CTO & Co-founder of Juzi.bot, \gaoyuan@juzi.bot\