पुश नोटिफ़िकेशन (Dialogflow)

Dialogflow में एक्सप्लोर करें

Dialogflow में सूचनाओं का सैंपल इंपोर्ट करने के लिए, जारी रखें पर क्लिक करें. इसके बाद, सैंपल को डिप्लॉय और टेस्ट करने के लिए, यह तरीका अपनाएं:

  1. एजेंट का नाम डालें और सैंपल के लिए, नया Dialogflow एजेंट बनाएं.
  2. एजेंट इंपोर्ट होने के बाद, एजेंट पर जाएं पर क्लिक करें.
  3. मुख्य नेविगेशन मेन्यू से, ग्राहक को आइटम भेजना पर जाएं.
  4. इनलाइन एडिटर चालू करें. इसके बाद, डिप्लॉय करें पर क्लिक करें. एडिटर में सैंपल होता है कोड.
  5. मुख्य नेविगेशन मेन्यू से, इंटिग्रेशन पर जाएं. इसके बाद, Google पर क्लिक करें Assistant पर टैप करें.
  6. स्क्रीन पर दिखने वाली मॉडल विंडो में, बदलावों की अपने-आप झलक देखने की सुविधा चालू करें. इसके बाद, जांच करें पर क्लिक करें ऐक्शन सिम्युलेटर खोलने के लिए.
  7. सैंपल की जांच करने के लिए, सिम्युलेटर में Talk to my test app डालें!
जारी रखें

आपकी सेट की गई कार्रवाई में लोगों को ज़रूरत के मुताबिक पुश नोटिफ़िकेशन भेजा जा सकता है, जैसे कि ईमेल भेजना रिमाइंडर, टास्क को पूरा करने की आखिरी तारीख आने पर.

इस गाइड में, हम Actions on Google से जुड़ी सलाह के सैंपल का इस्तेमाल करते हैं ताकि आपको अपनी कार्रवाई के लिए पुश नोटिफ़िकेशन सेट अप करने का तरीका पता चल सके. जब उपयोगकर्ता इस कार्रवाई को शुरू करते हैं, तो उनसे पूछा जाता है कि वे सलाह सुनना चाहते हैं या नहीं अपना ऐक्शन डेवलप करने के बारे में ज़्यादा जानें. उपयोगकर्ता यह कर सकते हैं सलाह के लिए कोई खास या बिना किसी क्रम के चुनी गई कैटगरी चुनें या वे सबसे हाल की टिप सुनने के लिए चुनें.

इस्तेमाल किए जा सकने वाले प्लैटफ़ॉर्म

पुश नोटिफ़िकेशन की सुविधा Android और iOS डिवाइसों पर उपलब्ध है. iOS डिवाइसों के लिए यह ज़रूरी है पुश नोटिफ़िकेशन पाने के लिए, Assistant ऐप्लिकेशन इंस्टॉल करना ज़रूरी है). वे नहीं हैं फ़िलहाल, इसे आवाज़ से चालू होने वाले स्पीकर, स्मार्ट डिसप्ले या अन्य प्लैटफ़ॉर्म पर इस्तेमाल किया जा सकता है.

ज़रूरी शर्तें

आपके कार्रवाई प्रोजेक्ट में कम से कम एक कार्रवाई को ट्रिगर करने की इंटेंट, जब उपयोगकर्ता से मिली सूचना पर टैप करेगा, तो ट्रिगर हो जाएगा Assistant को चालू करें.

आपकी कार्रवाइयों को, पुश नोटिफ़िकेशन से डिफ़ॉल्ट तौर पर सेट किए गए स्वागत इंटेंट को ट्रिगर करने के लिए कॉन्फ़िगर नहीं किया जा सकता.

कंसोल सेटअप

अपनी कार्रवाई में पुश नोटिफ़िकेशन की सुविधा जोड़ने के लिए:

  1. कार्रवाइयां कंसोल पर जाएं और नेविगेट करें बिल्ड > में कार्रवाइयां.

  2. उस कार्रवाई पर क्लिक करें जो ट्रिगर करने की आपकी इच्छा से मेल खाती हो पुश नोटिफ़िकेशन चालू करें.

    Actions on Google सलाह के नमूने के लिए, आप "tell_latest_tip" चुनें.

  3. नीचे स्क्रोल करते हुए उपयोगकर्ता का जुड़ाव सेक्शन पर जाएं और चालू करें क्या आपको पुश नोटिफ़िकेशन भेजना है.

  4. कॉन्टेंट का टाइटल डालें.

    Actions on Google सलाह सैंपल के लिए, यह शीर्षक "नई टिप जोड़ी गई" हो सकता है.

  5. सेव करें पर क्लिक करें.

आयात

अगले सेक्शन में दी गई जानकारी के लिए, अपने फ़ुलफ़िलमेंट कोड में आपको ये काम करने होंगे को नीचे दिए गए इंपोर्ट का एलान करना होगा:

डायलॉगफ़्लो
const {
  dialogflow,
  UpdatePermission,
  Suggestions,
} = require('actions-on-google');
कार्रवाइयां SDK टूल
const {
  actionssdk,
  UpdatePermission,
  Suggestions,
} = require('actions-on-google');

ऑप्ट-इन करने वाले उपयोगकर्ता

उपयोगकर्ताओं को पुश नोटिफ़िकेशन भेजने से पहले, आपको उन्हें ऑप्ट-इन करने के लिए कहना होगा. इसके लिए, उन्हें सुझाव वाला चिप दिखाया जा सकता है, ताकि उनसे अनुमति ली जा सके. जब वे अनुमति देते हैं, तब आपको भेजने के लिए एक अपडेट यूज़र आईडी मिलता है पुश नोटिफ़िकेशन भेज सकते हैं.

ऑप्ट-इन करने के लिए सुझाव वाले चिप दिखाएं

इससे पहले कि उपयोगकर्ताओं को आपकी सेट की गई कार्रवाई से पुश नोटिफ़िकेशन मिलें, आपको उन्हें उन्हें पुश नोटिफ़िकेशन के लिए ऑप्ट-इन करने का न्योता दें.

यह कोड स्निपेट उपयोगकर्ता को 'नई सलाह के बारे में मुझे सूचना दें' भेजता है सुझाव चिप का इस्तेमाल करें.

Dialogflow Node.js
conv.ask('I can send you push notifications. Would you like that?');
conv.ask(new Suggestions('Send notifications'));
कार्रवाइयां SDK टूल Node.js
conv.ask(' I can send you push notifications. Would you like that?');
conv.ask(new Suggestions('Send notifications'));
डायलॉगफ़्लो Java
responseBuilder
    .add("I can send you push notifications. Would you like that?")
    .addSuggestions(new String[] {
        "Send notifications"
    });
कार्रवाइयां SDK टूल Java
responseBuilder
    .add("I can send you push notifications. Would you like that?")
    .addSuggestions(new String[] {
        "Send notifications"
    });
Dialogflow JSON

ध्यान दें कि नीचे दिए गए 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"
          }
        ]
      }
    }
  }
}
कार्रवाइयां SDK टूल JSON

ध्यान दें कि नीचे दिए गए 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 की अनुमति लेनी होगी. इस कोड में, askForUpdatePermission की मदद से यह करने का तरीका बताया गया है Node.js क्लाइंट लाइब्रेरी का फंक्शन.

Dialogflow Node.js
  1. अपने एजेंट को Dialogflow कंसोल में खोलें और उस इंटेंट को चुनें जिसे अपडेट के लिए कॉन्फ़िगर किया जा रहा है.
  2. नीचे की ओर स्क्रोल करके जवाब पर जाएं और Google Assistant टैब खोलें.
  3. मैसेज का कॉन्टेंट जोड़ें पर क्लिक करें और सुझाव वाले चिप चुनें.
  4. चिप टेक्स्ट को किसी ऐसी चीज़ पर सेट करें जो उपयोगकर्ताओं को ऑप्ट-इन करने का न्योता देती हो. इस Actions on Google से जुड़े सुझावों का सैंपल. हमने इस चिप को नए सुझावों के बारे में मुझे सूचना दें के तौर पर सेट कर दिया है.
  5. एक और Dialogflow इंटेंट जोड़ें, जिसे उदाहरण के लिए setup_push कहा जाता है. इससे जुड़ी कार्रवाई सेट करें, जैसे कि setup.push. इस इंटेंट का उपयोगकर्ता एक्सप्रेशन, ऑप्ट-इन चिप के टेक्स्ट से मेल खाना चाहिए, हमारे उदाहरण में मुझे नई सलाह के बारे में सूचना दें पर क्लिक करें.
नीचे दिए गए स्निपेट में, कार्रवाइयों का इस्तेमाल करके अनुमति का अनुरोध करने का तरीका बताया गया है Node.js के लिए Google क्लाइंट लाइब्रेरी पर उपलब्ध है:
app.intent('Subscribe to Notifications', (conv) => {
  conv.ask(new UpdatePermission({
    intent: 'Notification',
  }));
});
कार्रवाइयां SDK टूल Node.js

आपको अपना एनएलयू सलूशन कॉन्फ़िगर करना चाहिए, ताकि यह पूछने वाले फ़ंक्शन को ट्रिगर कर सके अगर उपयोगकर्ता के एक्सप्रेशन, पुश नोटिफ़िकेशन की वैल्यू से मेल खाते हैं, तो अनुमति ऑप्ट-इन करने का प्रॉम्प्ट. यहां स्ट्रिंग मैचिंग के आधार पर एक बहुत बुनियादी उदाहरण दिया गया है:

conv.ask(new UpdatePermission({
  intent: 'Notification',
}));
डायलॉगफ़्लो Java
  1. अपने एजेंट को Dialogflow कंसोल में खोलें और उस इंटेंट को चुनें जिसे अपडेट के लिए कॉन्फ़िगर किया जा रहा है.
  2. नीचे की ओर स्क्रोल करके जवाब पर जाएं और Google Assistant टैब खोलें.
  3. मैसेज का कॉन्टेंट जोड़ें पर क्लिक करें और सुझाव वाले चिप चुनें.
  4. चिप टेक्स्ट को किसी ऐसी चीज़ पर सेट करें जो उपयोगकर्ताओं को ऑप्ट-इन करने का न्योता देती हो. इस Actions on Google से जुड़े सुझावों का सैंपल. हमने इस चिप को नए सुझावों के बारे में मुझे सूचना दें के तौर पर सेट कर दिया है.
  5. एक और Dialogflow इंटेंट जोड़ें, जिसे उदाहरण के लिए setup_push कहा जाता है. इससे जुड़ी कार्रवाई सेट करें, जैसे कि setup.push. इस इंटेंट का उपयोगकर्ता एक्सप्रेशन, ऑप्ट-इन चिप के टेक्स्ट से मेल खाना चाहिए, हमारे उदाहरण में मुझे नई सलाह के बारे में सूचना दें पर क्लिक करें.
नीचे दिए गए स्निपेट में, कार्रवाइयों का इस्तेमाल करके अनुमति का अनुरोध करने का तरीका बताया गया है Google Java/Kotlin की क्लाइंट लाइब्रेरी पर जाकर:
@ForIntent("Subscribe to Notifications")
public ActionResponse subscribeToNotifications(ActionRequest request) {
  ResponseBuilder responseBuilder = getResponseBuilder(request);
  responseBuilder.add(new UpdatePermission().setIntent("Notification"));
  return responseBuilder.build();
}
कार्रवाइयां SDK टूल Java

आपको अपना एनएलयू सलूशन कॉन्फ़िगर करना चाहिए, ताकि यह पूछने वाले फ़ंक्शन को ट्रिगर कर सके अगर उपयोगकर्ता के एक्सप्रेशन, पुश नोटिफ़िकेशन की वैल्यू से मेल खाते हैं, तो अनुमति ऑप्ट-इन करने का प्रॉम्प्ट. यहां स्ट्रिंग मैचिंग के आधार पर एक बहुत बुनियादी उदाहरण दिया गया है:

ResponseBuilder responseBuilder = getResponseBuilder(request);
responseBuilder.add(new UpdatePermission().setIntent("Notification"));
return responseBuilder.build();
Dialogflow JSON

ध्यान दें कि नीचे दिए गए 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"
          }
        }
      }
    }
  }
}
कार्रवाइयां SDK टूल JSON

ध्यान दें कि नीचे दिए गए JSON में, Actions 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 को हैंडल करता हो.
  • इंटेंट के कार्रवाई नाम के लिए कोई ऐसी चीज़ बताएं जिसे आपका वेबहुक कर सके बाद के लिए फ़िल्टर करें.

नीचे दिया गया कोड, किसी इंटेंट के साथ Dialogflow के इंटेंट को मैनेज करने का तरीका बताता है finish.push.setup नाम वाली कार्रवाई के साथ finish_push_setup नाम:

Dialogflow Node.js
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.`);
  }
});
कार्रवाइयां SDK टूल Node.js
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.`);
  }
});
डायलॉगफ़्लो Java
@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();
}
कार्रवाइयां SDK टूल Java
@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();
}
Dialogflow JSON

ध्यान दें कि नीचे दिए गए 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"
}
कार्रवाइयां SDK टूल JSON

ध्यान दें कि नीचे दिए गए 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 प्रोजेक्ट में एपीआई चालू करना होगा. साथ ही, Google Cloud Manager JSON फ़ाइल के सेवा खाते की कुंजी. यहां कोड सैंपल में दिए गए निर्देश में चरण #8 देखें.

इसके बाद, सेवा खाते की कुंजी को एक्सचेंज करने के लिए Google OAuth2 क्लाइंट लाइब्रेरी का इस्तेमाल किया जा सकता है और कार्रवाइयों एपीआई पर अपने अनुरोधों की पुष्टि करने के लिए टोकन का इस्तेमाल करें.

सेवा खाते की कुंजी पाएं

  1. "example-project-1" की जगह इस यूआरएल पर जाएं आखिर में, आपका प्रोजेक्ट आईडी हो कार्रवाइयां कंसोल में: https://console.developers.google.com/apis/api/actions.googleapis.com/overview?project=example-project-1
  2. अगर आपको चालू करें बटन दिखता है, तो उस पर क्लिक करें. अगर वेबसाइट बिल्डर का इस्तेमाल नहीं किया जा रहा है, तो तीसरे चरण पर जाएं.
  3. "example-project-1" की जगह इस यूआरएल पर जाएं आखिर में, आपका प्रोजेक्ट आईडी हो कार्रवाइयां कंसोल में: https://console.developers.google.com/apis/credentials?project=example-project-1
  4. क्रेडेंशियल बनाएं > सेवा खाते की कुंजी.
  5. सेवा खाता के नीचे चुनें बॉक्स को क्लिक करें और नई सेवा पर क्लिक करें खाता.
  6. सेवा खाते को "सूचनाएं" जैसा कोई नाम दें और इसकी भूमिका प्रोजेक्ट का मालिक.
  7. JSON कुंजी का टाइप चुनें और बनाएं पर क्लिक करें. JSON सेवा खाते की कुंजी है कंप्यूटर पर डाउनलोड किया गया.

ऐक्सेस टोकन पाने के लिए कुंजी बदलें और इसकी सूचना भेजें

Actions API के ज़रिए सूचना भेजने के लिए, आपको एक्सचेंज करना होगा ऐक्सेस टोकन के लिए सेवा खाता कुंजी. हमारा सुझाव है कि आप Google API क्लाइंट का इस्तेमाल करें लाइब्रेरी देखें. बाद में आने वाली कोड स्निपेट की शृंखला में, हम Google API Node.js क्लाइंट लाइब्रेरी.

  1. Google API क्लाइंट लाइब्रेरी इंस्टॉल करें और अनुरोध करें: npm install googleapis request --save अभी तक किसी भी व्यक्ति ने चेक इन नहीं किया है
  2. सेवा खाता कुंजी से ऐक्सेस टोकन पाने के लिए, इस कोड का इस्तेमाल करें पुश नोटिफ़िकेशन भेजने के लिए:
Dialogflow Node.js
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}`);
    });
  }
});
कार्रवाइयां SDK टूल Node.js
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}`);
    });
  }
});
डायलॉगफ़्लो Java
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);
}
कार्रवाइयां SDK टूल Java
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);
}