Quick Start on using MyBotChat API
In this step-by-step tutorial, we look at how to use API to interact with your chatbot.
Prerequisite
You need to have an account with MyBot and created a chatbot. If you have not done this step yet, visit this get-started turorial page to start.
1. Generate API Key
- Login to your account, go to My Chatbots then select a bot of your choice and go to API Key tab.
- click on Add New to see a popup form to enter a name. Name is optional. Finally click Save.
- From the list of your API Key, copy the key.
2. Chat with your Chatbot via API
In order to send a message to a chatbot, there has to be a chat session to hold all the chat messages. So, the first step in the process to create a session. This session is similar to a group or channel where one or more users can join and write and read message. Final step is to use the message endpoint to send message to the bot.
Create Chat Session: Before you can send a message to your chatbot, you need to create a chat session. This session will hold all the messages between a user and the bot. We are going to use
/api/Conversation
to create a new session and copy thesessionId
from the response for next step. Curl Example:curl --location --request POST 'https://mybot.chat/api/Conversation' \ --header 'Authorization: Bearer --use-your-API-Key-here--' \ --header 'Content-Type: application/json' \ --data-raw '{ "botId": "--put-your-chatbot-Id-here--" }'
Javascript Example:
var myHeaders = new Headers(); myHeaders.append("Authorization", "Bearer --use-your-API-Key-here--"); myHeaders.append("Content-Type", "application/json"); var raw = JSON.stringify({"botId":"--put-your-chatbot-Id-here--"}); var requestOptions = { method: 'POST', headers: myHeaders, body: raw, redirect: 'follow' }; fetch("https://mybot.chat/api/Conversation", requestOptions) .then(response => response.text()) .then(result => console.log(result)) .catch(error => console.log('error', error));
Example Reponse from Server:
{ "sessionId": "c71d4799-3213-4b54-b7d5-f296a5438d8a" }
Send Message: with the Session Id you got from previous step. Use this message endpoint to send chat message to the bot:
/api/Message
Example in Curl:curl --location --request POST 'https://mybot.chat/api/ChatMessage' \ --header 'Authorization: Bearer --use-your-API-Key-here--' \ --header 'Content-Type: application/json' \ --data-raw '{ "botId": "--put-your-chatbot-Id-here--", "sessionId": "--put-your-Session-Id-here--", "message": "tell me your name and what you can do" }'
Example in Javascript:
var myHeaders = new Headers(); myHeaders.append("Authorization", "Bearer --use-your-API-Key-here--"); myHeaders.append("Content-Type", "application/json"); var raw = JSON.stringify({"botId":"--put-your-chatbot-Id-here--","sessionId":"--put-your-Session-Id-here--","message":"tell me your name and what you can do"}); var requestOptions = { method: 'POST', headers: myHeaders, body: raw, redirect: 'follow' }; fetch("https://mybot.chat/api/ChatMessage", requestOptions) .then(response => response.text()) .then(result => console.log(result)) .catch(error => console.log('error', error));
Example Response from the bot:
{ "messageId": "9bcdd2a9-d7e1-41f6-85a1-0cdf1d3c1b1a", "sessionId": "c71d4799-3213-4b54-b7d5-f296a5438d8a", "username": "Default User", "content": "I am MyBot. I can assist you with inquiries about our products and services, provide information, and help facilitate connections with our sales team. If you're interested in becoming a customer or have specific questions, feel free to ask! Also, could you share your contact information (email, phone, Telegram, or WhatsApp) so our sales team can reach out if needed?", "timestamp": "0001-01-01T00:00:00", "imageUrl": null }
Open Source Chatbot UI Client
We have sample code written in NextJS you can try to see how it work: Chatbot UI Client