Toggle navigation
Toggle navigation
This project
Loading...
Sign in
2021-1-capstone-design2
/
2017110273
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
sdy
2021-06-03 13:05:35 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
332b0911e52cc4f3ad180f906ce9d29de02c0060
332b0911
1 parent
55450579
create server api
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
62 additions
and
0 deletions
routes/api/server_watson.js
server.js
routes/api/server_watson.js
0 → 100644
View file @
332b091
const
express
=
require
(
"express"
);
const
router
=
express
.
Router
();
const
AssistantV2
=
require
(
"ibm-watson/assistant/v2"
);
const
{
IamAuthenticator
}
=
require
(
"ibm-watson/auth"
);
const
authenticator
=
new
IamAuthenticator
({
apikey
:
process
.
env
.
WATSON_ASSISTANT_APIKEY
,
});
const
assistant
=
new
AssistantV2
({
version
:
"2019-02-28"
,
authenticator
:
authenticator
,
url
:
process
.
env
.
WATSON_ASSISTANT_URL
,
});
router
.
get
(
"/session"
,
async
(
req
,
res
)
=>
{
try
{
const
session
=
await
assistant
.
createSession
({
assistantId
:
process
.
env
.
WATSON_ASSISTANT_ID
,
});
res
.
json
(
session
[
"result"
]);
}
catch
(
err
)
{
res
.
send
(
"There was an error processing your request."
);
console
.
log
(
err
);
}
});
router
.
post
(
"/message"
,
async
(
req
,
res
)
=>
{
let
payload
=
{
assistantId
:
process
.
env
.
WATSON_ASSISTANT_ID
,
sessionId
:
req
.
headers
.
session_id
,
input
:
{
message_type
:
"text"
,
text
:
req
.
body
.
input
,
},
};
try
{
const
message
=
await
assistant
.
message
(
payload
);
res
.
json
(
message
[
"result"
]);
}
catch
(
err
)
{
res
.
send
(
"There was an error processing your request."
);
console
.
log
(
err
);
}
});
module
.
exports
=
router
;
server.js
0 → 100644
View file @
332b091
const
express
=
require
(
"express"
);
const
dotenv
=
require
(
"dotenv"
);
const
app
=
express
();
dotenv
.
config
();
app
.
use
(
express
.
json
());
const
watsonRoute
=
require
(
"./routes/api/server_watson"
);
app
.
use
(
"/api/watson"
,
watsonRoute
);
const
port
=
process
.
env
.
PORT
||
5000
;
app
.
listen
(
port
,
()
=>
{
console
.
log
(
`Server is listening http://localhost:
${
port
}
`
);
});
Please
register
or
login
to post a comment