Building a Chatbot based on Blockchain

Building a Chatbot based on Blockchain
3 min read

A blockchain-based chatbot is a chatbot that leverages blockchain technology as a core component to enhance its functionality, security, and transparency. A blockchain development company can develop this type of chatbot by incorporating the decentralized and immutable nature of blockchain.

Here are some key characteristics and advantages of a blockchain-based chatbot:

Decentralization

A blockchain-based chatbot functions on a decentralised network of nodes, eliminating the need for a centralised authority. Because of this decentralisation, no single entity has authority over the chatbot's activities, making it immune to censorship and single points of failure.

Immutable and Transparent Chat History:

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.

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

npm init — y

npm i ethers

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

Wallet connected: 0xD5de11DDaaE10Ee4607a22b3AAC2259450aeE859
Wallet connected: 0x06C2479D95AEe2C66e3369440A92EC0AA2885Ea0
Message sent to the blockchain: 0x86b5934eca8de572c04c2fd52563c87c671360b6ef6fbedf96906914b61ecf31
Message sent to the blockchain: 0x05c8e5a834854f117221bf67a163ca5a1a3dfe10d155a253391ed89c8d7571e1

If you are interested in developing a similar project, you may connect with our skilled blockchain developers to get started.

In case you have found a mistake in the text, please send a message to the author by selecting the mistake and pressing Ctrl-Enter.
Pranjali Tiwari 4
Joined: 11 months ago
Comments (0)

    No comments yet

You must be logged in to comment.

Sign In / Sign Up