Skip to content

Company Info Helper #411

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

Merged
merged 6 commits into from
Nov 25, 2022
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
3 changes: 2 additions & 1 deletion config/vars.template.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
"OFFICE_STATUS_CHANNEL_ID": "CHANNEL_ID",
"RESUME_CHANNEL_ID": "CHANNEL_ID",
"IRC_USER_ID": "USER_ID",
"MOD_USER_ID_FOR_BAN_APPEAL": "USER_ID"
"MOD_USER_ID_FOR_BAN_APPEAL": "USER_ID",
"CRUNCHBASE_API_KEY": "CRUNCHBASE_KEY"
}
4 changes: 2 additions & 2 deletions docs/SETUP.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ Then, add your bot to the Discord testing server.
- `OFFICE_STATUS_CHANNEL_ID`: the ID of the office hours channel.
- `IRC_USER_ID`: the user ID of the irc-bridge bot.
- `MOD_USER_ID_FOR_BAN_APPEAL`: the user ID of the mod tagged in the appeal messages for bans.

Note that this file will not be pushed to the remote.
- `CRUNCHBASE_API_KEY`: API key if you wish to use the upcoming enroll company feature. (Create an account here)[https://www.crunchbase.com/home]. NOTE: feature is still under construction.
Note that this file will not be pushed to the remote.

6. Make an `.env` file in the root folder of the project, and put your Discord bot's token, which can be found in the Discord Developer Portal. The format of the `.env` file should be as follows.

Expand Down
37 changes: 37 additions & 0 deletions src/utils/companyInfo.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { vars } from '../config';
import { isValidUrl } from './validateUrl';
import fetch from 'node-fetch';

const CRUNCHBASE_ORGANIZATION_URL = 'crunchbase.com/organization/';
const CRUNCHBASE_ORGANIZATION_API_URL = 'https://api.crunchbase.com/api/v4/entities/organizations/';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const getCompanyInfo = async (companyCrunchbaseLink: string) => {
// check if url contains crunchbase.com/organization
// if not, throw error
if (
isValidUrl(companyCrunchbaseLink) &&
!companyCrunchbaseLink.includes(CRUNCHBASE_ORGANIZATION_URL)
) {
throw new Error('Invalid URL');
}

let companyName;
if (companyCrunchbaseLink.includes(CRUNCHBASE_ORGANIZATION_URL)) {
// grab everything after the last / character
companyName = companyCrunchbaseLink.split('/').pop();
} else {
// assume they just supplied the id if we can't find the CRUNCHBASE_ORGANIZATION_URL
companyName = companyCrunchbaseLink;
}

const response = await fetch(
`${CRUNCHBASE_ORGANIZATION_API_URL}${companyName}?user_key=${vars.CRUNCHBASE_API_KEY}&field_ids=categories,short_description`,
);
const data = await response.json();
if (data.error) {
throw new Error(data.error);
}

return data;
};
12 changes: 12 additions & 0 deletions src/utils/validateUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const isValidUrl = (url: string): boolean => {
const urlPattern = new RegExp(
'^(https?:\\/\\/)?' + // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|' + // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path
'(\\?[;&a-z\\d%_.~+=-]*)?' + // query string
'(\\#[-a-z\\d_]*)?$',
'i',
); // fragment locator
return urlPattern.test(url);
};