Skip to content

Commit

Permalink
feat: Zapier integration
Browse files Browse the repository at this point in the history
  • Loading branch information
gmpetrov committed Jun 10, 2023
1 parent 807391e commit 35e9b7c
Show file tree
Hide file tree
Showing 17 changed files with 1,622 additions and 0 deletions.
9 changes: 9 additions & 0 deletions widgets/zappier/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
API_KEY=XXX
TEST_DATASTORE_ID=XXX
TEST_AGENT_ID=XXX
API_URL=http://localhost:3000

# OAUTH_CONSUMER_KEY=YOUR_OAUTH_CONSUMER_KEY
# OAUTH_CONSUMER_SECRET=YOUR_OAUTH_CONSUMER_SECRET
# OAUTH_TOKEN=YOUR_OAUTH_TOKEN
# OAUTH_TOKEN_SECRET=YOUR_OAUTH_TOKEN_SECRET
62 changes: 62 additions & 0 deletions widgets/zappier/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/

# Dependency directories
node_modules/
jspm_packages/

# Typescript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# environment variables file
.env
.environment

# next.js build output
.next
4 changes: 4 additions & 0 deletions widgets/zappier/.zapierapprc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"id": 185359,
"key": "App185359"
}
28 changes: 28 additions & 0 deletions widgets/zappier/authentication.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const baseApiUrl = process.env.API_URL
? process.env.API_URL
: 'https://api.databerry.ai';

module.exports = {
type: 'custom',
test: {
headers: { Authorization: 'Bearer {{bundle.authData.api_key}}' },
removeMissingValuesFrom: { params: true },
url: `${baseApiUrl}/api/external/me`,
},
connectionLabel: (z, bundle) => {
const { email } = bundle.inputData;
return `User: ${email}`;
},
fields: [
{
computed: false,
key: 'api_key',
required: false,
label: 'Databerry API Key',
type: 'password',
helpText:
'Your Databerry API Key can be found here https://app.databerry.ai/account',
},
],
customConfig: {},
};
81 changes: 81 additions & 0 deletions widgets/zappier/creates/datastore_file_upload.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const http = require('https'); // require('http') if your URL is not https

const FormData = require('form-data');

const zapier = require('zapier-platform-core');

zapier.tools.env.inject();

const baseApiUrl = process.env.API_URL
? process.env.API_URL
: 'https://api.databerry.ai';

// Getting a stream directly from http. This only works on core 10+. For core
// 9.x compatible code, see uploadFile_v9.js.
const makeDownloadStream = (url) =>
new Promise((resolve, reject) => {
http
.request(url, (res) => {
// We can risk missing the first n bytes if we don't pause!
res.pause();
resolve(res);
})
.on('error', reject)
.end();
});

const perform = async (z, bundle) => {
// bundle.inputData.file will in fact be an URL where the file data can be
// downloaded from which we do via a stream
const stream = await makeDownloadStream(bundle.inputData.file, z);

const form = new FormData();
form.append('file', stream);
form.append('fileName', bundle.inputData.fileName || '');

// All set! Resume the stream
stream.resume();

console.log('bundle.authData.api_key', bundle.authData);

const response = await z.request({
url: `${baseApiUrl}/api/external/datastores/file-upload/${bundle.inputData.datastore_id}`,
method: 'POST',
body: form,
headers: {
Authorization: `Bearer ${bundle.authData.api_key}`,
},
});

return response.data;
};

module.exports = {
key: 'datastore_file_upload',
noun: 'File',
display: {
label: 'Upload File',
description: 'Upload a new file to one of your Datastore.',
},
operation: {
inputFields: [
{
key: 'datastore_id',
label: 'Datastore ID',
type: 'string',
helpText: 'The ID of your Datastore',
dynamic: 'list_datastores.id.name',
required: true,
list: false,
altersDynamicFields: false,
},
{ key: 'fileName', required: false, type: 'string', label: 'File Name' },
{ key: 'file', required: true, type: 'file', label: 'File' },
],
perform,
sample: {
file: 'SAMPLE FILE',
fileName: 'test.pdf',
},
},
};
56 changes: 56 additions & 0 deletions widgets/zappier/creates/query_agent.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const zapier = require('zapier-platform-core');

zapier.tools.env.inject();

const baseApiUrl = process.env.API_URL
? process.env.API_URL
: 'https://api.databerry.ai';

module.exports = {
display: {
description: 'Query one of your Agent',
hidden: false,
label: 'Query Agent',
},
key: 'query_agent',
noun: 'Agent',
operation: {
inputFields: [
{
key: 'agent_id',
label: 'Agent ID',
type: 'string',
helpText: 'The ID of your Agent',
dynamic: 'list_agents.id.name',
required: true,
list: false,
altersDynamicFields: false,
},
{
key: 'query',
label: 'Query',
type: 'string',
helpText: 'The query to send to the Agent',
required: true,
list: false,
altersDynamicFields: false,
},
],
perform: {
body: { query: '{{bundle.inputData.query}}' },
headers: {
'Content-Type': 'application/json',
Accept: 'application/json',
Authorization: 'Bearer {{bundle.authData.api_key}}',
},
method: 'POST',
url: `${baseApiUrl}/api/external/agents/{{bundle.inputData.agent_id}}/query`,
},
sample: {
query: 'Hello',
answer:
'Hello! How may I assist you today? Please let me know if you have any questions or concerns related to our services or policies. If you need more information about our chat site app, you can check out our documentation at https://docs.databerry.ai/apps/chat-site. Additionally, if you have any questions about our privacy policy, you can find it at https://www.databerry.ai/privacy. Please let me know if there is anything else I can help you with.',
},
outputFields: [{ key: 'answer', label: 'Answer', type: 'string' }],
},
};
23 changes: 23 additions & 0 deletions widgets/zappier/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const authentication = require('./authentication');
const listAgentsTrigger = require('./triggers/list_agents.js');
const listDatastoresTrigger = require('./triggers/list_datastores.js');
const queryAgentCreate = require('./creates/query_agent.js');
const queryDatastoreSearch = require('./searches/query_datastore.js');
const datastoreFileUpload = require('./creates/datastore_file_upload.js');

module.exports = {
version: require('./package.json').version,
platformVersion: require('zapier-platform-core').version,
authentication: authentication,
searches: {
[queryDatastoreSearch.key]: queryDatastoreSearch,
},
creates: {
[queryAgentCreate.key]: queryAgentCreate,
[datastoreFileUpload.key]: datastoreFileUpload,
},
triggers: {
[listAgentsTrigger.key]: listAgentsTrigger,
[listDatastoresTrigger.key]: listDatastoresTrigger,
},
};
Loading

1 comment on commit 35e9b7c

@vercel
Copy link

@vercel vercel bot commented on 35e9b7c Jun 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.