新手求助:如何用SendGrid与Google Cloud Function发邮件并对接Dialogflow?
新手向:Google Cloud Functions + SendGrid 发邮件 + Dialogflow 对接指南
作为刚接触这些工具的新手,踩坑太正常了!我一步步带你搞定这两个需求,代码和步骤都尽量写得直白易懂~
一、创建Google Cloud Function并使用SendGrid发邮件
前置准备
- 拥有Google Cloud(GCP)账号,创建一个新的GCP项目
- 启用Cloud Functions API(在GCP控制台的API库中搜索启用)
- 注册SendGrid账号,获取你的SendGrid API Key(在SendGrid控制台的Settings > API Keys里创建,选Full Access权限方便测试)
步骤1:创建Cloud Function
- 打开GCP控制台,进入「Cloud Functions」页面,点击「创建函数」
- 填写基本信息:函数名称随便取(比如
sendEmailWithSendGrid),选择「HTTP」触发器,权限暂时选「允许未认证调用」(测试完后可以改成仅限内部调用更安全) - 运行环境选择Node.js 18(目前比较稳定的版本),点击「下一步」
步骤2:编写代码
在代码编辑器里,分别修改index.js和package.json:
index.js(纯HTTP测试版)
// 引入SendGrid邮件库 const sgMail = require('@sendgrid/mail'); // 从环境变量获取SendGrid API Key(绝对不要硬编码!) sgMail.setApiKey(process.env.SENDGRID_API_KEY); // 处理HTTP请求的核心函数 exports.sendEmail = async (req, res) => { // 从POST请求体里获取目标邮箱 const { toEmail } = req.body; if (!toEmail) { return res.status(400).send('请提供目标邮箱地址'); } // 配置邮件内容 const msg = { to: toEmail, // 收件人邮箱 from: 'your-verified-email@example.com', // 必须是SendGrid里已验证的发件邮箱 subject: '来自Cloud Function的测试邮件', text: '你好!这是通过Google Cloud Functions + SendGrid发送的测试邮件~', html: '<strong>你好!</strong>这是通过Google Cloud Functions + SendGrid发送的测试邮件~', }; try { await sgMail.send(msg); res.status(200).send('邮件发送成功!'); } catch (error) { console.error(error); res.status(500).send('邮件发送失败,请查看GCP日志排查问题'); } };
package.json
需要添加SendGrid的依赖,修改后如下:
{ "name": "send-email-function", "version": "1.0.0", "dependencies": { "@sendgrid/mail": "^7.7.0" } }
步骤3:配置环境变量并部署
- 在代码页面的「运行时,构建,连接和部署设置」里,找到「环境变量」,点击「添加变量」,键填
SENDGRID_API_KEY,值填你之前获取的SendGrid API Key - 点击「部署」,等待几分钟部署完成
步骤4:测试函数
部署完成后,复制函数的触发URL,用Postman或者curl发送POST请求:
curl -X POST -H "Content-Type: application/json" -d '{"toEmail": "your-test-email@example.com"}' https://your-function-url
如果收到邮件,说明第一步成功了!
二、将Cloud Function与Dialogflow对接
前置准备
- 拥有Dialogflow ES账号(新手推荐用ES,操作更简单),创建一个Agent
- 已经完成上面的Cloud Function部署
步骤1:在Dialogflow里创建意图
- 打开Dialogflow控制台,进入你的Agent,点击「意图」>「创建意图」
- 意图名称设为「发送邮件」,在「训练短语」里添加比如:
- "帮我给xxx@example.com发邮件"
- "我要发送邮件到abc@test.com"
- 添加参数:点击「添加参数」,名称设为
email,实体选择@sys.email(系统自带的邮箱实体,能自动识别邮箱地址),设置为必填参数 - 在「响应」里先加个默认回复,比如「正在帮你发送邮件,请稍等...」
步骤2:启用Webhook并配置Cloud Function URL
- 在意图页面,找到「Fulfillment」,勾选「启用Webhook调用用于意图」
- 回到Dialogflow控制台的「Fulfillment」页面,启用「Webhook」,把之前Cloud Function的触发URL粘贴到「URL」框里,点击「保存」
步骤3:修改Cloud Function适配Dialogflow请求
Dialogflow的Webhook请求是特定的JSON格式,我们需要修改index.js来解析这个请求,提取邮箱参数,发送邮件后再返回响应给Dialogflow:
修改后的index.js(对接Dialogflow版)
const sgMail = require('@sendgrid/mail'); sgMail.setApiKey(process.env.SENDGRID_API_KEY); exports.sendEmail = async (req, res) => { // 解析Dialogflow的Webhook请求体 const dialogflowReq = req.body; const email = dialogflowReq.queryResult.parameters.email; if (!email) { // 返回Dialogflow格式的错误响应 return res.json({ fulfillmentText: '抱歉,我没识别到你要发送的邮箱地址,请再说一遍?' }); } const msg = { to: email, from: 'your-verified-email@example.com', // 还是要用SendGrid验证过的邮箱 subject: '来自Dialogflow的邮件', text: '这是通过Dialogflow触发Cloud Function发送的邮件~', html: '<strong>这是通过Dialogflow触发Cloud Function发送的邮件~</strong>' }; try { await sgMail.send(msg); // 返回成功响应给Dialogflow res.json({ fulfillmentText: `邮件已经成功发送到${email}啦!` }); } catch (error) { console.error(error); res.json({ fulfillmentText: '抱歉,邮件发送失败了,请稍后再试' }); } };
修改后重新部署Cloud Function即可。
步骤4:测试Dialogflow对接
回到Dialogflow控制台的「测试模拟器」,输入比如「帮我发送邮件到test@example.com」,如果一切正常,模拟器会返回「邮件已经成功发送到test@example.com啦!」,同时你会收到对应的邮件。
新手必看注意事项
- 不要硬编码API Key:一定要用GCP的环境变量存储,避免泄露敏感信息
- SendGrid发件邮箱验证:必须在SendGrid控制台验证你的发件邮箱,否则邮件会被拦截或者进入垃圾箱
- 权限问题:如果后续要限制访问,可以把Cloud Function的触发器权限改成仅限Dialogflow的服务账号调用,避免未授权访问
- 日志排查:如果发送失败,去GCP控制台的Cloud Functions日志里看具体错误信息,大部分问题都是API Key错误、邮箱未验证或者网络问题
内容的提问来源于stack exchange,提问作者Yoshii
相关产品推荐
相关产品推荐

