Cloud Functions: Fungsi Ucapan Salam
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Contoh ini menunjukkan cara membuat Cloud Function yang menerima nama depan dan nama belakang sebagai input, serta menampilkan pesan sapaan yang dipersonalisasi.
Contoh kode
Kecuali dinyatakan lain, konten di halaman ini dilisensikan berdasarkan Lisensi Creative Commons Attribution 4.0, sedangkan contoh kode dilisensikan berdasarkan Lisensi Apache 2.0. Untuk mengetahui informasi selengkapnya, lihat Kebijakan Situs Google Developers. Java adalah merek dagang terdaftar dari Oracle dan/atau afiliasinya.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Sulit dipahami","hardToUnderstand","thumb-down"],["Informasi atau kode contoh salah","incorrectInformationOrSampleCode","thumb-down"],["Informasi/contoh yang saya butuhkan tidak ada","missingTheInformationSamplesINeed","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],[],[[["\u003cp\u003eThis page demonstrates the creation of a Cloud Function that takes a first and last name as input.\u003c/p\u003e\n"],["\u003cp\u003eThe Cloud Function returns a personalized greeting message using the provided first and last name.\u003c/p\u003e\n"],["\u003cp\u003eCode samples for the greeting function are provided in C#, Go, Java, Node.js, PHP, Python, and Ruby.\u003c/p\u003e\n"],["\u003cp\u003eThe document specifies how to set up Application Default Credentials for authentication to Cloud Run functions.\u003c/p\u003e\n"]]],[],null,["# Cloud Functions: Greeting Function\n\nThis sample demonstrates how to create a Cloud Function that accepts a first name and last name as input, and returns a personalized greeting message.\n\nCode sample\n-----------\n\n### C#\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n using Google.Cloud.Functions.Framework;\n using System.Text.Json.Serialization;\n using System.Threading;\n using System.Threading.Tasks;\n\n namespace Greeting;\n\n public class Function : ITypedFunction\u003cGreetingRequest, GreetingResponse\u003e\n {\n public Task\u003cGreetingResponse\u003e HandleAsync(GreetingRequest request, CancellationToken cancellationToken) =\u003e\n Task.FromResult(new GreetingResponse\n {\n Message = $\"Hello {request.FirstName} {request.LastName}!\",\n });\n }\n\n public class GreetingRequest\n {\n [JsonPropertyName(\"first_name\")]\n public string FirstName { get; set; }\n\n [JsonPropertyName(\"last_name\")]\n public string LastName { get; set; }\n }\n\n public class GreetingResponse\n {\n [JsonPropertyName(\"message\")]\n public string Message { get; set; }\n }\n\n### Go\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n // Package greeting provides a set of Cloud Functions samples.\n package greeting\n\n import (\n \t\"fmt\"\n\n \t\"github.com/GoogleCloudPlatform/functions-framework-go/functions\"\n )\n\n func init() {\n \tfunctions.Typed(\"Greeting\", greeting)\n }\n\n // greeting is a Typed Cloud Function.\n func greeting(request *GreetingRequest) (*GreetingResponse, error) {\n \treturn &GreetingResponse{\n \t\tMessage: fmt.Sprintf(\"Hello %v %v!\", request.FirstName, request.LastName),\n \t}, nil\n }\n\n type GreetingRequest struct {\n \tFirstName string `json:\"first_name\"`\n \tLastName string `json:\"last_name\"`\n }\n\n type GreetingResponse struct {\n \tMessage string `json:\"message\"`\n }\n\n### Java\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import com.google.cloud.functions.TypedFunction;\n import com.google.gson.annotations.SerializedName;\n\n public class Greeting implements TypedFunction\u003cGreetingRequest, GreetingResponse\u003e {\n @Override\n public GreetingResponse apply(GreetingRequest request) throws Exception {\n GreetingResponse response = new GreetingResponse();\n response.message = String.format(\"Hello %s %s!\", request.firstName, request.lastName);\n return response;\n }\n }\n\n### Node.js\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n const functions = require('@google-cloud/functions-framework');\n\n functions.typed('greeting', req =\u003e {\n return {\n message: `Hello ${req.first_name} ${req.last_name}!`,\n };\n });\n\n### PHP\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n use Google\\CloudFunctions\\FunctionsFramework;\n\n class GreetingRequest\n {\n /** @var string */\n public $first_name;\n\n /** @var string */\n public $last_name;\n\n public function __construct(string $first_name = '', string $last_name = '')\n {\n $this-\u003efirst_name = $first_name;\n $this-\u003elast_name = $last_name;\n }\n\n public function serializeToJsonString(): string\n {\n return json_encode([\n 'first_name' =\u003e $this-\u003efirst_name,\n 'last_name' =\u003e $this-\u003elast_name,\n ]);\n }\n\n public function mergeFromJsonString(string $body): void\n {\n $obj = json_decode($body);\n $this-\u003efirst_name = $obj['first_name'];\n $this-\u003elast_name = $obj['last_name'];\n }\n }\n\n class GreetingResponse\n {\n /** @var string */\n public $message;\n\n public function __construct(string $message = '')\n {\n $this-\u003emessage = $message;\n }\n\n public function serializeToJsonString(): string\n {\n return json_encode([\n 'message' =\u003e $message,\n ]);\n }\n\n public function mergeFromJsonString(string $body): void\n {\n $obj = json_decode($body);\n $this-\u003emessage = $obj['message'];\n }\n };\n\n function greeting(GreetingRequest $req): GreetingResponse\n {\n return new GreetingResponse(\"Hello $req-\u003efirst_name $req-\u003elast_name!\");\n };\n\n FunctionsFramework::typed('greeting', 'greeting');\n\n### Python\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n from dataclasses import dataclass\n\n import functions_framework\n\n\n @dataclass\n class GreetingRequest:\n first_name: str\n last_name: str\n\n # Required to deserialize the request\n @staticmethod\n def from_dict(req: dict) -\u003e \"GreetingRequest\":\n return GreetingRequest(\n first_name=req[\"first_name\"],\n last_name=req[\"last_name\"],\n )\n\n\n @dataclass\n class GreetingResponse:\n message: str\n\n # Required to serialize the response\n def to_dict(self) -\u003e dict:\n return {\n \"message\": self.message,\n }\n\n\n @functions_framework.typed\n def greeting(req: GreetingRequest):\n return GreetingResponse(message=f\"Hello {req.first_name} {req.last_name}!\")\n\n### Ruby\n\n\nTo authenticate to Cloud Run functions, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n require \"functions_framework\"\n\n FunctionsFramework.typed \"greeting\" do |req|\n {\n message: \"Hello #{req['first_name']} #{req['last_name']}!\"\n }\n end\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=functions)."]]