|
|
Line 369: |
Line 369: |
|
| |
|
| === Http Examples === | | === Http Examples === |
| -----
| | See '''[[Trigger Examples#Http Examples]]'''. |
| ==== Currency Conversion ====
| |
| Suppose the library contains two fields: PriceUSD and PriceEUR.
| |
| The user will enter the value in PriceUSD and the value in Euros will appear in PriceEUR according to the current exchange rate.
| |
| | |
| :; Add new trigger: Set Event '''Create a new entry''' and Phase '''After saving the entry'''. It will run asynchronously.
| |
| | |
| :'''Trigger script:'''
| |
| <source lang="javascript">
| |
| result = http().get("http://api.fixer.io/latest?base=USD"); // Use http://fixer.io/ for
| |
| // conversion rate in JSON
| |
| usdToEur = JSON.parse(result.body)["rates"]["Eur"]; // Use JavaScript object JSON
| |
| // to parse the result
| |
| entry().set("PriceEUR",
| |
| entry().field( "PriceUSD") * usdToEur ); // PriceUSD * conversion rate
| |
| // for value for PriceEUR
| |
| </source>
| |
| <br/>
| |
| -----
| |
| | |
| ==== Creating a Task in the Todoist App ====
| |
| [https://todoist.com Todoist] — A Web service and mobile app for task management. Todoist allows task creation via API [https://developer.todoist.com/].
| |
| In the following example of task creation, text will be taken from the Memento library entry.
| |
| | |
| :; Add a new trigger: Set Event to '''Creating a new entry''' or perhaps '''Update an entry'''. Set Phase as appropriate.
| |
| :'''Trigger script:'''
| |
| <source lang="javascript">
| |
| // Create a JSON command for Todoist task creation
| |
| // using the format described in [https://develop.todoist.com/#add-an-item].
| |
| // This command should include a unique identifier
| |
| // created using the guid() global function.
| |
| var commands =
| |
| '[{"uuid":"' + guid() + '","temp_id":"' + guid() +
| |
| '","type":"item_add","args":{"content":"' + entry().field("Task") + '"}}]';
| |
| | |
| // Execute the HTTP request. An attribute called '''token''' is used
| |
| // for authorization in Todoist.
| |
| // It is available in the Todoist Account setting.
| |
| // Since the text of the task can include symbols
| |
| // not allowed in a URL request,
| |
| // use the standard function encodeURIComponent() to filter them out.
| |
| result = http().get("https://todoist.com/API/v7/sync?token=15281e8e4d499dаff817af0b14112eac3176f9dc&commands=" +
| |
| encodeURIComponent(commands));
| |
| | |
| // Show the user a message indicating successful creation of the task.
| |
| if (result.code == 200)
| |
| message('Task has been successfully created");
| |
| </source>
| |
| <br/> | | <br/> |
| ----- | | ----- |