diff --git a/components/surveybot/actions/get-survey-respondent/get-survey-respondent.mjs b/components/surveybot/actions/get-survey-respondent/get-survey-respondent.mjs new file mode 100644 index 0000000000000..5ab93e071e2a5 --- /dev/null +++ b/components/surveybot/actions/get-survey-respondent/get-survey-respondent.mjs @@ -0,0 +1,41 @@ +import surveybot from "../../surveybot.app.mjs"; + +export default { + key: "surveybot-get-survey-respondent", + name: "Get Survey Respondent", + description: "Get a respondent for a survey from SurveyBot. [See the documentation](https://app.surveybot.io/accounts/api_use_cases)", + version: "0.0.1", + type: "action", + annotations: { + destructiveHint: false, + openWorldHint: true, + readOnlyHint: true, + }, + props: { + surveybot, + surveyId: { + propDefinition: [ + surveybot, + "surveyId", + ], + }, + respondentId: { + propDefinition: [ + surveybot, + "respondentId", + ({ surveyId }) => ({ + surveyId, + }), + ], + }, + }, + async run({ $ }) { + const respondent = await this.surveybot.getSurveyRespondent({ + $, + respondentId: this.respondentId, + }); + + $.export("$summary", `Successfully retrieved respondent with ID: "${this.respondentId}" for survey with ID: "${this.surveyId}"`); + return respondent; + }, +}; diff --git a/components/surveybot/actions/get-survey-responses/get-survey-responses.mjs b/components/surveybot/actions/get-survey-responses/get-survey-responses.mjs new file mode 100644 index 0000000000000..a9945a3f81c65 --- /dev/null +++ b/components/surveybot/actions/get-survey-responses/get-survey-responses.mjs @@ -0,0 +1,32 @@ +import surveybot from "../../surveybot.app.mjs"; + +export default { + key: "surveybot-get-survey-responses", + name: "Get Survey Responses", + description: "Get responses for a survey from SurveyBot. [See the documentation](https://app.surveybot.io/accounts/api_use_cases)", + version: "0.0.1", + type: "action", + annotations: { + destructiveHint: false, + openWorldHint: true, + readOnlyHint: true, + }, + props: { + surveybot, + surveyId: { + propDefinition: [ + surveybot, + "surveyId", + ], + }, + }, + async run({ $ }) { + const responses = await this.surveybot.getSurveyResponses({ + $, + surveyId: this.surveyId, + }); + + $.export("$summary", `Successfully retrieved survey responses for survey with ID: "${this.surveyId}"`); + return responses; + }, +}; diff --git a/components/surveybot/actions/list-surveys/list-surveys.mjs b/components/surveybot/actions/list-surveys/list-surveys.mjs new file mode 100644 index 0000000000000..5ad78e273479b --- /dev/null +++ b/components/surveybot/actions/list-surveys/list-surveys.mjs @@ -0,0 +1,42 @@ +import surveybot from "../../surveybot.app.mjs"; + +export default { + key: "surveybot-list-surveys", + name: "List Surveys", + description: "List all surveys from SurveyBot. [See the documentation](https://app.surveybot.io/accounts/api_use_cases)", + version: "0.0.1", + type: "action", + annotations: { + destructiveHint: false, + openWorldHint: true, + readOnlyHint: true, + }, + props: { + surveybot, + maxResults: { + type: "integer", + label: "Max Results", + description: "The maximum number of surveys to return", + default: 100, + optional: true, + }, + }, + async run({ $ }) { + const surveys = this.surveybot.paginate({ + fn: this.surveybot.listSurveys, + $, + max: this.maxResults, + dataField: "surveys", + }); + + const results = []; + for await (const survey of surveys) { + results.push(survey); + } + + $.export("$summary", `Successfully retrieved ${results.length} survey${results.length === 1 + ? "" + : "s"}`); + return results; + }, +}; diff --git a/components/surveybot/package.json b/components/surveybot/package.json index 5474f4c3e2c20..12f406b65d15f 100644 --- a/components/surveybot/package.json +++ b/components/surveybot/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/surveybot", - "version": "0.6.0", + "version": "0.7.0", "description": "Pipedream surveybot Components", "main": "surveybot.app.mjs", "keywords": [ @@ -13,6 +13,6 @@ "access": "public" }, "dependencies": { - "@pipedream/platform": "^3.0.0" + "@pipedream/platform": "^3.1.0" } } diff --git a/components/surveybot/sources/new-survey-response/new-survey-response.mjs b/components/surveybot/sources/new-survey-response/new-survey-response.mjs new file mode 100644 index 0000000000000..9fa486236e7a0 --- /dev/null +++ b/components/surveybot/sources/new-survey-response/new-survey-response.mjs @@ -0,0 +1,70 @@ +import { DEFAULT_POLLING_SOURCE_TIMER_INTERVAL } from "@pipedream/platform"; +import surveybot from "../../surveybot.app.mjs"; +import sampleEmit from "./test-event.mjs"; + +export default { + key: "surveybot-new-survey-response", + name: "New Survey Response", + description: "Emit new event when a new survey response is received. [See the documentation](https://app.surveybot.io/accounts/api_use_cases)", + version: "0.0.1", + type: "source", + dedupe: "unique", + props: { + surveybot, + db: "$.service.db", + timer: { + type: "$.interface.timer", + default: { + intervalSeconds: DEFAULT_POLLING_SOURCE_TIMER_INTERVAL, + }, + }, + surveyId: { + propDefinition: [ + surveybot, + "surveyId", + ], + }, + }, + methods: { + _getLastDate() { + return this.db.get("lastDate") || 0; + }, + _setLastDate(lastDate) { + this.db.set("lastDate", lastDate); + }, + async emitEvent(maxResults = false) { + const lastDate = this._getLastDate(); + const data = await this.surveybot.getSurveyResponses({ + surveyId: this.surveyId, + }); + + let responses = data.responses; + + responses = responses.filter((response) => Date.parse(response.started_at) > lastDate); + + if (responses.length) { + if (maxResults && (responses.length > maxResults)) { + responses.length = maxResults; + } + this._setLastDate(Date.parse(responses[0].started_at)); + } + + for (const item of responses.reverse()) { + this.$emit(item, { + id: item.id, + summary: `New response for survey ${this.surveyId} by ${item.respondent.name}`, + ts: Date.parse(item.started_at), + }); + } + }, + }, + hooks: { + async deploy() { + await this.emitEvent(25); + }, + }, + async run() { + await this.emitEvent(); + }, + sampleEmit, +}; diff --git a/components/surveybot/sources/new-survey-response/test-event.mjs b/components/surveybot/sources/new-survey-response/test-event.mjs new file mode 100644 index 0000000000000..014d6b3f6c55f --- /dev/null +++ b/components/surveybot/sources/new-survey-response/test-event.mjs @@ -0,0 +1,89 @@ +export default { + "completed":true, + "started_at":"03/11/2025 03:45 PM", + "completed_at":"03/11/2025 03:46 PM", + "id":"29360C9E3A5F5E8F61BFE425A3C8BC4E", + "respondent":{ + "id":1789117, + "name":"John Doe", + "first_name":"John", + "last_name":"Doe", + "gender":"male", + "locale":"pt_BR", + "timezone":null, + "email":null, + "picture":null, + "date_of_birth":null, + "occupation":null, + "address":null, + "relationship_status":null, + "phone_number":null, + "mobile_number":null, + "tags":"", + "location":null, + "city":null, + "country":null, + "attributes":[ + + ] + }, + "quiz_score":0, + "answers":[ + { + "question_id":217859, + "question_text":"content removed", + "content":null + }, + { + "question_id":217860, + "question_text":"Which country is the largest producer of rubber?", + "content":"Thailand" + }, + { + "question_id":217861, + "question_text":"content removed", + "content":null + }, + { + "question_id":217862, + "question_text":"Which country is also known as the land of the rising sun?", + "content":"Japan" + }, + { + "question_id":217863, + "question_text":"content removed", + "content":null + }, + { + "question_id":217864, + "question_text":"In which country was Lord of the Rings filmed?", + "content":"New Zealand" + }, + { + "question_id":217865, + "question_text":"content removed", + "content":null + }, + { + "question_id":217866, + "question_text":"What was the name of Alexander's horse?", + "content":"Thunderbolt " + }, + { + "question_id":217867, + "question_text":"content removed", + "content":null + }, + { + "question_id":217868, + "question_text":"Which Shakespeare play does the line ''The lady doth protest too much, methinks'' come from?", + "content":"Macbeth" + }, + { + "question_id":217869, + "question_text":"content removed", + "content":null + } + ], + "shared_by_respondent":null +} \ No newline at end of file diff --git a/components/surveybot/surveybot.app.mjs b/components/surveybot/surveybot.app.mjs index e46adf7e833c7..f900dbfbc3b96 100644 --- a/components/surveybot/surveybot.app.mjs +++ b/components/surveybot/surveybot.app.mjs @@ -1,11 +1,114 @@ +import { axios } from "@pipedream/platform"; + export default { type: "app", app: "surveybot", - propDefinitions: {}, + propDefinitions: { + surveyId: { + type: "string", + label: "Survey ID", + description: "The ID of the survey to get responses for", + async options({ page }) { + const { surveys } = await this.listSurveys({ + params: { + page: page + 1, + }, + }); + return surveys.map(({ + id: value, name: label, + }) => ({ + label, + value, + })); + }, + }, + respondentId: { + type: "string", + label: "Respondent ID", + description: "The ID of the respondent to get", + async options({ surveyId }) { + const { responses } = await this.getSurveyResponses({ + surveyId, + }); + + return responses.map(({ + respondent: { + id: value, + name: label, + }, + }) => ({ + label, + value, + })); + }, + }, + }, methods: { - // this.$auth contains connected account data - authKeys() { - console.log(Object.keys(this.$auth)); + _apiUrl() { + return "https://surveybot.io/api/v1"; + }, + _getHeaders() { + return { + "X-Api-Key": `${this.$auth.api_key}`, + }; + }, + _makeRequest({ + $ = this, path, ...opts + }) { + return axios($, { + url: `${this._apiUrl()}/${path}`, + headers: this._getHeaders(), + ...opts, + }); + }, + listSurveys(args = {}) { + return this._makeRequest({ + path: "surveys", + ...args, + }); + }, + getSurveyResponses({ + surveyId, ...args + }) { + return this._makeRequest({ + path: `surveys/${surveyId}/responses`, + ...args, + }); + }, + getSurveyRespondent({ + respondentId, ...args + }) { + return this._makeRequest({ + path: `respondents/${respondentId}`, + ...args, + }); + }, + async *paginate({ + fn, params = {}, maxResults = null, dataField, ...opts + }) { + let hasMore = false; + let count = 0; + let page = 0; + + do { + params.page = ++page; + const response = await fn({ + params, + ...opts, + }); + + const data = response[dataField]; + for (const d of data) { + yield d; + + if (maxResults && ++count === maxResults) { + return count; + } + } + + hasMore = data.length; + + } while (hasMore); }, }, }; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ef46e344909ae..a28fba5bf51b6 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -3414,8 +3414,7 @@ importers: components/cronly: {} - components/cronlytic: - specifiers: {} + components/cronlytic: {} components/crossmint: {} @@ -3624,8 +3623,7 @@ importers: components/data_police_uk: {} - components/data_soap: - specifiers: {} + components/data_soap: {} components/data_stores: dependencies: @@ -3733,13 +3731,11 @@ importers: specifier: ^1.5.1 version: 1.6.6 - components/decodo: - specifiers: {} + components/decodo: {} components/deel: {} - components/deep_tagger: - specifiers: {} + components/deep_tagger: {} components/deepgram: dependencies: @@ -3834,8 +3830,7 @@ importers: specifier: ^0.3.2 version: 0.3.2 - components/deutschlandgpt: - specifiers: {} + components/deutschlandgpt: {} components/dev_to: dependencies: @@ -4110,8 +4105,7 @@ importers: specifier: ^3.1.0 version: 3.1.0 - components/documentero: - specifiers: {} + components/documentero: {} components/documenterra: dependencies: @@ -4365,8 +4359,7 @@ importers: specifier: ^1.5.1 version: 1.6.6 - components/dumplingai: - specifiers: {} + components/dumplingai: {} components/dungeon_fighter_online: {} @@ -4433,8 +4426,7 @@ importers: components/easy_projects: {} - components/easy_redmine: - specifiers: {} + components/easy_redmine: {} components/easybroker: {} @@ -4481,8 +4473,7 @@ importers: components/ebay: {} - components/echowin: - specifiers: {} + components/echowin: {} components/echtpost_postcards: dependencies: @@ -4803,8 +4794,7 @@ importers: specifier: ^1.1.1 version: 1.6.6 - components/evervault: - specifiers: {} + components/evervault: {} components/everwebinar: dependencies: @@ -4871,8 +4861,7 @@ importers: components/extracta_ai: {} - components/extruct_ai: - specifiers: {} + components/extruct_ai: {} components/eyepop_ai: {} @@ -4986,8 +4975,7 @@ importers: specifier: ^3.0.3 version: 3.0.3 - components/featherless: - specifiers: {} + components/featherless: {} components/feathery: dependencies: @@ -5056,8 +5044,7 @@ importers: specifier: ^3.0.0 version: 3.0.3 - components/filescan: - specifiers: {} + components/filescan: {} components/filestack: dependencies: @@ -5995,8 +5982,7 @@ importers: components/google_chat_developer_app: {} - components/google_chat_service_account_key: - specifiers: {} + components/google_chat_service_account_key: {} components/google_classroom: dependencies: @@ -6499,8 +6485,7 @@ importers: components/hana: {} - components/handelsregister_ai: - specifiers: {} + components/handelsregister_ai: {} components/handwrytten: {} @@ -6548,8 +6533,7 @@ importers: specifier: ^2.29.4 version: 2.30.1 - components/hasdata: - specifiers: {} + components/hasdata: {} components/hashnode: {} @@ -7382,8 +7366,7 @@ importers: specifier: ^1.1.1 version: 1.6.6 - components/ipregistry: - specifiers: {} + components/ipregistry: {} components/ipstack: dependencies: @@ -8237,8 +8220,7 @@ importers: specifier: ^1.0.3 version: 1.0.3 - components/linkupapi: - specifiers: {} + components/linkupapi: {} components/linode: dependencies: @@ -14224,8 +14206,8 @@ importers: components/surveybot: dependencies: '@pipedream/platform': - specifier: ^3.0.0 - version: 3.0.3 + specifier: ^3.1.0 + version: 3.1.0 components/surveycto: dependencies: @@ -31542,22 +31524,22 @@ packages: superagent@3.8.1: resolution: {integrity: sha512-VMBFLYgFuRdfeNQSMLbxGSLfmXL/xc+OO+BZp41Za/NRDBet/BNbkRJrYzCUu0u4GU0i/ml2dtT8b9qgkw9z6Q==} engines: {node: '>= 4.0'} - deprecated: Please upgrade to superagent v10.2.2+, see release notes at https://github.com/forwardemail/superagent/releases/tag/v10.2.2 - maintenance is supported by Forward Email @ https://forwardemail.net + deprecated: Please upgrade to v9.0.0+ as we have fixed a public vulnerability with formidable dependency. Note that v9.0.0+ requires Node.js v14.18.0+. See https://github.com/ladjs/superagent/pull/1800 for insight. This project is supported and maintained by the team at Forward Email @ https://forwardemail.net superagent@4.1.0: resolution: {integrity: sha512-FT3QLMasz0YyCd4uIi5HNe+3t/onxMyEho7C3PSqmti3Twgy2rXT4fmkTz6wRL6bTF4uzPcfkUCa8u4JWHw8Ag==} engines: {node: '>= 6.0'} - deprecated: Please upgrade to superagent v10.2.2+, see release notes at https://github.com/forwardemail/superagent/releases/tag/v10.2.2 - maintenance is supported by Forward Email @ https://forwardemail.net + deprecated: Please upgrade to v9.0.0+ as we have fixed a public vulnerability with formidable dependency. Note that v9.0.0+ requires Node.js v14.18.0+. See https://github.com/ladjs/superagent/pull/1800 for insight. This project is supported and maintained by the team at Forward Email @ https://forwardemail.net superagent@5.3.1: resolution: {integrity: sha512-wjJ/MoTid2/RuGCOFtlacyGNxN9QLMgcpYLDQlWFIhhdJ93kNscFonGvrpAHSCVjRVj++DGCglocF7Aej1KHvQ==} engines: {node: '>= 7.0.0'} - deprecated: Please upgrade to superagent v10.2.2+, see release notes at https://github.com/forwardemail/superagent/releases/tag/v10.2.2 - maintenance is supported by Forward Email @ https://forwardemail.net + deprecated: Please upgrade to v9.0.0+ as we have fixed a public vulnerability with formidable dependency. Note that v9.0.0+ requires Node.js v14.18.0+. See https://github.com/ladjs/superagent/pull/1800 for insight. This project is supported and maintained by the team at Forward Email @ https://forwardemail.net superagent@7.1.6: resolution: {integrity: sha512-gZkVCQR1gy/oUXr+kxJMLDjla434KmSOKbx5iGD30Ql+AkJQ/YlPKECJy2nhqOsHLjGHzoDTXNSjhnvWhzKk7g==} engines: {node: '>=6.4.0 <13 || >=14'} - deprecated: Please upgrade to superagent v10.2.2+, see release notes at https://github.com/forwardemail/superagent/releases/tag/v10.2.2 - maintenance is supported by Forward Email @ https://forwardemail.net + deprecated: Please upgrade to v9.0.0+ as we have fixed a public vulnerability with formidable dependency. Note that v9.0.0+ requires Node.js v14.18.0+. See https://github.com/ladjs/superagent/pull/1800 for insight. This project is supported and maintained by the team at Forward Email @ https://forwardemail.net supports-color@10.0.0: resolution: {integrity: sha512-HRVVSbCCMbj7/kdWF9Q+bbckjBHLtHMEoJWlkmYzzdwhYMkjkOwubLM6t7NbWKjgKamGDrWL1++KrjUO1t9oAQ==}