Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automated chat example #714

Merged
merged 3 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/examples/automated-chat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# About Automated Chat
Automated chat example shows how you can receive chats when you talk to someone using Push Chat. In this example, we establish a socket connection and then send a message to pushai.eth, which is an automated chat bot that replies back.

## What's the use case
You can use this example to see the functionality of sockets and how you can respond to messages from backend (server) when a wallet messages you (or a wallet you own). Some use cases are:

- Creating an automated support bot
- Creating an AI chat bot

## Install instructions
1. Navigate to this directory from the terminal
2. do `npm install` or `yarn install`
3. do `yarn start`
39 changes: 39 additions & 0 deletions packages/examples/automated-chat/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { PushAPI } from '@pushprotocol/restapi';
import { createSocketConnection, EVENTS } from '@pushprotocol/socket';
import { ethers } from 'ethers';

// Creating a random signer from a wallet, ideally this is the wallet you will connect
const signer = ethers.Wallet.createRandom();

// Initialize wallet user, pass 'prod' instead of 'staging' for mainnet apps
const userAlice = await PushAPI.initialize(signer, { env: 'prod' });

// This will be the wallet address of the recipient
const pushAIWalletAddress = '0x99A08ac6254dcf7ccc37CeC662aeba8eFA666666';

// Create Socket to Listen to incoming messages
const pushSDKSocket = await createSocketConnection({
user: signer.address,
socketType: 'chat',
socketOptions: { autoConnect: true, reconnectionAttempts: 3 },
env: 'prod',
});

pushSDKSocket.on(EVENTS.CONNECT, (message) => {
console.log('Socket Connected');

// Send a message to Bob after socket connection so that messages as an example
console.log('Sending message to PushAI Bot');
const aliceMessagesPushAI = userAlice.chat.send(pushAIWalletAddress, {
content: "Gm gm! It's a me... Mario",
});

});

// React to message payload getting recieved
pushSDKSocket.on(EVENTS.CHAT_RECEIVED_MESSAGE, (message) => {
console.log('Encrypted Message Received');
console.log(message);

pushSDKSocket.disconnect();
});
Loading
Loading