1

I am not able to parse JSON using Google Apps. The payload is coming from Microsoft Teams. When I dump the payload info using json.postData.getDataAsString() I receive the below:

{"type":"message",
  "Id":"1490878296446",
  "timestamp":"2017-03-30T12:51:36.857Z",
  "localTimestamp":null,
  "serviceUrl":"https://smba.trafficmanager.net/amer-client-ss.msg/",
  "channelId":"msteams",
  "from":{"id":"394724749272","name":"Test, User"},
  "conversation":{"isGroup":true,"id":"39482095823@thread.skype;messageid=1490878296446","name":null},
  "Recipient":null,
  "textFormat":"plain",
  "attachmentLayout":null,
  "membersAdded":null,
  "membersRemoved":null,
  "topicName":null,
  "historyDisclosed":null,
  "Locale":null,
  "text":"Test message",
  "Summary":null,
  "attachments":[{"contentType":"text/html","contentUrl":null,"content":"<div><span contenteditable=\"false\" itemscope=\"\" itemtype=\"http://schema.skype.com/Mention\" itemid=\"0\">Testing</span><span contenteditable=\"false\" itemscope=\"\" itemtype=\"http://schema.skype.com/Mention\" itemid=\"1\"></span> Hi there</div>","name":null,"thumbnailUrl":null}],
  "entities":[{"type":"mention","mentioned":{"id":"28:22e50a9b-80cc-4eab-a092-ce64796d28d7","name":""}},{"type":"clientInfo","locale":"en-US","country":"US","platform":"Windows"}],
"channelData":{"teamsChannelId":"19:c5a0767fdc434c8097444a6f7488ae6d@thread.skype","teamsTeamId":"984720987342876876@thread.skype","channel":{"id":"2098374239872-7@thread.skype"},"team":{"id":"19:8fce7e8df8084d62bc0ddb5d77830131@thread.skype"},"onBehalfOf":"[{\"itemid\":0,\"mri\":\"1f265431-39ad-43f7-8dbe-93ee5cc43777\",\"mentionType\":\"webhook\",\"displayName\":\"Testing\"}]","tenant":{"id":"cf857d10-00c4-44ac-8e8a-33f3f8a6d701"}},"action":null,"replyToId":null,"value":null}

Using dot syntax like I do with Slack isn't letting me delve into that payload. Here is the code I am trying to use:

function doPost(json) {
    var parameters = json.parameters;
    var text = String(parameters);
    var datetime = new Date();
    var date = datetime.toDateString();
    var spreadsheet = SpreadsheetApp.openById('myspreadsheetid');
    var sheet = spreadsheet.getSheetByName('mysheetname'); 
    sheet.appendRow([text,date])
};

The above works fine with a payload from a Slack webhook. Here is what that payload looks like:

token=fihwEGHwpoerihKe&team_id=T04U18AG8
&team_domain=mydomain&service_id=234211
&channel_id=C2QJ4SQL0
&channel_name=testchannel
&timestamp=1490881481.055677
&user_id=U0KDGEBJB
&user_name=test.user
&text=my test message
&trigger_word=my test message
4

1 回答 1

1

While I don't understand why this is necessary the solution is below:

First I need to convert the MSTeams payload into a string, then parse it into an object. Removing either of those steps or changing the order does not work.

function doPost(json) {   
    var jsonstring = json.postData.getDataAsString()
    var jsonobject = JSON.parse(jsonstring);
    var text = String(jsonobject.text);
...//the rest of the webapp follows
于 2017-03-30T20:41:46.263 回答