在这篇文章的指导下,我编写了以下 queuetrigger 代码,用于在消息排队时发送电子邮件。
import logging
import sendgrid
import azure.functions as func
import os
def main(msg: func.QueueMessage) -> None:
logging.info('Python queue trigger function processed a queue item: %s',
msg.get_body().decode('utf-8'))
data = {
"personalizations": [
{
"to": [
{
"email": "rrrrrrrr"
}
],
"subject": "Sending with SendGrid is Fun"
}
],
"from": {
"email": "ghyu"
},
"content": [
{
"type": "text/plain",
"value": "and easy to do anywhere, even with Python"
}
]
}
print('Sending email using SendGrid:', data)
with open(os.environ[_AZURE_FUNCTION_SENDGRID_OUTPUT_ENV_NAME], 'wb') as f:
json.dump(data,f)
函数.json
{
"scriptFile": "__init__.py",
"bindings": [
{
"name": "msg",
"type": "queueTrigger",
"direction": "in",
"queueName": "outqueue1",
"connection": "storageaccountautom92bb_STORAGE"
},
{
"name": "outputMessage",
"type": "sendGrid",
"from": "ghyu",
"apiKey": "MY_SENDGRID_API_KEY",
"direction": "out"
}
],
"disabled": false
}
local.settings.json
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "hjk",
"FUNCTIONS_WORKER_RUNTIME": "python",
"storageaccountautom92bb_STORAGE": "hjk",
"MY_SENDGRID_API_KEY": "tyhuE",
"_AZURE_FUNCTION_SENDGRID_OUTPUT_ENV_NAME" : "GIS klop",
"_AZURE_FUNCTION_QUEUE_INPUT_ENV_NAME" : "msg"
}
}
尽管该功能在消息排队时响应消息,但它无法发送电子邮件。它抛出一个错误;
the following parameters are declared in function.json but not in Python: {'outputMessage'}
- 我怎样才能最好地解决这个问题?
- 外部绑定是否正确?