Make Hubot run preconfigured commands automatically - coffeescript

I'm using Hubot on flowdock and I'm trying to make Hubot post automatically the respond of a user command.
With the help of https://leanpub.com/automation-and-monitoring-with-hubot/read#leanpub-auto-periodic-task-execution , I've managed to make Hubot talk at a specific time creating a cron.coffee script, so no issues with that.
The thing is that I have another script (trello.coffee) that makes Hubot respond to the command "show cards" and I want the result of this command to be posted in a specific time without the need of me telling Hubot to do this.
How exactly can I do this without the need to write another script (ie. trello.auto.coffee) and the whole robot.emit -> robot.on procedure?

What about using the http listener?
This page has a section for HTTP Listener and has this code example:
module.exports = (robot) ->
robot.router.post '/hubot/chatsecrets/:room', (req, res) ->
room = req.params.room
data = if req.body.payload? then JSON.parse req.body.payload else req.body
secret = data.secret
robot.messageRoom room, "I have a secret: #{secret}"
res.send 'OK'
Would that work?

Related

How to make a http request in a playground update function?

I want to make http requests from my elm program.
I use the openapi-generator https://eriktim.github.io/openapi-elm for the http requests:
https://github.com/eriktim/openapi-elm,
The only example I could find is this:
https://github.com/eriktim/openapi-elm/tree/master/example
There, a request has e.g. type Api.Request Api.Data.PlanetList and is converted with the send function: (Result Http.Error a -> msg) -> Request a -> Cmd msg.
The send function takes a function to convert the Request result to msg but but returns it wrapped in Cmd.
The update function has type
update : Msg -> Model -> ( Model, Cmd Msg )
So as long as the request is made in the update function and the result is put in the return value the framework will get msg out of Cmd.
Now I want to make requests in my program, but I'm using playground game as my main function (example) where the update function is update : Computer -> Model -> Model so the "trick" from the example project is not applicable. How can I still get the values from my request call then?
A Http request is a piece of data for the runtime to execute. If the Cmd is not passed to the runtime through the main update, the actual http call will never happen.
This is why you cannot have side-effects in simple programs (Playground and Browser.sandbox).
I'm not really sure what elm-playground is, but that's not the right starting point for a webapp such as you want to create, as it does not support Commands, such as Http requests.
You want to be using normal / standard Elm - see https://guide.elm-lang.org/install/elm.html and then you want to be building a Program based on Browser.document - https://guide.elm-lang.org/webapps/
Hope that gets you on your way

uber_rides.errors.UberIllegalState: Bad Request. Missing state parameter

I'm trying to just retrieve all my receipts amount from my Uber account and dump into excel.
I installed uber_riders and trying to do that. I have no intention of creating a website or any other business purpose. My mere purpose is just fetch trips bills and dump to excel sheet.
So in my code.
from uber_rides.auth import AuthorizationCodeGrant
auth_flow = AuthorizationCodeGrant(
"xxx",
"partner.trips",
"xxx",
"http://localhost/redirect_uri"
)
print auth_flow
auth_url = auth_flow.get_authorization_url()
session = auth_flow.get_session("http://localhost/redirect_uri")
So what should i fill in the redirect_uri ? I believe i don't need it. Can someone help me on what to do with this. I currently get uber_rides.errors.UberIllegalState: Bad Request. Missing state parameter.
I was also facing the same problem, after spending some time thinking, I found this solution:
Execute till this statement as it is:
auth_url = auth_flow.get_authorization_url()
After executing this instruction copy the auth_url and paste it into a browser.
If everything is fine, you would get to your redirected link with two parameters, 'code' and 'state', now copy this complete url along with the parameters, and use it for the get_session()
session = auth_flow.get_session(<new_url_here>)

Hubot Message Only Specific Channel On Enter/Leave

Working on creating a list of messages that Hubot can randomly choose from to display in the #general channel when someone joins the company. I've got the message part working, but it's doing it on ANY channel... how can I limit it to just a specific channel? One step further, would like to take the users name who entered and paste it inside the sentences if possible.
Thanks!
validWelcome = [
'We have a new kid on the block, Hello!'
'Welcome the newest member to the team!'
'Thanks for joining us!'
'Happy to have you here!'
]
module.exports = (robot) ->
robot.enter (msg) ->
msg.send {room: '#integration-test'}, msg.random validWelcome
There are two issues to consider
Does the chat software you are using expose enough information to Hubot via the adapter when a user joins a room (see docs)
Do you want to display this message if someone leaves and re-joins the #general room?
Taking a wild guess that you are using Slack you can see what the Slack adaptor sends you here. You really want access to channel.name but you can get channel.id from msg.room and take it from there and solve #1. If you're not using Slack find the source for your adapter and search for EnterMessage.
If you want to solve #2 you'll need to do something clever with Hubot's brain and record the fact that you've sent a welcome for each user.

How to intercept/hook into Hubot responses

Is there any way to intercept all Hubot triggers/response globally? The interception should be able to inspect, modify, forward, or reject Hubot response before being sent.
Some goals I would like to achieve:
Throttle all message sent by Hubot (from all plugins/scripts) to prevent flooding.
Apply some kind of ACL (Access Control List) to limit who can use a command.
etc.
I cannot find it in the official Hubot documentation. Am I missing some things?
For controlling access to listeners, check out listener middleware: https://hubot.github.com/docs/scripting/#listener-middleware
https://hubot.github.com/docs/patterns/#restricting-access-to-commands
For rate limiting command execution, check out hubot-rate-limit: https://github.com/michaelansel/hubot-rate-limit
For controlling responses, keep an eye on the response middleware PR: https://github.com/github/hubot/pull/1021
this is a simple middleware I wrote to log messages that are directed at the robot. it can easily be modified to do something else depending on the user name or room name or whatever.
module.exports = (robot) ->
robot.listenerMiddleware (context, next, done) ->
#create a regex with the robots name in it
robotName = new RegExp("#{context.listener.robot.name}", "i")
#only log messages meant for the robot
if robotName.test("#{context.response.message.text}")
#only log messages once with the "everything" listener context
if context.listener.regex.source is /(.+)/i.source
console.log "User: #{context.response.message.user.name} asked me to \"#{context.response.message.text}\" in Channel: #{context.response.message.room}"
#your code goes here
next()
this thing will allow you to rate limit

How do I use send/topic from within a robot.router.post block in hubot

I've been hacking away with hubot for a few weeks now and loving it but have been stuck for a couple of days on the code snippet below. I have a listener that is receiving events via webhooks from our ticket system. That's working great! The problem I'm having is how do I then send some of this data to a room and update the topic?
Normally this is done via the msg.send or msg.topic callback that is sent with functions such as robot.respond but when using robot.router.post, I'm not clear on what object I should be referencing to send the message. Basically, how/where do I get msg defined within robot.router.post?
Apologies for newbness, I'm pretty new to all things javascript/coffeescript. Thanks!
module.exports = (robot) ->
robot.router.post "/hubot/ticket_change", (req, res) ->
ticket_info = req.body.issue.name + ": " + req.body.issue.summary
robot.send ticket_info
robot.topic ticket_info
room = <room id>
robot.messageRoom room, ticket_info