使用Predix的郵件通知服務

使用Predix的郵件通知服務

作者:唐翊國,開發者生態資深經理,GE數字集團

23年工作經驗,長期在杜邦、歐文斯科寧、莊信萬豐、通用電氣醫療等從事製造業信息化工作,規劃、實施了大量MES、SAP ERP、LIMS、BPM等項目,積累了豐富的製造業數字化轉型經驗。

 

如果您還沒有Predix試用帳號,請訪問

https://supportcentral.ge.com/esurvey/GE_survey/takeSurvey.html?form_id=18446744073709715720

申請。請務必準確提供您的信息,我們會以郵件方式通知您註冊結果。

 

如果您使用Windows操作系統,請參考http://blog.csdn.net/predixcn/article/details/53967673系列文章設置您的開發環境。

如果您使用Linux操作系統,請參考http://blog.csdn.net/predixcn/article/details/54093234系列文章設置您的開發環境。

 

在文章http://blog.csdn.net/predixcn/article/details/74639346 中我們已經創建了一個郵件通知服務,本文將示例如何使用Python語言來使用郵件通知服務的REST API。

由於通知服務使用了UAA來做用戶認證和授權管理,我們必須要在代碼中先獲得UAA 訪問令牌(access token),請參考文章

http://blog.csdn.net/predixcn/article/details/70213325 來學習如何獲得一個訪問令牌(access token)。

 

Predix的官方文檔https://docs.predix.io/en-US/content/service/operations/notification/using-configuration-services

介紹了郵件通知服務的各個REST API,本文將主要介紹如何配置郵件服務器和發送文本型郵件。

1、配置郵件服務器

注:本文中是以配置google的免費SMTP服務來作爲參考的。

對應的入口是:

https://ev-notification-service.run.aws-usw02-pr.ice.predix.io/v1/tenants/724967f8-9822-483b-adbf-4796f0ebaf9b/configurations/

其中的“724967f8-9822-483b-adbf-4796f0ebaf9b”是文章http://blog.csdn.net/predixcn/article/details/74639346中環境變量裏的tenantUuid。

使用POST協議,請求頭爲:

Authorization: bearer <token fromtrusted issuer>

Content-Type: application/json

請求數據部分是

[

    {

     "protocol": "smtp",

     "host": "smtp.gmail.com",

     "port": "587",

     "smtpAuth": "true",

     "smtpStarttlsEnable": "true",

     "mailFrom": "[email protected]",

     "mailUsername": "[email protected]",

     "mailPassword": "password",

     "mailReturnPath": null

    }

]

解釋完請求的細節,我們來學習如何在Python中實現它。

本文使用的Python 是2.7.13版本,編輯一個configuresmtp.py文件:

import requests

#Get UAA access token

data ={"grant_type":"client_credentials","client_id":"yourclientid","client_secret":"secret","response_type":"token"}

uaa ="https://ba67c2f3-3af7-436a-9c40-d86da25600b7.predix-uaa.run.aws-usw02-pr.ice.predix.io/oauth/token"

response = requests.post(uaa, data=data)

access_token =response.json()["access_token"]

#Create Configuration

url = "https://ev-notification-service.run.aws-usw02-pr.ice.predix.io/v1/tenants/724967f8-9822-483b-adbf-4796f0ebaf9b/configurations/"

payload = """[{

 "protocol": "smtp",

 "host": "smtp.gmail.com",

 "port": "587",

 "smtpAuth": "true",

 "smtpStarttlsEnable": "true",

 "mailFrom": "[email protected]",

 "mailUsername": "[email protected]",

 "mailPassword": "password",

 "mailReturnPath": null

}]"""

headers = {"Authorization":"Bearer " + access_token,"content-type":"application/json"}

r = requests.post(url, data=payload,headers=headers)

print r.json()["payload"]

在命令行裏運行,

python configuresmtp.py

應該會得到類似這樣的結果:

{

   "payload": [

       {

           "id": 486,

           "uuid": "83255967-ebaf-4b26-b218-6f6bfdecf3a0",

           "tenantUuid":"724967f8-9822-483b-adbf-4796f0ebaf9b",

           "protocol": "smtp",

           "host": "smtp.gmail.com",

            "port": 587,

           "smtpAuth": "true",

           "smtpStarttlsEnable": "true",

           "mailFrom": "[email protected]",

           "mailUsername": "[email protected]",

           "mailPassword": "password",

           "timestamp": 1499405790123,

           "lastUpdated": 1499405790128,

           "mailReturnPath": null

      }

   ],

   "uuid": "a8acfd42-02ca-424e-a459-a9c6a3139412",

   "status": 1000,

   "message": "Configuration(s) added successfully andsensitive information is encrypted for security reasons.",

   "timestamp": 1499311964526

}

 

2、發送文本類型的郵件

注:本文中是以配置google的免費SMTP服務來作爲參考的。

對應的入口是:

https://ev-notification-service.run.aws-usw02-pr.ice.predix.io/v1/tenants/724967f8-9822-483b-adbf-4796f0ebaf9b/email?configuration=83255967-ebaf-4b26-b218-6f6bfdecf3a0

其中的“724967f8-9822-483b-adbf-4796f0ebaf9b”是文章http://blog.csdn.net/predixcn/article/details/74639346中環境變量裏的tenantUuid,“83255967-ebaf-4b26-b218-6f6bfdecf3a0”是第一步裏創建的SMTP服務器的uuid。

使用POST協議,請求頭爲:

Authorization: bearer <token fromtrusted issuer>

Content-Type: application/json

請求數據部分是

{

 "body":"Content of the email message",

 "subject":"Subject of the email",

 "fromEmail":"[email protected]",

 "fromName" :"****",

 "important" :false,

 "recipients":

  [

    {

     "email":"[email protected]",

     "recipientName":"****",

     "type":"to"

    }

  ],

 "attachments":null

}

解釋完請求的細節,我們來學習如何在Python中實現它。

本文使用的Python 是2.7.13版本,編輯一個sendmail.py文件:

import requests

import json

#Get UAA access token

data ={"grant_type":"client_credentials","client_id":"yourclientid","client_secret":"secret","response_type":"token"}

uaa ="https://ba67c2f3-3af7-436a-9c40-d86da25600b7.predix-uaa.run.aws-usw02-pr.ice.predix.io/oauth/token"

response = requests.post(uaa, data=data)

access_token =response.json()["access_token"]

#Send email

url ="https://ev-notification-service.run.aws-usw02-pr.ice.predix.io/v1/tenants/724967f8-9822-483b-adbf-4796f0ebaf9b/email?configuration=83255967-ebaf-4b26-b218-6f6bfdecf3a0"

payload = """{

 "body":"this is a test from google SMTP server",

 "subject":"test from predix email notification -20170707",

 "fromEmail":"[email protected]",

 "fromName" :"****",

 "important" :false,

 "recipients":

  [

    {

     "email":"*****",

     "recipientName":"****",

     "type":"to"

    }

  ],

 "attachments":null

}"""

headers = {"Authorization":"Bearer " + access_token,"content-type":"application/json"}

r = requests.post(url, data=payload,headers=headers)

print r.json()

在命令行裏運行,

python sendmail.py

應該會得到類似這樣的結果:

{

   "payload": {

       "errors": [],

       "notificationReferenceUuid":"c263f6c1-9018-4cf3-b59a-451caa35e6b7"

   },

   "uuid": "bc8a3b04-4ff0-4dcd-854a-0b5965a940f3",

   "status": 1013,

   "message": "Email message is queued",

   "timestamp": 1499312487101

}

同時在郵箱裏可以看到發來的郵件:

 

在使用中您有任何問題,請訪問我們的論壇http://bbs.csdn.net/forums/GEPredix

GE數字集團的技術專家們會在線回答您的問題。

也請訪問我們在CSDN的Predix專區http://predix.csdn.net/ 瞭解更多Predix的內容和相關活動。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章