-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
247 changed files
with
35,823 additions
and
7,648 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
Oops, something went wrong.