Posted By : Yogesh
A blockchain-based chatbot is a chatbot that leverages blockchain technology as a core component to enhance its functionality, security, and transparency. This type of chatbot incorporates the decentralized and immutable nature of blockchain and is developed using blockchain development services.
A blockchain-based chatbot operates on a decentralized network of nodes, removing the need for a central authority. This decentralization ensures that no single entity has control over the chatbot's operations, making it resistant to censorship and single points of failure.
The chat history in a blockchain-based chatbot is stored on the blockchain, which ensures its immutability. Once a message is added to the blockchain, it cannot be modified or deleted. This transparency builds trust and allows users to verify the authenticity of the chatbot's responses.
Also, Explore | Exploring the Synergy of Blockchain and Chatbot Technology
Now Open the Remix and paste the following code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Chatbot {
struct Message {
address sender;
string content;
}
Message[] public chatHistory;
event MessageSent(address sender, string content);
function sendMessage(string memory message) public {
Message memory newMessage = Message(msg.sender, message);
chatHistory.push(newMessage);
emit MessageSent(msg.sender, message);
}
function getChatHistoryLength() public view returns (uint) {
return chatHistory.length;
}
function getMessage(uint index) public view returns (address, string memory) {
require(index < chatHistory.length, "Invalid message index");
Message memory message = chatHistory[index];
return (message.sender, message.content);
}
}
Now, deploy the contract on the Sepolia network.
Open the VS code and setup the node js project
Copy the following code:
const { ethers } = require("ethers");
const contractABI = require("./Chatbot.json"); // Contract ABI
const contractAddress = "0x002D7EF6B4Bd982eD4D05D56744401fbeC7703f4";
const provider = new ethers.JsonRpcProvider(
"https://sepolia.infura.io/v3/API_KEY"
);
const contract = new ethers.Contract(contractAddress, contractABI, provider);
async function contractEvents() {
contract.on("MessageSent", async (param1, param2, pair, amount, event) => {
const chatBotPrivateKey =
"";
const chatBotWallet = await connectWallet(chatBotPrivateKey);
await sendResponse(param1, param2, chatBotWallet);
});
}
async function connectWallet(walletPrivateKey) {
try {
const wallet = new ethers.Wallet(walletPrivateKey, provider);
const connectedWallet = wallet.connect(provider);
console.log("Wallet connected:", connectedWallet.address);
return connectedWallet;
} catch (error) {
console.error("Failed to connect with wallet:", error);
return null;
}
}
async function sendMessage(message, wallet) {
try {
const contractWithSigner = contract.connect(wallet);
const transaction = await contractWithSigner.sendMessage(message);
await transaction.wait();
console.log("Message sent to the blockchain:", transaction.hash);
} catch (error) {
console.error("Failed to send the message:", error);
}
}
async function sendResponse(param1, param2, chatBotWallet) {
if (param1 !== "0x06C2479D95AEe2C66e3369440A92EC0AA2885Ea0") {
if (param2.includes("Hello") || param2.includes("Hii")) {
try {
const contractWithSigner = contract.connect(chatBotWallet);
const transaction = await contractWithSigner.sendMessage(
"Hello! How can I assist you today?"
);
await transaction.wait();
console.log("Message sent to the blockchain:", transaction.hash);
} catch (error) {
console.error("Failed to send the message:", error);
}
} else {
try {
const contractWithSigner = contract.connect(chatBotWallet);
const transaction = await contractWithSigner.sendMessage(
"I'm sorry, but I'm having trouble understanding your message. It seems to be a random string of characters. If you have any specific questions or need assistance with something, please let me know, and I'll be happy to help!"
);
await transaction.wait();
console.log("Message sent to the blockchain:", transaction.hash);
} catch (error) {
console.error("Failed to send the message:", error);
}
}
}
}
async function main() {
const walletPrivateKey =
""; // Replace with your private key
const wallet = await connectWallet(walletPrivateKey);
if (wallet) {
await contractEvents();
await sendMessage("Hello, blockchain!", wallet);
}
}
main();
Run node index.js
If you are interested in developing a similar project, you may connect with our skilled blockchain developers to get started.
November 18, 2024 at 03:09 pm
Your comment is awaiting moderation.