Cloud Code Functions
Integrations
SendGrid APIとCloud Codeの統合:実践ガイド
9 分
sendgridメールapiの使用 はじめに このセクションでは、sendgridをcloud code関数に統合する方法を説明します。このガイドをステップバイステップの指示に従って完了すると、アプリで関数を使用し、iosまたはandroidアプリで呼び出す準備が整います。 前提条件 このチュートリアルを完了するには、以下が必要です: back4appで作成されたアプリ。 新しいアプリの作成チュートリアル を参照して、back4appでアプリを作成する方法を学んでください。 プロジェクトに設定されたback4appコマンドライン。 cloud codeの設定チュートリアル を参照して、プロジェクトのためにクラウドコードを設定する方法を学んでください。 twilio でアカウントを作成してください。 さあ、始めましょう! sendgridを使用して関数を作成します。これにより、顧客へのメッセージの配信やsendgrid v3 rest apiを使用するためのパラメータの設定など、多くの可能性で作業できるようになります。 sendgridでアカウントを作成またはアクセスする方法を学ぶには、以下のリンクを確認してください: 新しいアカウントを作成 アカウントにログイン 1 sendgrid apiキーの作成 コーディングを始める前の最も重要なステップは、環境を設定するための正しいキーを作成することです。アカウントにアクセスした後、設定のドロップダウンメニューで、 apiキー apiキー のオプションを見つけてください。以下の画像のように その後、右上で、 apiキー名 apiキー名 を選択してください。以下のように表示されます 上の画像にあるように、apiキーに対して フルアクセス を許可するオプションを選択する必要があります。 キーの作成を進めるために、 作成と表示 作成と表示 をクリックした後、以下の画面を見ることができます ヒント 書き留めるように注意してください。取得する方法はありません。テキストをクリックしてコピーしてください。 2 cloud codeに関数を追加する sendgridのapiを使用するこの方法の主な戦略は、cloud codeに「 sendgridemail sendgridemail という名前の関数を作成し、アプリから呼び出すことです。 2 1 sendgridからモジュールをインストールする 「 package json package json 」という名前のファイルを作成し、このファイルの中でtwilioモジュールをインストールする必要があります。 1 { 2 "dependencies" { 3 "@sendgrid/mail" " " 4 } 5 } 2 2 cloud codeを実装する すべてのメールフィールドは、アプリから送信される必要があることに注意してください 件名から内容まで パラメータとして。コードは次のとおりです。 parse server 3 x 1 parse cloud define("sendgridemail", async(request) => { 2 const sgmail = require('@sendgrid/mail'); 3 4 // import sendgrid module and call with your sendgrid api key 5 sgmail setapikey("your sendgrid api key here"); 6 7 const msg = { 8 to request params toemail, 9 replyto 'info\@youremail com', 10 from 'info\@youremail com', 11 subject request params subject, 12 text request params body 13 }; 14 15 try{ 16 await sgmail send(msg); 17 return 'ok' 18 } catch (e){ 19 return `error ${e message}` 20 } 21 22 }); parse server 2 x 1 parse cloud define("sendgridemail", (request, response) => { 2 const sgmail = require('@sendgrid/mail'); 3 4 // import sendgrid module and call with your sendgrid api key 5 sgmail setapikey("your sendgrid api key here"); 6 7 const msg = { 8 to request params toemail, 9 replyto 'info\@youremail com', 10 from 'info\@youremail com', 11 subject request params subject, 12 text request params body 13 }; 14 15 sgmail send(msg) then(() => { 16 response success("the message was sent!"); 17 }) 18 catch(error => { 19 //log friendly error 20 response error(error tostring()); 21 }); 22 }); ヒント フィールドを変更することを忘れないでください から から と reply to reply to あなたの個人情報に。 次に、アプリでcloud code関数を呼び出す必要があります。 3 cloud code関数を呼び出す この現在のステップでは、関数を呼び出すための2つの可能性、すなわち:androidとios(swiftとobjective c)で作業できます。 android 1 map\<string, string> params = new hashmap<>(); 2 3 // create the fields "emailaddress", "emailsubject" and "emailbody" 4 // as strings and use this piece of code to add it to the request 5 params put("toemail", emailaddress); 6 params put("subject", emailsubject); 7 params put("body", emailbody); 8 9 parsecloud callfunctioninbackground("sendgridemail", params, new functioncallback\<object>() { 10 @override 11 public void done(object response, parseexception exc) { 12 if(exc == null) { 13 // the function executed, but still has to check the response 14 } 15 else { 16 // something went wrong 17 } 18 } 19 }); ios(swift) 1 pfcloud callfunctioninbackground("sendgridemail", withparameters \[ 2 // these fields have to be defined earlier 3 "toemail" toemail, 4 "subject" subject, 5 "body" body 6 ]) { (response, error) in 7 if error == nil { 8 // the function executed, but still has to check the response 9 } else { 10 // the function returned an error 11 } 12 } ios(objective c) 1 \[pfcloud callfunctioninbackground @"sendgridemail" 2 withparameters @{@"toemail" toemail, 3 @"subject" subject, 4 @"body" body} 5 block ^(nsstring myalertmsg, nserror error){ 6 if(!error) { 7 // the function executed, but still has to check the response 8 } 9 else { 10 // the function returned an error 11 } 12 } 13 ]; 4 完了! これでsendgridの使用は完了です。誰かがあなたのsendgrid apiを使用してメールを送信することを許可する前に、何らかの認証を使用することをお勧めします。 何か助けが必要な場合や、関数/リンクが機能しない場合は、チャットを通じて私たちのチームに連絡してください!