Last active
October 28, 2022 14:03
-
-
Save daichan4649/b99026ce1a19f7dd9a7e0c1a69b6dec0 to your computer and use it in GitHub Desktop.
Bot2Notion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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