Building a Chatbot based on Blockchain

Posted By : Yogesh

Jul 16, 2023

What is a Blockchain-based Chatbot

 

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.

 

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

 

Decentralization

 

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.

 

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.

 

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 

  

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. 

Leave a

Comment

Name is required

Invalid Name

Comment is required

Recaptcha is required.

blog-detail

September 8, 2024 at 02:19 am

Your comment is awaiting moderation.

By using this site, you allow our use of cookies. For more information on the cookies we use and how to delete or block them, please read our cookie notice.

Chat with Us
Telegram Button
Youtube Button
Contact Us

Oodles | Blockchain Development Company

Name is required

Please enter a valid Name

Please enter a valid Phone Number

Please remove URL from text