Dialogflow में जानें
Dialogflow में सूचनाओं के सैंपल को इंपोर्ट करने के लिए, जारी रखें पर क्लिक करें. इसके बाद, सैंपल को डिप्लॉय और टेस्ट करने के लिए, यह तरीका अपनाएं:
- एजेंट का नाम डालें और सैंपल के लिए, नया Dialogflow एजेंट बनाएं.
- एजेंट इंपोर्ट हो जाने के बाद, एजेंट पर जाएं पर क्लिक करें.
- मुख्य नेविगेशन मेन्यू से, पूरा किया गया पर जाएं.
- इनलाइन एडिटर को चालू करें. इसके बाद, डिप्लॉय करें पर क्लिक करें. एडिटर में सैंपल कोड होता है.
- मुख्य नेविगेशन मेन्यू में, इंटिग्रेशन पर जाएं. इसके बाद, Google Assistant पर क्लिक करें.
- इसके बाद दिखने वाली मोडल विंडो में, बदलावों की ऑटो-झलक देखने की सुविधा को चालू करें. इसके बाद, ऐक्शन सिम्युलेटर खोलने के लिए, जांच करें पर क्लिक करें.
- नमूने की जांच करने के लिए सिम्युलेटर में,
Talk to my test app
डालें!
आपकी सेट की गई कार्रवाई, ज़रूरत के हिसाब से उपयोगकर्ताओं को पुश नोटिफ़िकेशन भेज सकती है, जैसे कि टास्क की आखिरी तारीख आने पर रिमाइंडर भेजना.
इस गाइड में, हमने आपकी सेट की गई कार्रवाई के लिए पुश नोटिफ़िकेशन सेट अप करने का तरीका बताया है. इसके लिए, हमने Actions on Google सुझाव सैंपल का इस्तेमाल करके आपको बताया है. जब उपयोगकर्ता इस कार्रवाई को शुरू करते हैं, तो वे पूछता है कि क्या वे अपनी कार्रवाई बनाने के बारे में सलाह सुनना चाहते हैं या नहीं. उपयोगकर्ता सलाह के लिए कोई खास या रैंडम तरीके से चुनी गई कैटगरी चुन सकते हैं या वे सबसे हाल की सलाह सुन सकते हैं.
काम करने वाले प्लैटफ़ॉर्म
पुश नोटिफ़िकेशन, Android और iOS डिवाइसों पर उपलब्ध हैं. पुश नोटिफ़िकेशन पाने के लिए, iOS डिवाइसों में Assistant ऐप्लिकेशन इंस्टॉल होना ज़रूरी है. फ़िलहाल, ये सुविधा, आवाज़ से चालू होने वाले स्पीकर, स्मार्ट डिसप्ले या दूसरे प्लैटफ़ॉर्म पर काम नहीं करती.
ज़रूरी शर्तें
आपके Actions प्रोजेक्ट में, कम से कम एक कार्रवाई को ट्रिगर करने वाले इंटेंट के तौर पर कॉन्फ़िगर किया जाना चाहिए. यह तब शुरू किया जाएगा, जब उपयोगकर्ता Assistant से मिली सूचना पर टैप करेगा.
आपकी कार्रवाइयों को पुश नोटिफ़िकेशन से, डिफ़ॉल्ट वेलकम इंटेंट ट्रिगर करने के लिए कॉन्फ़िगर नहीं किया जा सकता.
कंसोल का सेटअप
अपनी सेट की गई कार्रवाई में पुश नोटिफ़िकेशन की सुविधा जोड़ने के लिए:
Actions कंसोल पर जाएं और बिल्ड > कार्रवाइयां पर जाएं.
उस कार्रवाई पर क्लिक करें जो ट्रिगर करने वाले उस अतिरिक्त इंटेंट से मेल खाती है जिसके लिए आपको पुश नोटिफ़िकेशन चालू करने हैं.
Actions on Google से जुड़ी सलाह के सैंपल के लिए, आपको "tell_latest_tip" चुनना होगा.
नीचे की ओर स्क्रोल करके, उपयोगकर्ता का जुड़ाव सेक्शन पर जाएं और क्या आपको पुश नोटिफ़िकेशन भेजने हैं को चालू करें.
कॉन्टेंट का टाइटल डालें.
Actions on Google से जुड़ी सलाह के सैंपल के लिए, इसका टाइटल "नई सलाह जोड़ी गई" हो सकता है.
सेव करें पर क्लिक करें.
इंपोर्ट
अगले सेक्शन के लिए, आपको ग्राहक को आइटम भेजने के कोड में, इन इंपोर्ट के बारे में बताना होगा:
const { dialogflow, UpdatePermission, Suggestions, } = require('actions-on-google');
const { actionssdk, UpdatePermission, Suggestions, } = require('actions-on-google');
उपयोगकर्ताओं के लिए ऑप्ट-इन करना
उपयोगकर्ताओं को पुश नोटिफ़िकेशन भेजने से पहले, आपको उनसे ऑप्ट-इन करने के लिए कहना होगा. ऐसा करने के लिए, उन्हें सुझाव वाला एक चिप दिखाएं, ताकि आप उनकी अनुमति मांग सकें. जब वे अनुमति देते हैं, तो आपको उस उपयोगकर्ता को पुश नोटिफ़िकेशन भेजने के लिए, अपडेट किया गया यूज़र आईडी मिलता है.
ऑप्ट-इन करने के लिए सुझाव वाले चिप दिखाएं
आपकी सेट की गई कार्रवाई से उपयोगकर्ताओं को पुश नोटिफ़िकेशन मिल सकें, इसके लिए आपको उन्हें एक सुझाव चिप दिखाना होगा. इससे उन्हें पुश नोटिफ़िकेशन के लिए ऑप्ट-इन करने का न्योता मिलेगा.
नीचे दिया गया कोड स्निपेट, उपयोगकर्ता को टेक्स्ट के जवाब के साथ 'मुझे नई सलाह की सूचना दें' सुझाव वाला चिप भेजता है.
conv.ask('I can send you push notifications. Would you like that?'); conv.ask(new Suggestions('Send notifications'));
conv.ask(' I can send you push notifications. Would you like that?'); conv.ask(new Suggestions('Send notifications'));
responseBuilder .add("I can send you push notifications. Would you like that?") .addSuggestions(new String[] { "Send notifications" });
responseBuilder .add("I can send you push notifications. Would you like that?") .addSuggestions(new String[] { "Send notifications" });
ध्यान दें कि नीचे दिया गया JSON, वेबहुक रिस्पॉन्स के बारे में बताता है.
{ "payload": { "google": { "expectUserResponse": true, "richResponse": { "items": [ { "simpleResponse": { "textToSpeech": "Hi! Welcome to Push Notifications!" } }, { "simpleResponse": { "textToSpeech": "I can send you push notifications. Would you like that?" } } ], "suggestions": [ { "title": "Send notifications" } ] } } } }
ध्यान दें कि नीचे दिया गया JSON, वेबहुक रिस्पॉन्स के बारे में बताता है.
{ "expectUserResponse": true, "expectedInputs": [ { "possibleIntents": [ { "intent": "actions.intent.TEXT" } ], "inputPrompt": { "richInitialPrompt": { "items": [ { "simpleResponse": { "textToSpeech": "Hi! Welcome to Push Notifications!" } }, { "simpleResponse": { "textToSpeech": " I can send you push notifications. Would you like that?" } } ], "suggestions": [ { "title": "Send notifications" } ] } } } ] }
जब वे चिप पर टैप करेंगे, तो आपको UPDATE
की अनुमति लेनी होगी.
इस कोड में, Node.js क्लाइंट लाइब्रेरी के askForUpdatePermission
फ़ंक्शन की मदद से ऐसा करने का तरीका बताया गया है.
- Dialogflow कंसोल में अपना एजेंट खोलें और वह इंटेंट चुनें जिसे आपको अपडेट के लिए कॉन्फ़िगर करना है.
- नीचे Response तक स्क्रोल करें और Google Assistant टैब खोलें.
- मैसेज का कॉन्टेंट जोड़ें पर क्लिक करें और सुझाव वाले चिप चुनें.
- चिप के टेक्स्ट में कोई ऐसी चीज़ सेट करें जो लोगों को ऑप्ट-इन करने का न्योता देती हो. Actions on Google से जुड़ी सलाह के सैंपल में, हमने चिप को मुझे नई सलाह के बारे में सूचना दें पर सेट किया है.
- एक और Dialogflow इंटेंट जोड़ें, जिसे setup_push कहा जाता है और कोई संबंधित कार्रवाई सेट करें, उदाहरण के लिए setup.push. इस इंटेंट का यूज़र एक्सप्रेशन, ऑप्ट-इन चिप के टेक्स्ट से मेल खाना चाहिए. इसके लिए, मुझे नई सलाह के बारे में सूचना दें के उदाहरण में बताया गया है.
app.intent('Subscribe to Notifications', (conv) => { conv.ask(new UpdatePermission({ intent: 'Notification', })); });
आपको अपने एनएलयू सलूशन को कॉन्फ़िगर करना चाहिए, ताकि एक फ़ंक्शन ट्रिगर हो सके. इस फ़ंक्शन से उपयोगकर्ता के एक्सप्रेशन के, पुश नोटिफ़िकेशन ऑप्ट-इन प्रॉम्प्ट की वैल्यू से मेल खाने पर, उपयोगकर्ता की अनुमति मांगी जाती है. यहां स्ट्रिंग मैचिंग के आधार पर एक बुनियादी उदाहरण दिया गया है:
conv.ask(new UpdatePermission({ intent: 'Notification', }));
- Dialogflow कंसोल में अपना एजेंट खोलें और वह इंटेंट चुनें जिसे आपको अपडेट के लिए कॉन्फ़िगर करना है.
- नीचे Response तक स्क्रोल करें और Google Assistant टैब खोलें.
- मैसेज का कॉन्टेंट जोड़ें पर क्लिक करें और सुझाव वाले चिप चुनें.
- चिप के टेक्स्ट में कोई ऐसी चीज़ सेट करें जो लोगों को ऑप्ट-इन करने का न्योता देती हो. Actions on Google से जुड़ी सलाह के सैंपल में, हमने चिप को मुझे नई सलाह के बारे में सूचना दें पर सेट किया है.
- एक और Dialogflow इंटेंट जोड़ें, जिसे setup_push कहा जाता है और कोई संबंधित कार्रवाई सेट करें, उदाहरण के लिए setup.push. इस इंटेंट का यूज़र एक्सप्रेशन, ऑप्ट-इन चिप के टेक्स्ट से मेल खाना चाहिए. इसके लिए, मुझे नई सलाह के बारे में सूचना दें के उदाहरण में बताया गया है.
@ForIntent("Subscribe to Notifications") public ActionResponse subscribeToNotifications(ActionRequest request) { ResponseBuilder responseBuilder = getResponseBuilder(request); responseBuilder.add(new UpdatePermission().setIntent("Notification")); return responseBuilder.build(); }
आपको अपने एनएलयू सलूशन को कॉन्फ़िगर करना चाहिए, ताकि एक फ़ंक्शन ट्रिगर हो सके. इस फ़ंक्शन से उपयोगकर्ता के एक्सप्रेशन के, पुश नोटिफ़िकेशन ऑप्ट-इन प्रॉम्प्ट की वैल्यू से मेल खाने पर, उपयोगकर्ता की अनुमति मांगी जाती है. यहां स्ट्रिंग मैचिंग के आधार पर एक बुनियादी उदाहरण दिया गया है:
ResponseBuilder responseBuilder = getResponseBuilder(request); responseBuilder.add(new UpdatePermission().setIntent("Notification")); return responseBuilder.build();
ध्यान दें कि नीचे दिया गया JSON, Dialogflow का इस्तेमाल करके वेबहुक रिस्पॉन्स के बारे में बताता है.
{ "payload": { "google": { "expectUserResponse": true, "systemIntent": { "intent": "actions.intent.PERMISSION", "data": { "@type": "type.googleapis.com/google.actions.v2.PermissionValueSpec", "permissions": [ "UPDATE" ], "updatePermissionValueSpec": { "intent": "tell_latest_tip" } } } } } }
ध्यान दें कि नीचे दिया गया JSON, कार्रवाई SDK टूल का इस्तेमाल करने वाले वेबहुक रिस्पॉन्स के बारे में बताता है.
{ "expectUserResponse": true, "expectedInputs": [ { "possibleIntents": [ { "intent": "actions.intent.PERMISSION", "inputValueData": { "@type": "type.googleapis.com/google.actions.v2.PermissionValueSpec", "permissions": [ "UPDATE" ], "updatePermissionValueSpec": { "intent": "tell_latest_tip" } } } ] } ] }
सदस्यता को पूरा करें
अपने Node.js वेबहुक से सदस्यता पूरी करने के लिए, आपको उपयोगकर्ता के सूचना आईडी और उनके चुने गए इंटेंट को सेव करना होगा. अगर उपयोगकर्ता अनुमति देता है, तो दोनों को तर्क के तौर पर पास किया जाता है.
अगर आपकी सेट की गई कार्रवाई को Dialogflow के साथ बनाया गया है, तो आपको ये काम करने होंगे:
actions_intent_PERMISSION
को हैंडल करने वाला इंटेंट जोड़ें.- इंटेंट के कार्रवाई के नाम में कुछ ऐसा डालें जिसे आपका वेबहुक बाद में फ़िल्टर कर सके.
इस कोड से, finish_push_setup
नाम वाले इंटेंट के साथ Dialogflow इंटेंट को मैनेज करने का तरीका बताया गया है. इसकी वैल्यू finish.push.setup
की गई है:
app.intent('Confirm Notifications Subscription', (conv) => { if (conv.arguments.get('PERMISSION')) { const updatesUserId = conv.arguments.get('UPDATES_USER_ID'); // Store user ID in database for later use conv.close(`Ok, I'll start alerting you.`); } else { conv.close(`Ok, I won't alert you.`); } });
app.intent('actions.intent.PERMISSION', (conv) => { if (conv.arguments.get('PERMISSION')) { const updatesUserId = conv.arguments.get('UPDATES_USER_ID'); // Store user ID in database for later use conv.close(`Ok, I'll start alerting you.`); } else { conv.close(`Ok, I won't alert you.`); } });
@ForIntent("Confirm Notifications Subscription") public ActionResponse confirmNotificationsSubscription(ActionRequest request) { // Verify the user has subscribed for push notifications ResponseBuilder responseBuilder = getResponseBuilder(request); if (request.isPermissionGranted()) { Argument userId = request.getArgument(ConstantsKt.ARG_UPDATES_USER_ID); if (userId != null) { // Store the user's ID in the database } responseBuilder.add("Ok, I'll start alerting you."); } else { responseBuilder.add("Ok, I won't alert you."); } responseBuilder.endConversation(); return responseBuilder.build(); }
@ForIntent("actions.intent.PERMISSION") public ActionResponse confirmNotificationsSubscription(ActionRequest request) { // Verify the user has subscribed for push notifications ResponseBuilder responseBuilder = getResponseBuilder(request); if (request.isPermissionGranted()) { Argument userId = request.getArgument(ConstantsKt.ARG_UPDATES_USER_ID); if (userId != null) { // Store the user's ID in the database } responseBuilder.add("Ok, I'll start alerting you."); } else { responseBuilder.add("Ok, I won't alert you."); } responseBuilder.endConversation(); return responseBuilder.build(); }
ध्यान दें कि नीचे दिया गया JSON, वेबहुक के अनुरोध के बारे में बताता है.
{ "responseId": "ee9e7ed5-fa1a-48c6-aac7-f9fbe94f1f58-712767ed", "queryResult": { "queryText": "actions_intent_PERMISSION", "action": "confirm.subscription", "parameters": {}, "allRequiredParamsPresent": true, "fulfillmentMessages": [ { "text": { "text": [ "" ] } } ], "outputContexts": [ { "name": "projects/PROJECT_ID/agent/sessions/ABwppHGIgmmU3zBcYMF_vWoHaM4JUo3wniYBHdbUF25l63G7EQWjRnlne8Ar7AOcRHWn1lrEKGy8qdP0UXLcWDBq93k/contexts/actions_capability_screen_output" }, { "name": "projects/PROJECT_ID/agent/sessions/ABwppHGIgmmU3zBcYMF_vWoHaM4JUo3wniYBHdbUF25l63G7EQWjRnlne8Ar7AOcRHWn1lrEKGy8qdP0UXLcWDBq93k/contexts/actions_capability_account_linking" }, { "name": "projects/PROJECT_ID/agent/sessions/ABwppHGIgmmU3zBcYMF_vWoHaM4JUo3wniYBHdbUF25l63G7EQWjRnlne8Ar7AOcRHWn1lrEKGy8qdP0UXLcWDBq93k/contexts/actions_capability_media_response_audio" }, { "name": "projects/PROJECT_ID/agent/sessions/ABwppHGIgmmU3zBcYMF_vWoHaM4JUo3wniYBHdbUF25l63G7EQWjRnlne8Ar7AOcRHWn1lrEKGy8qdP0UXLcWDBq93k/contexts/actions_capability_audio_output" }, { "name": "projects/PROJECT_ID/agent/sessions/ABwppHGIgmmU3zBcYMF_vWoHaM4JUo3wniYBHdbUF25l63G7EQWjRnlne8Ar7AOcRHWn1lrEKGy8qdP0UXLcWDBq93k/contexts/actions_capability_web_browser" }, { "name": "projects/PROJECT_ID/agent/sessions/ABwppHGIgmmU3zBcYMF_vWoHaM4JUo3wniYBHdbUF25l63G7EQWjRnlne8Ar7AOcRHWn1lrEKGy8qdP0UXLcWDBq93k/contexts/google_assistant_input_type_keyboard" }, { "name": "projects/PROJECT_ID/agent/sessions/ABwppHGIgmmU3zBcYMF_vWoHaM4JUo3wniYBHdbUF25l63G7EQWjRnlne8Ar7AOcRHWn1lrEKGy8qdP0UXLcWDBq93k/contexts/actions_intent_permission", "parameters": { "PERMISSION": true, "text": "yes", "UPDATES_USER_ID": "ABwppHHssyPbvEBF1mgN7Ddwb7mkhiVohW9PZ--I_svqy7zFElA4DHkf9pn04UBd5gwZo26_RfXCQ8otcztyIfe6MCQ" } } ], "intent": { "name": "projects/PROJECT_ID/agent/intents/c7f7b30b-5b88-4bb5-b0b8-1cd0862d1dd2", "displayName": "Confirm Notifications Subscription" }, "intentDetectionConfidence": 1, "languageCode": "en" }, "originalDetectIntentRequest": { "source": "google", "version": "2", "payload": { "user": { "permissions": [ "UPDATE" ], "locale": "en-US", "userVerificationStatus": "VERIFIED" }, "conversation": { "conversationId": "ABwppHGIgmmU3zBcYMF_vWoHaM4JUo3wniYBHdbUF25l63G7EQWjRnlne8Ar7AOcRHWn1lrEKGy8qdP0UXLcWDBq93k", "type": "ACTIVE", "conversationToken": "[]" }, "inputs": [ { "intent": "actions.intent.PERMISSION", "rawInputs": [ { "inputType": "KEYBOARD", "query": "yes" } ], "arguments": [ { "name": "PERMISSION", "boolValue": true, "textValue": "true" }, { "name": "text", "rawText": "yes", "textValue": "yes" }, { "name": "UPDATES_USER_ID", "textValue": "ABwppHHssyPbvEBF1mgN7Ddwb7mkhiVohW9PZ--I_svqy7zFElA4DHkf9pn04UBd5gwZo26_RfXCQ8otcztyIfe6MCQ" } ] } ], "surface": { "capabilities": [ { "name": "actions.capability.SCREEN_OUTPUT" }, { "name": "actions.capability.ACCOUNT_LINKING" }, { "name": "actions.capability.MEDIA_RESPONSE_AUDIO" }, { "name": "actions.capability.AUDIO_OUTPUT" }, { "name": "actions.capability.WEB_BROWSER" } ] }, "availableSurfaces": [ { "capabilities": [ { "name": "actions.capability.AUDIO_OUTPUT" }, { "name": "actions.capability.SCREEN_OUTPUT" }, { "name": "actions.capability.WEB_BROWSER" } ] } ] } }, "session": "projects/PROJECT_ID/agent/sessions/ABwppHGIgmmU3zBcYMF_vWoHaM4JUo3wniYBHdbUF25l63G7EQWjRnlne8Ar7AOcRHWn1lrEKGy8qdP0UXLcWDBq93k" }
ध्यान दें कि नीचे दिया गया JSON, वेबहुक के अनुरोध के बारे में बताता है.
{ "user": { "permissions": [ "UPDATE" ], "locale": "en-US", "userVerificationStatus": "VERIFIED" }, "conversation": { "conversationId": "ABwppHEP6OAFZHkSGEiZ5HYM9qrlk8YtIH1DQmJ52cxXELSPvM-kSc_tMJ_5O6ITbgVJlY9i2FIsKWjE_HXLke48", "type": "NEW" }, "inputs": [ { "intent": "actions.intent.PERMISSION", "rawInputs": [ { "inputType": "KEYBOARD", "query": "yes" } ], "arguments": [ { "name": "PERMISSION", "boolValue": true, "textValue": "true" }, { "name": "text", "rawText": "yes", "textValue": "yes" }, { "name": "UPDATES_USER_ID", "textValue": "ABwppHFvBKC-tMYUsUjJkm3YECgZvd6A3sOc7KuQvO4ZdQX3bGLmyoQ41dh4Zmtlzv_kaOKBt1Sf6eRpNbayynrl" } ] } ], "surface": { "capabilities": [ { "name": "actions.capability.AUDIO_OUTPUT" }, { "name": "actions.capability.WEB_BROWSER" }, { "name": "actions.capability.SCREEN_OUTPUT" }, { "name": "actions.capability.ACCOUNT_LINKING" }, { "name": "actions.capability.MEDIA_RESPONSE_AUDIO" } ] }, "availableSurfaces": [ { "capabilities": [ { "name": "actions.capability.AUDIO_OUTPUT" }, { "name": "actions.capability.SCREEN_OUTPUT" }, { "name": "actions.capability.WEB_BROWSER" } ] } ] }
सूचनाएं भेजें
Actions API का इस्तेमाल करके, उपयोगकर्ताओं को पुश नोटिफ़िकेशन भेजे जा सकते हैं. इस एपीआई का इस्तेमाल करने के लिए, आपको अपने Google Cloud प्रोजेक्ट में एपीआई चालू करना होगा. साथ ही, JSON सेवा खाता कुंजी सेट अप और डाउनलोड करनी होगी. यहां कोड सैंपल में दिए गए निर्देशों में चरण #8 देखें.
इसके बाद, Google OAuth2 क्लाइंट लाइब्रेरी का इस्तेमाल करके, सेवा खाता कुंजी को ऐक्सेस टोकन के साथ बदला जा सकता है. साथ ही, इस टोकन का इस्तेमाल करके Actions API पर अपने अनुरोधों की पुष्टि की जा सकती है.
सेवा खाता कुंजी पाएं
- इस यूआरएल पर जाएं और ऐक्शन कंसोल में, "example-project-1" के आखिर में मौजूद प्रोजेक्ट आईडी को अपना प्रोजेक्ट आईडी दें: https://console.developers.google.com/apis/api/actions.googleapis.com/overview?project=example-project-1
- अगर आपको चालू करें बटन दिखता है, तो उस पर क्लिक करें. अगर ऐसा नहीं है, तो चरण 3 पर जाएं.
- इस यूआरएल पर जाएं और 'कार्रवाइयां कंसोल' में "example-project-1" के आखिर में मौजूद "example-project-1" की जगह अपना प्रोजेक्ट आईडी डालें: https://console.developers.google.com/apis/क्रेडेंशियल?project=example-project-1
- क्रेडेंशियल बनाएं > सेवा खाता कुंजी पर क्लिक करें.
- सेवा खाता में जाकर, चुनें बॉक्स पर क्लिक करें. इसके बाद, नया सेवा खाता पर क्लिक करें.
- सेवा खाते को "सूचनाएं" और प्रोजेक्ट के मालिक की भूमिका जैसा कोई नाम दें.
- JSON कुंजी का टाइप चुनें और बनाएं पर क्लिक करें. JSON सेवा खाता कुंजी, आपकी लोकल मशीन पर डाउनलोड हो जाती है.
ऐक्सेस टोकन के लिए कुंजी एक्सचेंज करें और सूचना भेजें
Actions API से सूचना भेजने के लिए, आपको सेवा खाता ऐक्सेस टोकन को बदलने की ज़रूरत होगी. हमारा सुझाव है कि इसके लिए आप Google API क्लाइंट लाइब्रेरी का इस्तेमाल करें. आगे दिए गए कोड स्निपेट की सीरीज़ में, हम Google API Node.js क्लाइंट लाइब्रेरी का इस्तेमाल कर रहे हैं.
- Google API की क्लाइंट लाइब्रेरी इंस्टॉल करें और इसके लिए अनुरोध करें:
npm install googleapis request --save
- सेवा खाता कुंजी से ऐक्सेस टोकन पाने और पुश नोटिफ़िकेशन भेजने के लिए, इस कोड का इस्तेमाल करें:
const {google} = require('googleapis'); const request = require('request'); const jwtClient = new google.auth.JWT( serviceAccount.client_email, null, serviceAccount.private_key, ['https://www.googleapis.com/auth/actions.fulfillment.conversation'], null ); jwtClient.authorize((err, tokens) => { if (!err) { request.post('https://actions.googleapis.com/v2/conversations:send', { auth: { bearer: tokens.access_token, }, json: true, body: { customPushMessage: { userNotification: { title: 'Push Notification Title', }, target: { userId: '<UPDATES_USER_ID>', intent: 'Notification Intent', }, }, isInSandbox: true, }, }, (err, httpResponse, body) => { console.log(`${httpResponse.statusCode}: ${httpResponse.statusMessage}`); }); } });
const {google} = require('googleapis'); const request = require('request'); const jwtClient = new google.auth.JWT( serviceAccount.client_email, null, serviceAccount.private_key, ['https://www.googleapis.com/auth/actions.fulfillment.conversation'], null ); jwtClient.authorize((err, tokens) => { if (!err) { request.post('https://actions.googleapis.com/v2/conversations:send', { auth: { bearer: tokens.access_token, }, json: true, body: { customPushMessage: { userNotification: { title: 'Push Notification Title', }, target: { userId: '<UPDATES_ORDER_ID>', intent: 'Notification Intent', }, }, isInSandbox: true, }, }, (err, httpResponse, body) => { console.log(`${httpResponse.statusCode}: ${httpResponse.statusMessage}`); }); } });
final class Notification { private final String title; Notification(String title) { this.title = title; } String getTitle() { return title; } } final class Target { private final String userId; private final String intent; private final String locale; Target(String userId, String intent, String locale) { this.userId = userId; this.intent = intent; this.locale = locale; } String getUserId() { return userId; } String getIntent() { return intent; } String getLocale() { return locale; } } final class PushMessage { private final Notification userNotification; private final Target target; PushMessage(Notification userNotification, Target target) { this.userNotification = userNotification; this.target = target; } Notification getUserNotification() { return userNotification; } Target getTarget() { return target; } } final class PushNotification { private final PushMessage customPushMessage; private boolean isInSandbox; PushNotification(PushMessage customPushMessage, boolean isInSandbox) { this.customPushMessage = customPushMessage; this.isInSandbox = isInSandbox; } PushMessage getCustomPushMessage() { return customPushMessage; } boolean getIsInSandbox() { return isInSandbox; } } private PushNotification createNotification(String title, String userId, String intent, String locale) { Notification notification = new Notification(title); Target target = new Target(userId, intent, locale); PushMessage message = new PushMessage(notification, target); boolean isInSandbox = true; return new PushNotification(message, isInSandbox); } private ServiceAccountCredentials loadCredentials() throws IOException { String actionsApiServiceAccountFile = this.getClass().getClassLoader().getResource("service-account.json").getFile(); InputStream actionsApiServiceAccount = new FileInputStream(actionsApiServiceAccountFile); ServiceAccountCredentials serviceAccountCredentials = ServiceAccountCredentials.fromStream(actionsApiServiceAccount); return (ServiceAccountCredentials) serviceAccountCredentials.createScoped( Collections.singleton( "https://www.googleapis.com/auth/actions.fulfillment.conversation")); } private String getAccessToken() throws IOException { AccessToken token = loadCredentials().refreshAccessToken(); return token.getTokenValue(); } public void sendNotification(String title, String userId, String intent, String locale) throws IOException { Preconditions.checkNotNull(title, "title cannot be null."); Preconditions.checkNotNull(userId, "userId cannot be null."); Preconditions.checkNotNull(intent, "intent cannot be null."); Preconditions.checkNotNull(locale, "locale cannot be null"); PushNotification notification = createNotification(title, userId, intent, locale); HttpPost request = new HttpPost("https://actions.googleapis.com/v2/conversations:send"); String token = getAccessToken(); request.setHeader("Content-type", "application/json"); request.setHeader("Authorization", "Bearer " + token); StringEntity entity = new StringEntity(new Gson().toJson(notification)); entity.setContentType(ContentType.APPLICATION_JSON.getMimeType()); request.setEntity(entity); HttpClient httpClient = HttpClientBuilder.create().build(); httpClient.execute(request); }
final class Notification { private final String title; Notification(String title) { this.title = title; } String getTitle() { return title; } } final class Target { private final String userId; private final String intent; Target(String userId, String intent) { this.userId = userId; this.intent = intent; } String getUserId() { return userId; } String getIntent() { return intent; } } final class PushMessage { private final Notification userNotification; private final Target target; PushMessage(Notification userNotification, Target target) { this.userNotification = userNotification; this.target = target; } Notification getUserNotification() { return userNotification; } Target getTarget() { return target; } } final class PushNotification { private final PushMessage customPushMessage; private boolean isInSandbox; PushNotification(PushMessage customPushMessage, boolean isInSandbox) { this.customPushMessage = customPushMessage; this.isInSandbox = isInSandbox; } PushMessage getCustomPushMessage() { return customPushMessage; } boolean getIsInSandbox() { return isInSandbox; } } private PushNotification createNotification(String title, String userId, String intent) { Notification notification = new Notification(title); Target target = new Target(userId, intent); PushMessage message = new PushMessage(notification, target); boolean isInSandbox = true; return new PushNotification(message, isInSandbox); } private ServiceAccountCredentials loadCredentials() throws IOException { String actionsApiServiceAccountFile = this.getClass().getClassLoader().getResource("service-account.json").getFile(); InputStream actionsApiServiceAccount = new FileInputStream(actionsApiServiceAccountFile); ServiceAccountCredentials serviceAccountCredentials = ServiceAccountCredentials.fromStream(actionsApiServiceAccount); return (ServiceAccountCredentials) serviceAccountCredentials.createScoped( Collections.singleton( "https://www.googleapis.com/auth/actions.fulfillment.conversation")); } private String getAccessToken() throws IOException { AccessToken token = loadCredentials().refreshAccessToken(); return token.getTokenValue(); } public void sendNotification(String title, String userId, String intent) throws IOException { Preconditions.checkNotNull(title, "title cannot be null."); Preconditions.checkNotNull(userId, "userId cannot be null."); Preconditions.checkNotNull(intent, "intent cannot be null."); PushNotification notification = createNotification(title, userId, intent); HttpPost request = new HttpPost("https://actions.googleapis.com/v2/conversations:send"); String token = getAccessToken(); request.setHeader("Content-type", "application/json"); request.setHeader("Authorization", "Bearer " + token); StringEntity entity = new StringEntity(new Gson().toJson(notification)); entity.setContentType(ContentType.APPLICATION_JSON.getMimeType()); request.setEntity(entity); HttpClient httpClient = HttpClientBuilder.create().build(); httpClient.execute(request); }