Action

Add To Notion

Posted by moggy1972, Last update 25 days ago

Action that adds current draft to Notion in the page you use as your inbox. If it’s a single line, it’s added as a note, if it’s multiple lines, it’s a page. Currently text is added as one block and it doesn’t convert markdown.

Prompts for your Notion Secret key plus inbox location on first run.

Steps

  • script

    // Variables and Functions for Notion
    
    // Basic variables
    base = "https://api.notion.com/v1/"
    
    //// Secret token
    var credential = Credential.create("Notion", "Notion API");
    credential.addPasswordField("secret", "Secret");
    credential.addTextField("inbox", "Inbox");
    credential.authorize();
    var secret = credential.getValue("secret")
    var inbox = credential.getValue("inbox")
    
    // Functions
    
    function notionClient() {
    	var credential = Credential.create("Notion", "Notion API");
    	credential.addPasswordField("secret", "Secret");
    	credential.authorize();
    
    	//Get token
    	var secret = credential.getValue("secret")
    
    	return secret
    }
    
    function getUsers() {
    	url = base+"users"
    	var http = HTTP.create(); // create HTTP object
    
    	var response = http.request({
      		"url": url,
      		"method": "GET",
      		"headers": {
        	"Authorization": "Bearer " + secret
      		}
    	});
    
    	if (response.success) {
      		var text = response.responseText;
      		var data = response.responseData;
      		var results = JSON.parse(text)
    	}
    	else {
      		console.log(response.statusCode);
      		console.log(response.error);
    	}
    
    return results
    }
    
    function addDatabasePage(database_id, text) {
    	url = base+"pages"
    	body = {	"parent": { "database_id": database_id },"properties": {"Name": {"title": [{"text": {"content": text}}]}}}
    
    	var http = HTTP.create(); // create HTTP object
    
    	var response = http.request({
    		"url": url,
    		"method": "POST",
      		"data": body,
      		"headers": {
      		"Authorization": "Bearer " + secret
      			}
    		});
    
    	if (response.success) {
    		var text = response.responseText;
    		var data = response.responseData;
    		var results = JSON.parse(text)
    	}
    	else {
    		console.log(response.statusCode);
    		console.log(response.error);
    	}
    	return results
    }
    
    function addPage(page_id, text) {
    	url = base+"pages"
    	body = {	"parent": { "page_id": page_id },"properties": {"title": [{"text": {"content": text}}]}}
    
    	var http = HTTP.create(); // create HTTP object
    
    	var response = http.request({
    		"url": url,
    		"method": "POST",
      		"data": body,
      		"headers": {
      		"Authorization": "Bearer " + secret
      			}
    		});
    
    	if (response.success) {
    		var text = response.responseText;
    		var data = response.responseData;
    		var results = JSON.parse(text)
    	}
    	else {
    		console.log(response.statusCode);
    		console.log(response.error);
    	}
    	return results
    }
    
    function addTextBlock(parent_id, text) {
    	url = base+"blocks/" + parent_id + "/children"
    	body = {	"children": [
    					{
    						"object": "block",
    						"type": "paragraph",
    						"paragraph": {
    							"text": [{ "type": "text", "text": { "content": text } }]
    							}
    					}
    				]}
    
    	var http = HTTP.create(); // create HTTP object
    
    	var response = http.request({
    		"url": url,
    		"method": "PATCH",
      		"data": body,
      		"headers": {
      		"Authorization": "Bearer " + secret
      			}
    		});
    
    	if (response.success) {
    		var text = response.responseText;
    		var data = response.responseData;
    		var results = JSON.parse(text)
    	}
    	else {
    		console.log(response.statusCode);
    		console.log(response.error);
    	}
    	return results
    }
    
  • script

    // Test script
    
    //secret = notionClient()
    
    var credential = Credential.create("Notion", "Notion API");
    credential.addPasswordField("inbox", "Inbox");
    credential.authorize();
    var inbox = credential.getValue("inbox")
    
    inbox = "14c0f34131574be1b78c9add79264c31"
    
    var title = draft.processTemplate("[[line|1]]")
    var note = draft.processTemplate("[[line|2..]]")
    
    if (note) {
    	page = addPage(inbox,title)
    	addTextBlock(page.id,note)
    } else {
    	addTextBlock(inbox,title)
    	}

Options

  • After Success Trash
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.