Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
yasuyukiuno
Active Participant


If you want to send an email from CF web application, you have several options.

The 3rd option, Alert Notification possibles send email without SMTP server.

I tried send an email with Alert Notification from CF Node.js app, so I will show you how to do it step by step.

See this article for an overview of custom alerts.

You will know..



  • How to configure SAP Cloud Platform Alert Notification.

  • How to send a custom alert from Node.js.


Estimated time



  • 40 minutes






Appendix: About SAP Cloud Platform Alert Notification


Alert Notification offers platform alerts and notifications for resources in the SAP Cloud Platform and gives you the possibility to create custom alerts for these resources.
This GA released at June 2019.







Prerequirements



Outline of the procedure



  1. Configure the Alert Notification.

  2. Acquiring API Access Credentials.

  3. Create a Node.js project.

  4. Run the app locally.

  5. Deploy the app to CF.

  6. Run the app on CF.


 

1.Setting up the Alert Notification for Initial Use.


Navigate to the Instances tab in the cockpit and click the New Instance button.





This time, I set the instance name as "alertinstance".



Navigate to the Subscriptions tab in the cockpit and click the Create button.



This subscription name is not important. Any name is ok.



The Condition name, I set "EventTypelsCustomAlert" this time.



The below setting is important. I set "eventType is Equal To mycustomevent".





Select the action type Email to send an email message to a specified email address and then choose Next.







After you created this action, receive an email with confirmation token.



In the Actions view, open your action and click Confirm Action button. Input your token there.


2.Acquiring API Access Credentials.


To enable interactions with the Producer API , you need to create a service key.
You also have to option to import a JSON containing the one of the following parameters:
{"type":"BASIC"} - use for creating basic authentication credentials
{"type":"OAUTH"} - use for creating OAuth 2.0 credentials
In this time, we use Basic authentication.

Navigate to the Service Keys tab and click Create Service Keys button.







3.Create a Node.js project.


Open Command Prompt and move to your workspace directory. In my case, the workspace is "D:\html5".
cd /d D:\html5

Install the express generator. Express is the most popular Node.js framework.
npm install express-generator -g

Generate new project.
express -e alertnode

Move to your project directory and edit package.json.
cd alertnode

■package.json (added request module).
{
"name": "alertnode",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"ejs": "~2.6.1",
"express": "~4.16.1",
"http-errors": "~1.6.3",
"morgan": "~1.9.1",
"request": "*"
}
}

Then install dependencies.
npm install

Then edit app.js
Copy the following code and use it.
Please change <YOUR_BASIC_USER>  place to your basic auth user id.
Please change <YOUR_BASIC_PASS> place to your basic auth password.

■app.js
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var request = require('request');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);

app.use("/api/notify", function(req,res,next){

var options = {
url: 'https://clm-sl-ans-live-ans-service-api.cfapps.eu10.hana.ondemand.com/cf/producer/v1/resource-events', // Endpoint URL + "/cf/producer/v1/resource-events"
method: 'POST',
auth: {
user: "<YOUR_BASIC_USER>", // Change here.
password: "<YOUR_BASIC_PASS>" // Change here.
},
json: {
"eventType": "mycustomevent",
"resource": {
"resourceName": "Your Node.js App.",
"resourceType": "app",
"tags": {
"env": "develop environment"
}
},
"severity": "FATAL",
"category": "ALERT",
"subject": "Something is wrong.",
"body": "Hello world",
"tags": {
"ans:correlationId": "30118",
"ans:status": "CREATE_OR_UPDATE",
"customTag": "42"
}
}
}

// Send request
request(options, function (error, response, body) {
console.log(response.body);

res.send('Send E-mail Notification.');
});

});

// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');
});

module.exports = app;


4.Run the app locally.


Run the application by
npm start

> alertnode@0.0.0 start D:\html5\alertnode
> node ./bin/www

Open your browser and access http://localhost:3000/api/notify



You will receive an email from Alert Notification.



Next, we'll deploy this app to your CF environment and run on cloud.

 

5.Deploy the app to CF.


Using CF CLI, login and deploy the app into Cloud Foundly.
cf api https://api.cf.eu10.hana.ondemand.com
cf login

# Deploy command.
cf push alertnode

Pushing app alertnode to org XXXXXXXX.XXXXXXXX / space dev as unoys@qunie.com...
Getting app info...
Updating app with these attributes...
name: alertnode
path: D:\html5\alertnode
buildpack: nodejs
command: npm start
disk quota: 1G
health check type: port
instances: 1
memory: 1G
stack: cflinuxfs3
routes:
alertnode.cfapps.eu10.hana.ondemand.com

Updating app alertnode...
Mapping routes...
Comparing local files to remote cache...
Packaging files to upload...
Uploading files...
1.40 MiB / 1.40 MiB [=====================================================================================] 100.00% 6s

Waiting for API to complete processing files...

Stopping app...

Staging app and tracing logs...
Downloaded app package (1.8M)

Now it works on your CF space.



6.Run the app on CF.


Click the application routes URL.



Edit the address bar manually and press ENTER key.



You will get an email from Alert Notification.

5 Comments
Labels in this area