team-telnyx / telnyx-python

Python SDK for the Telnyx API
MIT License
49 stars 16 forks source link

webhooks.py example error TypeError: unsupported operand type(s) for -: 'float' and 'str' #62

Closed jabowery closed 8 months ago

jabowery commented 3 years ago

Running https://github.com/team-telnyx/telnyx-python/blob/master/examples/webhooks.py

[2021-08-28 00:10:07,376] ERROR in app: Exception on /webhooks [POST]
Traceback (most recent call last):
  File "/home/delegate/anaconda3/envs/sms.service/lib/python3.9/site-packages/flask/app.py", line 2070, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/delegate/anaconda3/envs/sms.service/lib/python3.9/site-packages/flask/app.py", line 1515, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/delegate/anaconda3/envs/sms.service/lib/python3.9/site-packages/flask/app.py", line 1513, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/delegate/anaconda3/envs/sms.service/lib/python3.9/site-packages/flask/app.py", line 1499, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**req.view_args)
  File "/home/delegate/sms.service/webhooks.py", line 22, in webhooks
    event = telnyx.Webhook.construct_event(body, signature, timestamp, public_key)
  File "/home/delegate/anaconda3/envs/sms.service/lib/python3.9/site-packages/telnyx/webhook.py", line 22, in construct_event
    if WebhookSignature.verify(payload, signature_header, timestamp, tolerance):
  File "/home/delegate/anaconda3/envs/sms.service/lib/python3.9/site-packages/telnyx/webhook.py", line 63, in verify
    if tolerance and int(timestamp) < time.time() - tolerance:
TypeError: unsupported operand type(s) for -: 'float' and 'str'

python --version Python 3.9.6

In [1]: import telnyx
In [2]: telnyx.__version__
Out[2]: '1.4.0'
coreybrett commented 1 year ago

Seems like the signature for construct_event doesn't match the example code. The api_key parameter is a KW.

I was able to make the following work,

@webhooks.route("/fax", methods=P)
@csrf.exempt
def view_fax():
    telnyx.api_key = current_app.config["TELNYX_API_KEY"]
    telnyx.public_key = current_app.config["TELNYX_PUB_KEY"]

    try:
        event = telnyx.Webhook.construct_event(
            request.data.decode("utf-8"),
            request.headers.get("Telnyx-Signature-ed25519", None),
            request.headers.get("Telnyx-Timestamp", None),
            tolerance=3600,
        )

    except telnyx.error.SignatureVerificationError as e:
        print(e.errors)
        return "Bad signature", 400

    ic(event)

    print(f"Received event: {event.data.id}, {event.data.event_type}")
    return Response(status=200)