Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
H
hubot-natural
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ar-noc
hubot-natural
Commits
472f59bb
Commit
472f59bb
authored
7 years ago
by
Ronaldo Richieri
Browse files
Options
Downloads
Patches
Plain Diff
Simple rest calls and answers
parent
b16e3b60
No related branches found
No related tags found
No related merge requests found
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
scripts/actions/rest.coffee
+53
-0
53 additions, 0 deletions
scripts/actions/rest.coffee
training_data/rest-example.yml
+786
-0
786 additions, 0 deletions
training_data/rest-example.yml
with
839 additions
and
0 deletions
scripts/actions/rest.coffee
0 → 100644
+
53
−
0
View file @
472f59bb
require
'coffeescript/register'
{
msgVariables
,
stringElseRandomKey
}
=
require
'../lib/common'
# interpolate a string to replace {{ placeholder }} keys with passed object values
# I couldn't find how to make delayed string interpolation with coffeescript yet :/
# Reference solution https://stackoverflow.com/questions/9829470/in-coffeescript-is-there-an-official-way-to-interpolate-a-string-at-run-time
String
::
interp
=
(
values
)
->
@
replace
/{{(.*)}}/g
,
(
ph
,
key
)
->
values
[
key
]
or
''
class
Rest
constructor
:
(
@
interaction
)
->
process
:
(
msg
)
=>
rest_uri
=
@
interaction
.
rest_uri
offline_message
=
(
@
interaction
.
offline
or
'Sorry, there is no online agents to transfer to.'
)
type
=
@
interaction
.
type
?
.
toLowerCase
()
or
'random'
switch
type
when
'block'
messages
=
@
interaction
.
answer
.
map
(
line
)
->
return
msgVariables
line
,
msg
msg
.
sendWithNaturalDelay
messages
when
'random'
message
=
stringElseRandomKey
@
interaction
.
answer
message
=
msgVariables
message
,
msg
msg
.
sendWithNaturalDelay
message
method
=
@
interaction
.
rest
.
method
?
.
toLowerCase
()
or
'get'
@
rest
(
msg
,
3000
,
rest_uri
,
offline_message
,
type
,
method
)
rest
:
(
msg
,
delay
=
3000
,
rest_uri
,
offline_message
,
type
,
method
)
->
data
=
JSON
.
stringify
(
@
interaction
.
rest
.
data
)
successmsg
=
@
interaction
.
rest
.
successmsg
headers
=
@
interaction
.
rest
.
headers
headers
=
'Content-Type'
:
'application/json'
msg
.
http
(
@
interaction
.
rest
.
url
)
.
headers
(
headers
)[
method
](
data
)
(
err
,
response
,
body
)
->
if
response
.
statusCode
isnt
200
msg
.
sendWithNaturalDelay
"We're sorry, something went wrong :/"
return
results
=
JSON
.
parse
(
body
)
message
=
successmsg
.
interp
(
results
)
msg
.
sendWithNaturalDelay
message
module
.
exports
=
Rest
This diff is collapsed.
Click to expand it.
training_data/rest-example.yml
0 → 100644
+
786
−
0
View file @
472f59bb
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment