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

feat(CDF-3933): Add Vercel support to discord bot implementation #308

Merged
merged 4 commits into from
Oct 31, 2024
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
1 change: 1 addition & 0 deletions quickstart-template/discord_tutorial/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vercel
43 changes: 43 additions & 0 deletions quickstart-template/discord_tutorial/app.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require("dotenv/config");
const express = require("express");
const axios = require("axios");
const bodyParser = require('body-parser')

const app = express();
const jsonParser = bodyParser.json()

app.get("/", jsonParser, (req, res) => {
res.send("Your https server is working!");
});

app.post("/", jsonParser, (req, res) => {
if (!process.env.DISCORD_URL) {
console.log('DISCORD_URL is missing from env');
res.sendStatus(400);
return;
}

const data = req.body;

let messageContent = 'A new ' + data.eventType + ' event was received from the webhook: \n```'
messageContent += JSON.stringify(data, null, 2)
messageContent += '```\n'
messageContent += `Data received at ${new Date().toLocaleString("en-US")}`

const postData = {
content: messageContent,
}
axios.post(process.env.DISCORD_URL, postData).then(() => {
console.log('Successfully posted message to discord');
res.sendStatus(200);
}).catch((e) => {
console.error(e)
res.sendStatus(400);
})
});

app.listen(5000, () => {
console.log("Running on port 5000.");
});

module.exports = app;
34 changes: 0 additions & 34 deletions quickstart-template/discord_tutorial/app.js

This file was deleted.

21 changes: 11 additions & 10 deletions quickstart-template/discord_tutorial/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions quickstart-template/discord_tutorial/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,19 @@
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start-server": "node app.js",
"start-server": "node app.cjs",
"start-transfer": "node webhook-transfer.js",
"start-pinggy": "ssh -p 443 -R0:localhost:3000 -L4300:localhost:4300 a.pinggy.io"
"start-pinggy": "ssh -p 443 -R0:localhost:5000 a.pinggy.io"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"type": "module",
"dependencies": {
"@coinbase/coinbase-sdk": "^0.9.1",
"@coinbase/coinbase-sdk": "^0.10.0",
"axios": "^1.7.7",
"body-parser": "^1.20.3",
"dotenv": "^16.4.5",
"express": "^4.21.1"
}
Expand Down
16 changes: 16 additions & 0 deletions quickstart-template/discord_tutorial/vercel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"version": 2,
"builds": [
{
"src": "app.cjs",
"use": "@now/node"
}
],
"routes": [

{
"src": "/(.*)",
"dest": "app.cjs"
}
]
}
23 changes: 13 additions & 10 deletions quickstart-template/discord_tutorial/webhook-transfer.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ const webhookNotificationUri = process.env.WEBHOOK_NOTIFICATION_URL;
console.log(`💰 Wallet USDC balance:`, balance);
if (balance <= 0) {
// If wallet doesn't have funds we need to add funds to it
await myWallet.faucet(Coinbase.assets.Usdc);
const faucetTx = await myWallet.faucet(Coinbase.assets.Usdc);

// Wait for the faucet transaction to confirm.
await faucetTx.wait();

console.log("✅ Funds added!");

// Sometimes funds take a few seconds to be available on the wallet, so lets wait 5 secs
Expand All @@ -46,21 +50,20 @@ const webhookNotificationUri = process.env.WEBHOOK_NOTIFICATION_URL;
const myWalletAddressId = myWalletAddress.getId();

console.log('💳 myWallet address: ', myWalletAddressId);
const webhookConfig = {
networkId: Coinbase.networks.BaseSepolia,
notificationUri: webhookNotificationUri,
eventType: 'wallet_activity',
eventTypeFilter: {
addresses: [myWalletAddressId],
},
}

const webhooks = await Webhook.list()
let shouldCreateWebhook = !webhookAlreadyExists(webhooks)

if (shouldCreateWebhook) {
console.log("🔄 Creating webhook...");
await Webhook.create(webhookConfig);
await Webhook.create({
networkId: Coinbase.networks.BaseSepolia,
notificationUri: webhookNotificationUri,
eventType: 'wallet_activity',
eventTypeFilter: {
addresses: [myWalletAddressId],
},
});
console.log("✅ Webhook created!");
} else {
console.log("⏩ Skipping Webhook creation...");
Expand Down
Loading