question

acrobatepee avatar image
acrobatepee asked

How to pass argument from Playstream event to Azure functions

Hey ! I see that Rules resulting from Playstream events from which I call an Azure Function allow to set a JSON object.

I have set a body to my Playstream event which is successfully passed, how would I go about getting a value from that body and pass it as an argument to that Azure function using the JSON object field ?

Thanks in advance.

dataPlayStream
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Made Wang avatar image
Made Wang answered

Arguments (JSON) set in Game Manager is your custom parameter, you need to write it manually. If you want to get PlayStream event data in Azure Function, you can refer to PlayFab CloudScript using Azure Functions Context Models - PlayFab | Microsoft Docs to get it in context.

For example, the Azure Function triggered by the rule, you can get the data in the PlayerPlayStreamFunctionExecutionContext, refer to the following code.

PlayerPlayStreamFunctionExecutionContext<dynamic> context = JsonConvert.DeserializeObject<PlayerPlayStreamFunctionExecutionContext<dynamic>>(await req.ReadAsStringAsync());

var data=context.PlayStreamEventEnvelope.EventData;
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

nwhitmont avatar image
nwhitmont answered

If you are using Typescript, you will find the Event Data object inside the context object that is passed into the Azure Function by default. The Event Data contains everything you passed into the body of your custom Playstream event config.

Examples

Send data in body of custom event (Typescript):

PlayFabClient.WritePlayerEvent(
  {
    EventName: 'player-grant-item',
    Body: {
      itemId,
    },
  },
  (error: any, data: any) => {
    if (error) {
      console.error(error)
    } else {
      console.log(data)
    }
  }
)

Use same body data from CloudScript (Typescript):

const exampleCloudscriptFunction: AzureFunction = async function (
  context: Context,
  req: HttpRequest
): Promise<void> {
  const eventData = JSON.parse(
    context.bindings.req.body.PlayStreamEventEnvelope.EventData
  )

  const itemId = eventData.itemId

...

}
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.