Skip to content

Commit

Permalink
fix: merge conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
strykerin committed Sep 19, 2023
2 parents 154dbe1 + 880a181 commit 6729e14
Show file tree
Hide file tree
Showing 247 changed files with 35,823 additions and 7,648 deletions.
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

0 comments on commit 6729e14

Please sign in to comment.