Created
July 20, 2023 19:10
-
-
Save altacountbabi/e5d4a57a79c3d8c7a3c61e12545ad9a5 to your computer and use it in GitHub Desktop.
OpenChat library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios from 'axios' | |
class OpenChatBot { | |
constructor(botToken) { | |
this.botToken = botToken; | |
} | |
askBot = async (content) => { | |
const response = await axios.post('https://cloud.openchat.so/api/chat/send', JSON.stringify({ | |
from: 'user', | |
type: 'text', | |
content: content | |
}), { | |
headers: { | |
'accept': 'application/json, text/plain, */*', | |
'cache-control': 'no-cache', | |
'content-type': 'application/json', | |
'pragma': 'no-cache', | |
'sec-fetch-dest': 'empty', | |
'sec-fetch-mode': 'cors', | |
'sec-fetch-site': 'same-origin', | |
'sec-gpc': '1', | |
'x-bot-token': this.botToken, | |
'referrer': `https://cloud.openchat.so/chat/${this.botToken}`, | |
'referrer-policy': 'strict-origin-when-cross-origin' | |
} | |
}) | |
if (response.status != 200) { | |
throw new Error(`Response wasn't 200\nStatus code: ${response.status}\nBody: ${response.data}`) | |
} | |
return response.data.response.text | |
} | |
} | |
const chatbot = new OpenChatBot('YOUR BOT TOKEN') | |
chatbot.askBot('Hello') // returns a string, dont forget to use await | |
/* | |
how to get bot token: | |
1. copy bot url | |
2. get the token: https://cloud.openchat.so/chat/TOKEN HERE | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment