-
Notifications
You must be signed in to change notification settings - Fork 53
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
41 changed files
with
13,974 additions
and
1,104 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
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 |
---|---|---|
@@ -1,39 +1,57 @@ | ||
import { PushAPI } from '@pushprotocol/restapi'; | ||
import { createSocketConnection, EVENTS } from '@pushprotocol/socket'; | ||
import { CONSTANTS, PushAPI } from '@pushprotocol/restapi'; | ||
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' }); | ||
const userAlice = await PushAPI.initialize(signer, { env: CONSTANTS.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', | ||
}); | ||
// IMPORTANT: Setup stream events before stream.connect() | ||
// Checkout all chat stream listen options - https://push.org/docs/chat/build/stream-chats/ | ||
const stream = await userAlice.initStream( | ||
[ | ||
CONSTANTS.STREAM.CHAT, | ||
CONSTANTS.STREAM.CONNECT, | ||
CONSTANTS.STREAM.DISCONNECT, | ||
] | ||
); | ||
|
||
// Setup responder for CONSTANTS.STREAM.CONNECT event | ||
stream.on(CONSTANTS.STREAM.CONNECT, () => { | ||
console.log('Stream Connected'); | ||
|
||
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, { | ||
userAlice.chat.send(pushAIWalletAddress, { | ||
content: "Gm gm! It's a me... Mario", | ||
}); | ||
|
||
}); | ||
|
||
// Setup responder for CONSTANTS.STREAM.DISCONNECT event | ||
stream.on(CONSTANTS.STREAM.DISCONNECT, () => { | ||
console.log('Stream Disconnected'); | ||
}); | ||
|
||
// Setup responder for CONSTANTS.STREAM.CHAT event | ||
// React to message payload getting recieved | ||
pushSDKSocket.on(EVENTS.CHAT_RECEIVED_MESSAGE, (message) => { | ||
console.log('Encrypted Message Received'); | ||
stream.on(CONSTANTS.STREAM.CHAT, (message) => { | ||
console.log('Message Received'); | ||
console.log(message); | ||
|
||
pushSDKSocket.disconnect(); | ||
if (message.origin === 'self') { | ||
console.log("Message sent by your wallet, please wait for few moments for PushAI response"); | ||
} | ||
if (message.origin === 'other') { | ||
console.log("Message received by PushAI.eth"); | ||
|
||
// disconnect stream | ||
stream.disconnect(); | ||
} | ||
}); | ||
|
||
// now that response logic for streams are setup | ||
// finally connect stream | ||
await stream.connect(); |
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
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
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
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
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
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
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` |
Oops, something went wrong.