Skip to main content

ActionScript Sample Code

This is an ActionScript sample code.

Some sample code can be called in the right pane of the Action Script editor.

blank

ActionScript calls the main function.

async function main(data) {

}

Post code search engine

Call Post code search API | Post-kun.

async function main(data) {
const url = 'https://postcode.teraren.com/postcodes/3230831.json';
const result = await request.get(url, function(error, response, body) {
logger.error("errorMsg:" + error)
logger.log(data)
}
)

return result; //return Promise
}

slack

Send notifications to Slack.

Set up

  1. Prepare your own app with Slack
  2. Set the scope
  3. App installation

If successful, two tokens will be generated.

  • User OAuth Token starting with xoxp-
    • ( token for running the API as a user )
    • xoxp-1498610429941-1501698551602-1234567890-XXXXXXXXXXXXXXXXXX
  • Bot User OAuth Token starting with xoxb-
    • ( Token for running API as a bot )
    • xoxb-1498610429941-1234567890-XXXXXXXXXXXXXXXXXX
  1. Add an app to your channel

Post a message

You can post a message by sending a request to the following endpoint.

POST https://slack.com/api/chat.postMessage

Parameters

  • token Generated OAuth Token
  • channel Where to post the message.
  • text The body of the message. Markdown can be used.

Call with curl command

$ curl -X POST 'https://slack.com/api/chat.postMessage' \
-d 'token=xoxb-1498610429941-1234567890-XXXXXXXXXXXXXXXXXX' \
-d 'channel=#test_channel' \
-d 'text=*テキスト*'

Sample code

async function main(data) {
const result = await request.post('https://slack.com/api/chat.postMessage', {
form: {
token: '{ここをトークンに置き換える}',
channel: 'sample',
text: JSON.stringify(data),
username: 'bot'
}
}
, function(error, response, body) {
logger.error("errorMsg:" + error)
logger.log(data)
}
)

return result; //return Promise
}

simpleAsync

A sample of simple Async-Await code.

async function main (data) {
// call your sub function
const res = await sub();

return res;
}

// you can define your sub-functions. (but should be kept to a minimum chars)
async function sub(){
return new Promise((resolve, reject) => {
var ok = true; //some logics;
if (ok) {
resolve({result: "OK" }); // return {result: "OK" } object to API result
} else {
reject({result: "NG" });
}
});
}

sendGridExample

async function main (data) {
// call sendMail sub-function
const res = await sendMail();

return res;
}

async function sendMail(){
return new Promise((resolve, reject) => {
const SENDGRID_API_KEY = 'SG.XXXXXXXXXXXXXXXXXXXXXX.XXXXXXXXXXXXXXXXXXXXXXX';

// you can use 'sendgridMail' as a variable for require('@sendgrid/mail')
// see https://www.npmjs.com/package/@sendgrid/mail about how to use this package

sendgridMail.setApiKey(SENDGRID_API_KEY)
const msg = {
to: '[email protected]', // Change to your recipient
from: '[email protected]', // Change to your verified sender
subject: 'Test mail from my script',
text: 'test mail',
html: '<strong>This is an example.</strong>',
}
sendgridMail
.send(msg)
.then(() => {
logger.info('Email sent');
resolve({result: "email sent successfully" });
})
.catch((error) => {
logger.error(error);
reject({result: "ERROR sending email" });
})

});
}

Call the Hexabase API once with callAPIAsync

async function main(data) {
return new Promise((resolve, reject) => {

const appId = "APP-AI-posts"; // App ID
const datastoreId = "Db-Article" // Datastore ID

logger.info("処理を開始します...");

// First API call
const url = "api/v0/applications/" + appId + "/datastores/" + datastoreId + "/items/search"; // API URL
const payload = {
use_display_id: true,
omit_fields_data: true,
conditions: [],
per_page: 1,
page: 1
} // API request parameters
callAPIAsync('POST', url, payload).then(res => {
logger.log(res.data);
logger.info("Proc 1 Called");
resolve();
}).catch(e => {
logger.error("エラーが発生しました");
reject(e);
});
});
}

Call the Hexabase API twice with callAPIAsync

ItemList APIでアイテムを読み込んだ後、UpdateItem APIでアイテムを更新しています。

※ If you call the Update Item API in the ActionScript of the "update contents" action, the "update contents" action will be called recursively.

async function main(data) {
return new Promise((resolve, reject) => {

const appId = "APP-AI-posts"; // App ID
const datastoreId = "Db-Article" // Datastore ID
const comment_fieldId = "answer" // Field ID to write comment

logger.info("処理を開始します...");

// First API call
const url = "api/v0/applications/" + appId + "/datastores/" + datastoreId + "/items/search"; // API URL
const payload = {
use_display_id: true,
omit_fields_data: true,
conditions: [],
per_page: 1,
page: 1
} // API request parameters
callAPIAsync('POST', url, payload).then(res => {
logger.log(res.data);
logger.info("Proc 1 Called:" + data.i_id)

// Returns the following API call
const url = "api/v0/applications/" + appId + "/datastores/" + datastoreId + "/items/edit/" + data.i_id; // URL of the following API
const payload = {
"item": {
[comment_fieldId]: data.title
},
"use_display_id": true,
"return_item_result": true,
"is_force_update": true,
"access_key_updates": {
"ignore_action_settings": true,
"overwrite": false,
"apply_related_ds": true,
"groups_to_publish": []
}
} // Request parameters for the following APIs
return callAPIAsync('POST', url, payload); // Can return a promise

}).then(res => {
logger.log(res.data);
logger.info("Proc 2 Called" + data.i_id)

// Call resolve to continue processing
resolve();

}).catch(e => {
logger.error("エラーが発生しました")

// Call reject to abort execution. "e" is returned in the Hexabase API results
reject(e);
});

});
}