def send_list_picker_with_data_ref(destination_id, interactive_data_ref): message_id = str(uuid.uuid4()) # generate a unique message id payload = { "v": 1, "type": "interactive", "id": message_id, "sourceId": BIZ_ID, "destinationId": destination_id, "interactiveDataRef": interactive_data_ref } headers = { "Content-Type": "application/json", "Authorization": "Bearer %s" % get_jwt_token(), "id": message_id, "Source-Id": BIZ_ID, "Destination-Id": destination_id } r = requests.post("%s/message" % BUSINESS_CHAT_SERVER, json=payload, headers=headers, timeout=10) print("Business Chat server return code: %s" % r.status_code)
def send_apple_pay_request(destination_id): message_id = str(uuid.uuid4()) # generate unique message id request_id = str(uuid.uuid4()) # generate unique request id headers = { "Content-Type": "application/json", "Authorization": "Bearer %s" % get_jwt_token(), "id": message_id, "Source-Id": BIZ_ID, "Destination-Id": destination_id } payload = { "v": 1, "type": "interactive", "id": message_id, "sourceId": BIZ_ID, "destinationId": destination_id, "interactiveData": { "receivedMessage": { "style": "icon", "title": "Payment Title", "subtitle": "Payment Subtitle" }, "bid": IMESSAGE_EXTENSION_BID, "data": { "requestIdentifier": request_id, "mspVersion": "1.0", "payment": { "paymentRequest": { "lineItems": [ { "label": "Item 1", "amount": "10.00", "type": "final" } ], "total": { "label": "Your Total", "amount": "10.00", "type": "final" }, "applePay": { "merchantIdentifier": MY_MERCHANT_ID, "supportedNetworks": [ "amex", "visa", "discover", "masterCard", "chinaUnionPay", "interac", "privateLabel" ], "merchantCapabilities": [ "supportsDebit", "supportsCredit", "supportsEMV", "supports3DS" ] }, "merchantName": MY_MERCHANT_NAME, "countryCode": "US", "currencyCode": "USD", "requiredBillingContactFields": [], "requiredShippingContactFields": [] }, "merchantSession": get_apple_pay_merchant_session(), "endpoints": { "paymentGatewayUrl": MY_PAYMENT_GATEWAY_URL } } } } } r = requests.post("%s/message" % BUSINESS_CHAT_SERVER, json=payload, headers=headers, timeout=10,verify=False) print("Business Chat server return code: %s" % r.status_code)
def send_list_picker_with_images(destination_id, image_file_path): message_id = str(uuid.uuid4()) # generate a unique message id # a unique request id that will be sent back with response request_id = str(uuid.uuid4()) # read the image file data and encoded it as base 64 with open(image_file_path, "rb") as image_file: image_data_encoded = base64.b64encode(image_file.read()) payload = { "v": 1, "type": "interactive", "id": message_id, "sourceId": BIZ_ID, "destinationId": destination_id, "interactiveData": { "bid": IMESSAGE_EXTENSION_BID, "data": { "images": [{ "data": image_data_encoded, "identifier": "0" }], "listPicker": { "sections": [{ "items": [{ "identifier": "1", "imageIdentifier": "0", "order": 0, "style": "default", "subtitle": "Red and delicious", "title": "Apple" }], "order": 0, "title": "Fruit", "multipleSelection": True }, { "items": [{ "identifier": "2", "imageIdentifier": "0", "order": 0, "style": "default", "subtitle": "Crispy red", "title": "another apple" }], "order": 1, "title": "Veggies", "multipleSelection": True }] }, "mspVersion": "1.0", "requestIdentifier": request_id }, "receivedMessage": { "imageIdentifier": "0", "style": "small", "subtitle": "Farm fresh to you", "title": "Select Produce" }, "replyMessage": { "style": "small", "title": "Selected Produce", "subtitle": "Selected Produce" } } } headers = { "Content-Type": "application/json", "Authorization": "Bearer %s" % get_jwt_token(), "id": message_id, "Source-Id": BIZ_ID, "Destination-Id": destination_id } r = requests.post("%s/message" % BUSINESS_CHAT_SERVER, json=payload, headers=headers, timeout=10) print("Business Chat server return code: %s" % r.status_code)
def receive_large_interactive_payload(r=request): # here we are sending and receiving the interactive payload # Verify the authentication try: authorization = request.headers.get("Authorization") print("beginning of authorization==> ") print(authorization) print(" ==>end of authorization") except TypeError as e: print("\nSYSTEM: jwt_token get error: %s" % e) abort(400) try: jwt_token = authorization[7:] # <-- skip the Bearer prefix except TypeError as etype: print("\nSYSTEM: jwt_token authorization error: %s" % etype) abort(400) try: jwt.decode(jwt=jwt_token, key=base64.b64decode(SECRET), audience=CSP_ID) except Exception as e: print("\nSYSTEM: Authorization failed, error message: %s" % e) abort(403) # read the Gzip data print(request.data) fileobj = io.BytesIO(request.data) uncompressed = gzip.GzipFile(fileobj=fileobj, mode='rb') try: payj = uncompressed.read() print(payj) except IOError as eio: print("\nSYSTEM: Payload decompression error: %s" % eio) abort(400) # decode JSON try: print("request arg ==> ") print(json.dumps(request.form.to_dict())) payload = json.loads(json.dumps(request.form.to_dict())) # payload = json.dump(payload, StringIO('["streaming API"]')) print(type(payload)) except ValueError as eve: print("\nSYSTEM: Payload decode error: %s" % eve) abort(400) print("payload %s" % payload) message_type = payload.get("type") print(message_type) if message_type != "interactive": print("Received a %s instead of an interactive ..." % message_type) else: jdataref = payload["interactiveDataRef[url]"] attachment_file_name = "data_reference_debug.json" decryption_key = payload["interactiveDataRef[key]"].upper() mmcs_url = payload["interactiveDataRef[url]"] mmcs_owner = payload["interactiveDataRef[owner]"] file_size = payload["interactiveDataRef[size]"] bid = payload["interactiveDataRef[bid]"] # get hex encoded signature and convert to base64 hex_encoded_signature = payload["interactiveDataRef[signature]"].upper( ) signature = base64.b16decode(hex_encoded_signature) base64_encoded_signature = base64.b64encode(signature) predownload_headers = { "Authorization": "Bearer %s" % get_jwt_token(), "source-id": BIZ_ID, "MMCS-Url": mmcs_url, "MMCS-Signature": base64_encoded_signature, "MMCS-Owner": mmcs_owner } print("pre download headers ==>") print(predownload_headers) print(BUSINESS_CHAT_SERVER) r = requests.get("%s/preDownload" % BUSINESS_CHAT_SERVER, headers=predownload_headers, timeout=10) print(r.status_code) print(r.content) download_url = json.loads( r.content.decode('utf-8')).get("download-url") # download_url = json.loads(r.content.decode('utf-8')).get("download-url"); print(download_url) # download the attachment data with GET request encrypted_attachment_data = requests.get(download_url).content print("size :: ") print(file_size) print("retrieved size ") print(len(encrypted_attachment_data)) # compare download size with expected file size # if len(encrypted_attachment_data) != file_size: # raise Exception("Data downloaded not of expected size! Check preDownload step.") # decrypted the downloaded data decrypted_attachment_data = decrypt(encrypted_attachment_data, decryption_key) decode_headers = { "Authorization": "Bearer %s" % get_jwt_token(), "source-id": BIZ_ID, "accept": "*/*", "accept-encoding": "gzip, deflate", "bid": bid } r = requests.post("%s/decodePayload" % BUSINESS_CHAT_SERVER, headers=decode_headers, data=decrypted_attachment_data, timeout=10) # Write out the Business Chat server response print("%d: %s" % (r.status_code, r.text)) print("attachent file name ", attachment_file_name) # Write out the file print("\nSYSTEM: Writing full response to local file: %s" % attachment_file_name) with open(attachment_file_name, "wb") as attachment_local_file: attachment_local_file.write((r.text).encode()) return r.text
def send_list_picker_with_images(destination_id, image_file_path_dict): # Array of JSON elements, starts empty images_descriptor = [] # generate a unique message id message_id = str(uuid.uuid4()) # a unique request id that will be sent back with response request_id = str(uuid.uuid4()) for this_image in image_file_path_dict: image_full_path = image_file_path_dict[this_image] with open(image_full_path, "rb") as image_file: image_data_encoded = base64.b64encode(image_file.read()) print("%s encoded!" % image_full_path) images_descriptor.append( { "data": image_data_encoded, "identifier": this_image } ) headers = { "Content-Type": "application/json", "Authorization": "Bearer %s" % get_jwt_token(), "id": message_id, "Source-Id": BIZ_ID, "Destination-Id": destination_id, "include-data-ref": "true" # <--- This tells Business Chat to return a data reference. # NOTE: it is a 4 character string "true", NOT A BOOLEAN ! } payload = { "v": 1, "type": "interactive", "id": message_id, "sourceId": BIZ_ID, "destinationId": destination_id, "interactiveData": { "bid": IMESSAGE_EXTENSION_BID, "data": { "images": images_descriptor, "listPicker": { "sections": [ { "items": [ { "identifier": "1", "imageIdentifier": "<imageIdentifier 0>", "order": 0, "style": "default", "subtitle": "Red and delicious", "title": "Apple" } ], "order": 0, "title": "Fruit", "multipleSelection": True }, { "items": [ { "identifier": "2", "imageIdentifier": "<imageIdentifier 1>", "order": 0, "style": "default", "subtitle": "Crispy red", "title": "another apple" } ], "order": 1, "title": "Veggies", "multipleSelection": True } ] }, "mspVersion": "1.0", "requestIdentifier": request_id }, "receivedMessage": { "imageIdentifier": "<imageIdentifier 2>", "style": "small", "subtitle": "Farm fresh to you", "title": "Select Produce" }, "replyMessage": { "style": "small", "title": "Selected Produce", "subtitle": "Selected Produce" } } } r = requests.post("%s/message" % BUSINESS_CHAT_SERVER, json=payload, headers=headers, timeout=10) print("Business Chat server return code: %s" % r.status_code) print("Business Chat server response body: %s" % r.text)
def send_text_list_picker(destination_id): message_id = str(uuid.uuid4()) # generate unique message id # a unique request id that will be sent back with response request_id = str(uuid.uuid4()) headers = { "Content-Type": "application/json", "Authorization": "Bearer %s" % get_jwt_token(), "id": message_id, "Source-Id": BIZ_ID, "Destination-Id": destination_id } payload = { "type": "interactive", "interactiveData": { "bid": IMESSAGE_EXTENSION_BID, "data": { "listPicker": { "sections": [{ "items": [{ "identifier": "1", "order": 0, "style": "default", "subtitle": "Red and delicious", "title": "Apple" }, { "identifier": "2", "order": 1, "style": "default", "subtitle": "Vitamin C boost", "title": "Orange" }], "order": 0, "title": "Fruit", "multipleSelection": True }, { "items": [{ "identifier": "3", "order": 0, "style": "default", "subtitle": "Crispy greens", "title": "Lettuce" }, { "identifier": "4", "order": 1, "style": "default", "subtitle": "Not just for your eye lids", "title": "Cucumber" }], "order": 1, "title": "Veggies", "multipleSelection": False }] }, "mspVersion": "1.0", "requestIdentifier": request_id }, "receivedMessage": { "style": "small", "subtitle": "Farm fresh to you", "title": "Select Produce" }, "replyMessage": { "style": "small", "title": "Selected Produce", "subtitle": "Selected Produce" } }, "sourceId": BIZ_ID, "destinationId": destination_id, "v": 1, "id": message_id } r = requests.post("%s/message" % BUSINESS_CHAT_SERVER, json=payload, headers=headers, timeout=10) print("Business Chat server return code: %s" % r.status_code)