Skip to content

Commit

Permalink
fix: merged main
Browse files Browse the repository at this point in the history
  • Loading branch information
akp111 committed Nov 15, 2023
2 parents 3b06ae1 + 7d9912c commit 45b90c3
Show file tree
Hide file tree
Showing 41 changed files with 13,974 additions and 1,104 deletions.
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
echo "\nRunning GIT hooks..."
yarn cleanbuild
yarn nx affected --target=lint
yarn nx affected --target=test
#yarn nx affected --target=test
56 changes: 37 additions & 19 deletions packages/examples/automated-chat/index.js
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();
2 changes: 1 addition & 1 deletion packages/examples/automated-chat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@pushprotocol/restapi": "1.4.19",
"@pushprotocol/restapi": "1.4.35",
"@pushprotocol/socket": "0.5.2",
"ethers": "5.7.2"
},
Expand Down
54 changes: 43 additions & 11 deletions packages/examples/sdk-backend-node/chat/chat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PushAPI } from '@pushprotocol/restapi';
import { CONSTANTS, PushAPI } from '@pushprotocol/restapi';
import {
adjectives,
animals,
Expand All @@ -10,6 +10,7 @@ import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts';
import { createWalletClient, http } from 'viem';
import { sepolia } from 'viem/chains';
import { STREAM } from '@pushprotocol/restapi/src/lib/pushstream/pushStreamTypes';
import { PushStream } from '@pushprotocol/restapi/src/lib/pushstream/PushStream';

// CONFIGS
const { env, showAPIResponse } = config;
Expand Down Expand Up @@ -56,10 +57,10 @@ const groupImage =
const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

const eventlistener = async (
pushAPI: PushAPI,
stream: PushStream,
eventName: string
): Promise<void> => {
pushAPI.stream.on(eventName, (data: any) => {
stream.on(eventName, (data: any) => {
if (showAPIResponse) {
console.log('Stream Event Received');
console.log(data);
Expand All @@ -70,21 +71,52 @@ const eventlistener = async (

export const runChatClassUseCases = async (): Promise<void> => {
const userAlice = await PushAPI.initialize(signer, { env });

const stream = await userAlice.initStream(
[CONSTANTS.STREAM.CHAT, CONSTANTS.STREAM.CHAT_OPS],
{
// stream supports other products as well, such as STREAM.CHAT, STREAM.CHAT_OPS
// more info can be found at push.org/docs/chat

filter: {
channels: ['*'],
chats: ['*'],
},
connection: {
auto: true, // should connection be automatic, else need to call stream.connect();
retries: 3, // number of retries in case of error
},
raw: true, // enable true to show all data
}
);

stream.on(CONSTANTS.STREAM.CONNECT, (a) => {
console.log('Stream Connected');
});

await stream.connect();

stream.on(CONSTANTS.STREAM.DISCONNECT, () => {
console.log('Stream Disconnected');
});

const userBob = await PushAPI.initialize(secondSigner, { env });
const userKate = await PushAPI.initialize(thirdSigner, { env });

// Listen stream events to receive websocket events
console.log(`Listening ${STREAM.CHAT} Events`);
eventlistener(userAlice, STREAM.CHAT);
console.log(`Listening ${STREAM.CHAT_OPS} Events`);
eventlistener(userAlice, STREAM.CHAT_OPS);
console.log(`Listening ${CONSTANTS.STREAM.CHAT} Events`);
eventlistener(stream, CONSTANTS.STREAM.CHAT);
console.log(`Listening ${CONSTANTS.STREAM.CHAT_OPS} Events`);
eventlistener(stream, CONSTANTS.STREAM.CHAT_OPS);
console.log('\n\n');

// -------------------------------------------------------------------
// -------------------------------------------------------------------
console.log('PushAPI.chat.list');
const aliceChats = await userAlice.chat.list('CHATS');
const aliceRequests = await userAlice.chat.list('REQUESTS');
const aliceChats = await userAlice.chat.list(CONSTANTS.CHAT.LIST_TYPE.CHATS);
const aliceRequests = await userAlice.chat.list(
CONSTANTS.CHAT.LIST_TYPE.REQUESTS
);
if (showAPIResponse) {
console.log(aliceChats);
console.log(aliceRequests);
Expand Down Expand Up @@ -115,7 +147,7 @@ export const runChatClassUseCases = async (): Promise<void> => {
console.log('PushAPI.chat.send');
const aliceMessagesBob = await userAlice.chat.send(secondSignerAddress, {
content: 'Hello Bob!',
type: 'Text',
type: CONSTANTS.CHAT.MESSAGE_TYPE.TEXT,
});
if (showAPIResponse) {
console.log(aliceMessagesBob);
Expand All @@ -136,7 +168,7 @@ export const runChatClassUseCases = async (): Promise<void> => {
console.log('PushAPI.chat.reject');
await userKate.chat.send(signerAddress, {
content: 'Sending malicious message',
type: 'Text',
type: CONSTANTS.CHAT.MESSAGE_TYPE.TEXT,
});
const AliceRejectsRequest = await userAlice.chat.reject(thirdSignerAddress);
if (showAPIResponse) {
Expand Down
45 changes: 40 additions & 5 deletions packages/examples/sdk-backend-node/notification/notification.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
import { PushAPI } from '@pushprotocol/restapi';
import { CONSTANTS, PushAPI } from '@pushprotocol/restapi';
import { config } from '../config';
import { ethers } from 'ethers';
import { STREAM } from '@pushprotocol/restapi/src/lib/pushstream/pushStreamTypes';
import { PushStream } from '@pushprotocol/restapi/src/lib/pushstream/PushStream';

// CONFIGS
const { env, showAPIResponse } = config;

const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

const eventlistener = async (
pushAPI: PushAPI,
stream: PushStream,
eventName: string
): Promise<void> => {
pushAPI.stream.on(eventName, (data: any) => {
stream.on(eventName, (data: any) => {
if (showAPIResponse) {
console.log('Stream Event Received');
console.log(data);
Expand Down Expand Up @@ -44,11 +45,45 @@ export const runNotificationClassUseCases = async (): Promise<void> => {
// -------------------------------------------------------------------
const userAlice = await PushAPI.initialize(signer, { env });

const stream = await userAlice.initStream(
[
CONSTANTS.STREAM.NOTIF,
CONSTANTS.STREAM.CHAT_OPS,
CONSTANTS.STREAM.CHAT,
CONSTANTS.STREAM.CONNECT,
CONSTANTS.STREAM.DISCONNECT,
],
{
// stream supports other products as well, such as STREAM.CHAT, STREAM.CHAT_OPS
// more info can be found at push.org/docs/chat

filter: {
channels: ['*'],
chats: ['*'],
},
connection: {
auto: true, // should connection be automatic, else need to call stream.connect();
retries: 3, // number of retries in case of error
},
raw: true, // enable true to show all data
}
);

stream.on(CONSTANTS.STREAM.CONNECT, (a) => {
console.log('Stream Connected');
});

await stream.connect();

stream.on(CONSTANTS.STREAM.DISCONNECT, () => {
console.log('Stream Disconnected');
});

// Listen Stream Events for getting websocket events
console.log(`Listening ${STREAM.NOTIF} Events`);
eventlistener(userAlice, STREAM.NOTIF);
eventlistener(stream, STREAM.NOTIF);
console.log(`Listening ${STREAM.NOTIF_OPS} Events`);
eventlistener(userAlice, STREAM.NOTIF_OPS);
eventlistener(stream, STREAM.NOTIF_OPS);
console.log('\n\n');
// -------------------------------------------------------------------
// -------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion packages/examples/sdk-backend-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@pushprotocol/restapi": "0.0.1-alpha.50",
"@pushprotocol/restapi": "1.4.35",
"@pushprotocol/socket": "^0.5.2"
}
}
6 changes: 3 additions & 3 deletions packages/examples/sdk-backend-node/pushAPI/chat.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PushAPI } from '@pushprotocol/restapi';
import { CONSTANTS, PushAPI } from '@pushprotocol/restapi';
import {
adjectives,
animals,
Expand Down Expand Up @@ -91,7 +91,7 @@ export const runPushAPIChatCases = async (): Promise<void> => {
console.log('PushAPI.chat.send');
const aliceMessagesBob = await userAlice.chat.send(secondSignerAddress, {
content: 'Hello Bob!',
type: 'Text',
type: CONSTANTS.CHAT.MESSAGE_TYPE.TEXT,
});
if (showAPIResponse) {
console.log(aliceMessagesBob);
Expand All @@ -110,7 +110,7 @@ export const runPushAPIChatCases = async (): Promise<void> => {
console.log('PushAPI.chat.reject');
await tempUser.chat.send(secondSignerAddress, {
content: 'Sending malicious message',
type: 'Text',
type: CONSTANTS.CHAT.MESSAGE_TYPE.TEXT,
});
const bobRejectsRequest = await userBob.chat.reject(thirdSignerAddress);
if (showAPIResponse) {
Expand Down
39 changes: 37 additions & 2 deletions packages/examples/sdk-backend-node/pushAPI/stream.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PushAPI } from '@pushprotocol/restapi';
import { CONSTANTS, PushAPI } from '@pushprotocol/restapi';
import { config } from '../config';
import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts';
import { createWalletClient, http } from 'viem';
Expand Down Expand Up @@ -47,11 +47,46 @@ const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

export const runPushAPIStreamCases = async (): Promise<void> => {
const userAlice = await PushAPI.initialize(signer, { env });

const stream = await userAlice.initStream(
[
CONSTANTS.STREAM.NOTIF,
CONSTANTS.STREAM.CHAT_OPS,
CONSTANTS.STREAM.CHAT,
CONSTANTS.STREAM.CONNECT,
CONSTANTS.STREAM.DISCONNECT,
],
{
// stream supports other products as well, such as STREAM.CHAT, STREAM.CHAT_OPS
// more info can be found at push.org/docs/chat

filter: {
channels: ['*'],
chats: ['*'],
},
connection: {
auto: true, // should connection be automatic, else need to call stream.connect();
retries: 3, // number of retries in case of error
},
raw: true, // enable true to show all data
}
);

stream.on(CONSTANTS.STREAM.CONNECT, (a) => {
console.log('Stream Connected');
});

await stream.connect();

stream.on(CONSTANTS.STREAM.DISCONNECT, () => {
console.log('Stream Disconnected');
});

const userBob = await PushAPI.initialize(secondSigner, { env });
const userKate = await PushAPI.initialize(thirdSigner, { env });
// -------------------------------------------------------------------
// -------------------------------------------------------------------
console.log(`Listening ${STREAM.PROFILE} Events`);
console.log(`Listening ${CONSTANTS.STREAM.PROFILE} Events`);
eventlistener(userAlice, STREAM.PROFILE);
console.log(`Listening ${STREAM.ENCRYPTION} Events`);
eventlistener(userAlice, STREAM.ENCRYPTION);
Expand Down
13 changes: 13 additions & 0 deletions packages/examples/stream-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`
Loading

0 comments on commit 45b90c3

Please sign in to comment.