Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firing a Signed Webhook Request #64

Open
gja opened this issue Jul 4, 2021 · 0 comments
Open

Firing a Signed Webhook Request #64

gja opened this issue Jul 4, 2021 · 0 comments

Comments

@gja
Copy link

gja commented Jul 4, 2021

There is no way to use this library currently to trigger a web hook to start a flow with message bird. Here is the code I'm using for this. I'll try to open a PR incorporating this sometime in the future.

module MessageBirdWebhook
  def self.build_payload(time, body)
    parts = []
    parts.push(time)
    parts.push("")
    parts.push(Digest::SHA256.new.digest(body))
    parts.join("\n")
  end

  def self.sign_webhook_request(signing_key, body, now = Time.now)
    now = now.getutc.to_i
    signature = OpenSSL::HMAC.digest(OpenSSL::Digest.new('sha256'), signing_key, build_payload(now, body))

    {
      "MessageBird-Request-Timestamp" => now.to_s,
      "MessageBird-Signature" => Base64.encode64(signature).strip,
    }
  end

  def self.trigger_webhook(signing_key, webhook_id, params)
    body = params.to_json
    client = Faraday.new(url: "https://flows.messagebird.com", headers: { 'Content-Type' => 'application/json' })
    client.post("/flows/#{webhook_id}/invoke", body, sign_webhook_request(signing_key, body))
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant