如何在Circuit Bot被添加至Circuit对话时触发指定事件?
Great question! Setting up your Circuit Bot to auto-introduce itself and send a form when added to a conversation is straightforward with Circuit's Bot Framework. Let's walk through the exact steps:
1. Listen for the Bot Addition Event
First, your bot needs to detect when it's been added to a new conversation. Circuit's SDK emits a memberAdded event whenever a new member joins a conversation—we just need to check if that new member is the bot itself.
Here's a JavaScript example using the Circuit SDK:
const Circuit = require('circuit-sdk'); // Initialize your bot client with credentials from the Circuit Developer Portal const botClient = new Circuit.Client({ client_id: 'YOUR_BOT_CLIENT_ID', client_secret: 'YOUR_BOT_CLIENT_SECRET' }); // Listen for new members joining conversations botClient.addEventListener('memberAdded', async (event) => { // Verify the added member is this bot if (event.member.userId === botClient.loggedOnUser.userId) { // Kick off our welcome workflow await handleBotAddedToConversation(event.convId); } });
2. Send the Self-Introduction Message
Once we confirm the bot was added, we can send a friendly intro message using the addTextItem API. We'll wrap this in a helper function to keep things clean:
async function handleBotAddedToConversation(convId) { // Send the introduction message const introText = "Hey there! I'm your Circuit Bot. I’m here to help you gather and organize key details. Let’s start by filling out this quick form!"; await botClient.addTextItem(convId, introText); // Next, send the information collection form await sendInformationForm(convId); }
3. Create and Send the Circuit Form
Circuit lets you build structured forms using the addFormItem API. You can customize fields (text inputs, email fields, dropdowns, etc.) based on the info you need to collect. Here's an example:
async function sendInformationForm(convId) { // Define your form structure const collectionForm = { title: 'Team Member Information Form', fields: [ { type: 'text', label: 'Full Name', name: 'fullName', required: true // Mark field as mandatory }, { type: 'email', label: 'Work Email', name: 'workEmail', required: true }, { type: 'select', label: 'Department', name: 'department', options: ['Engineering', 'Marketing', 'Sales', 'HR', 'Operations'], required: true }, { type: 'textarea', label: 'Brief About Yourself', name: 'bio', required: false // Optional field } ] }; // Send the form to the conversation await botClient.addFormItem(convId, collectionForm); }
Quick Notes to Keep in Mind
- Permissions: Ensure your bot has the
sendMessagesandmanageFormspermissions enabled in the Circuit Developer Portal—without these, the bot won't be able to send messages or forms. - Testing: Always test the workflow by adding the bot to a test conversation first to verify the event triggers and messages/forms send correctly.
- Customization: Tweak the intro text and form fields to match your specific use case (e.g., add date pickers, checkboxes, etc.).
内容的提问来源于stack exchange,提问作者Kamal Garg

