Skip to content

Instantly share code, notes, and snippets.

@daichan4649
Last active October 28, 2022 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daichan4649/b99026ce1a19f7dd9a7e0c1a69b6dec0 to your computer and use it in GitHub Desktop.
Save daichan4649/b99026ce1a19f7dd9a7e0c1a69b6dec0 to your computer and use it in GitHub Desktop.
Bot2Notion
import { Injectable } from '@nestjs/common';
import { ConfigService as NestConfigService } from '@nestjs/config';
// Notion
import { Client as NotionClient } from '@notionhq/client';
@Injectable()
export class ConfigService {
private readonly config = this.createConfig();
createConfig() {
const configService = new NestConfigService();
const config = {
notion: {
apiKey: configService.get<string>('NOTION_API_KEY'),
pageId: configService.get<string>('NOTION_PAGE_ID'),
databaseId: configService.get<string>('NOTION_DATABASE_ID'),
},
};
return config;
}
// Notion
createNotionClient() {
const notion = this.config.notion;
const apiKey = notion.apiKey;
return new NotionClient({ auth: apiKey });
}
}
import { Injectable } from '@nestjs/common';
import { ConfigService as NestConfigService } from '../config.service';
import { ActivityDto } from 'src/dto/activity.dto';
@Injectable()
export class NotionService {
constructor(private configService: NestConfigService) {}
private readonly notionConfig = this.configService.createConfig().notion;
async add(dto: ActivityDto) {
// Create a page
// https://developers.notion.com/reference/post-page
return this.configService.createNotionClient().pages.create({
parent: { database_id: this.notionConfig.databaseId },
properties: this.getPropertiesFromActivity(dto),
});
}
getPropertiesFromActivity(dto: ActivityDto) {
return {
Title: {
title: [
{
text: {
content: dto.title,
},
},
],
},
Text: {
rich_text: [
{
text: {
content: dto.text,
},
},
],
},
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment