Home Community Blog Buy Now
Blog

Misty Community Forum

Creating a .txt file into a Google Drive?

My Misty is able to interact with users through a general affect questionnaire. With Dialogflow already in use, I am curious if there is a way we can have Misty create a .txt file in Google Drive and be able to append input/output into said .txt file through Drive?

@cp_sridhar has written a few skills that read/write from Google Sheets. I imagine you could also do a Google Doc pretty easily as well. Does it need to be a text file?

If it turned out to be difficult to create/modify a txt file using the Google Drive API API Reference  |  Google Drive API  |  Google Developers you could also try just using a Google Doc and then exporting it at txt file into Drive to keep it in sync. This export could probably be done with the Docs APIs or maybe with Zapier.

It doesn’t have to necessarily be a .txt file, would just like to have a file stored and be able to have read and edit privileges. Will look into the Google Drive and Google Docs APIs and will come back if any further questions persist. @cp_sridhar any chance I could glance at how you were able to do this with Sheets? Have to assume it’ll be very similar.

Hi @kywelch17,

I just uploaded snippets functions to add and pull data from google sheets here. Hope it helps :slight_smile:

2 Likes

Hello CP! Thank you very much for your reply, and appreciate sharing your progress with the Google Sheets integration with Misty. Just curious, if there is anyway to create just a plain sheet through this API and then we can add values as I go along the skill?

Yes you can create sheets / docs directly from the skill and then add data to it.

Here is some documentation on how to create a new sheet.

Request: POST https://sheets.googleapis.com/v4/spreadsheets
Response will include the spreadsheet ID + URL.

There is a “Try this API” pop up window in the link above that lets you look at the Request and Response URL, method and body.

Also here is some very similar documentation for google docs as well:

1 Like

Hi CP,

I am still experiencing a problem here, probably me doing something wrong. I would do

misty.SendExternalRequest("POST", url, authorizationType, accessToken, arguments, false, false, null, "application/json", "GoogleDoc");

and when it runs it tells me: “Request had insignificant authentication scopes.”

I see the scopes in the Google documentation, how would I incorporate that into the SendExternalRequest command?

The scope needs to be incorporated in the place where you actually generate your access token.
How are you currently getting your access token?

I set up a Google Cloud Function that generates a token, and I use a REST API call to get the token. So I have a URL https://us-central1-{google-project-name}-{google-project-id}.cloudfunctions.net/get-access-token, and I have a function:

function _SetAccessToken(data) {
let resp = JSON.parse(data.Result.ResponseObject.Data);
misty.Set('googleAccessToken', resp.accessToken);
}

Pretty similar to the Misty DialogFlow skill in the Misty conversation skill. Should I do a different way of retrieving a token, or is this method workable?

This method is fine :slight_smile:
Add this to your the cloud function…

const auth = new GoogleAuth({
    scopes: ['https://www.googleapis.com/auth/cloud-platform','https://www.googleapis.com/auth/drive','https://www.googleapis.com/auth/drive.file','https://www.googleapis.com/auth/spreadsheets']
  });

You would likely already have this variable (const auth) in there; Just expand the scope to match this.

Let us know if this works !!

I got the request to work! However, whenever I do the request it doesn’t output the JSON response it says in the documentation. It just brings up a bunch of random characters, and no new google doc is created.

That looks like it is base64 encoded…
Try copying that whole string into something like https://www.base64decode.org and decode just to verify that is the data you need.

If that is good, I have uploaded an example function that could help parsing JSON data from base64 string here.

Hello again @cp_sridhar!

I got the response to work, and it now creates a Google Doc. However, I cannot find it on the Google Docs page. Whenever I use the URL, it tells me that I need permission from the owner to view it. Is there any kind of way to change that so I am able to view it?

This works whenever I do the ‘Try this API’ in the Google Docs API Documentation. Just doesn’t work when running it on Misty or when I try to run it on Postman also.

Glad you got the important pieces working!

Regarding permission:

  • If you are creating the access token from a project that is liked to a gmail account as the owner for that project, then this gmail account should be able to access the sheet.

  • I looked through google documentation and came across this thread on the google drive community forum that might help. It discusses this exact situation & steps to add access => how to access the spreadsheet created by google api? - Google Drive Community