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

Fix: WhatsApp Cloud Connect #445

Merged
merged 4 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 1 addition & 13 deletions temba/channels/types/whatsapp_cloud/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,6 @@ def get_context_data(self, **kwargs):
url = "https://graph.facebook.com/v13.0/debug_token"
params = {"access_token": f"{app_id}|{app_secret}", "input_token": oauth_user_token}

unsupported_facebook_business_id = False

response = requests.get(url, params=params)
if response.status_code != 200: # pragma: no cover
context["waba_details"] = []
Expand All @@ -78,11 +76,6 @@ def get_context_data(self, **kwargs):
waba_targets = []
granular_scopes = response_json.get("data", dict()).get("granular_scopes", [])
for scope_dict in granular_scopes:
if scope_dict["scope"] == "business_management":
for business_id in scope_dict.get("target_ids", []):
if business_id not in settings.ALLOWED_WHATSAPP_FACEBOOK_BUSINESS_IDS: # pragma: no cover
unsupported_facebook_business_id = True

if scope_dict["scope"] in ["whatsapp_business_management", "whatsapp_business_messaging"]:
waba_targets.extend(scope_dict.get("target_ids", []))

Expand All @@ -106,9 +99,6 @@ def get_context_data(self, **kwargs):
target_waba_details = response_json

business_id = target_waba_details["on_behalf_of_business_info"]["id"]
if business_id not in settings.ALLOWED_WHATSAPP_FACEBOOK_BUSINESS_IDS: # pragma: no cover
unsupported_facebook_business_id = True
continue

url = f"https://graph.facebook.com/v13.0/{target_waba}/phone_numbers"
params = {"access_token": oauth_user_token}
Expand All @@ -123,7 +113,7 @@ def get_context_data(self, **kwargs):
display_phone_number=target_phone["display_phone_number"],
phone_number_id=target_phone["id"],
waba_id=target_waba_details["id"],
currency="USD",
currency=target_waba_details.get("currency", "USD"),
business_id=business_id,
message_template_namespace=target_waba_details["message_template_namespace"],
)
Expand All @@ -141,8 +131,6 @@ def get_context_data(self, **kwargs):
claim_error = context["form"].errors["__all__"][0]
context["claim_error"] = claim_error

context["unsupported_facebook_business_id"] = unsupported_facebook_business_id

# make sure we clear the session credentials if no number was granted
if not context.get("phone_numbers", []):
self.remove_token_credentials_from_session()
Expand Down
69 changes: 34 additions & 35 deletions templates/orgs/org_whatsapp_cloud_connect.haml
Original file line number Diff line number Diff line change
Expand Up @@ -52,45 +52,44 @@
}
});

//window.fbAsyncInit = function () {

window.fbAsyncInit = function () {
// JavaScript SDK configuration and setup
// FB.init({
// appId: '{{ whatsapp_app_id }}', // Meta App ID
// cookie: true, // enable cookies
// xfbml: true, // parse social plugins on this page
// version: 'v14.0' //Graph API version
// });
//};
FB.init({
appId: '{{ whatsapp_app_id }}', // Meta App ID
xfbml: true, // parse social plugins on this page
version: 'v14.0' //Graph API version
});
};

// Load the JavaScript SDK asynchronously
//(function (d, s, id) {
// var js, fjs = d.getElementsByTagName(s)[0];
// if (d.getElementById(id)) return;
// js = d.createElement(s); js.id = id;
// js.src = "https://connect.facebook.net/en_US/sdk.js";
// fjs.parentNode.insertBefore(js, fjs);
//}(document, 'script', 'facebook-jssdk'));
(function (d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "https://connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

// Facebook Login with JavaScript SDK
function launchWhatsAppSignup() {
// Launch Facebook login
//FB.login(function (response) {
//if (response.authResponse) {
// const accessToken = response.authResponse.accessToken;
// if (accessToken) {
// $("#user-access-token").val(accessToken);
// $("#claim-form").submit();
// }
// //Use this token to call the debug_token API and get the shared WABA's ID
//} else {
console.log('User cancelled login or did not fully authorize, redirect to the dialog auth');
location.replace("https://www.facebook.com/v14.0/dialog/oauth?client_id={{ whatsapp_app_id }}&redirect_uri=" + window.location.origin + window.location.pathname + "&scope=business_management,whatsapp_business_management,whatsapp_business_messaging&response_type=token")
//}
//}, {
// scope: 'business_management,whatsapp_business_management,whatsapp_business_messaging',
// extras: {
// feature: 'whatsapp_embedded_signup',
// setup: {
// }
// }
//});
FB.login(function (response) {
if (response.authResponse) {
const accessToken = response.authResponse.accessToken;
if (accessToken) {
$("#user-access-token").val(accessToken);
$("#claim-form").submit();
}
//Use this token to call the debug_token API and get the shared WABA's ID
} else {
console.log('User cancelled login or did not fully authorize, redirect to the dialog auth');
//location.replace("https://www.facebook.com/v14.0/dialog/oauth?client_id={{ facebook_app_id }}&redirect_uri=" + window.location.origin + window.location.pathname + "&scope=business_management,whatsapp_business_management,whatsapp_business_messaging&response_type=token")
}
}, {
scope: 'business_management,whatsapp_business_management,whatsapp_business_messaging',
extras: {
feature: 'whatsapp_embedded_signup',
}
});
}
Loading