以前写了篇关于zabbix nagios的语音电话报警方案,这里用的是pageduty 和nexmo api进行语言和短信报警…. ….
这次是借用twilio实现,现在国外用twilio做运维语言方案的不少.
电话报警的优点,我想大家应该了解,或者有不少公司已经用到了,那就是解放劳动力。 我现在在一家公司做python程序员,其实对于这种自动电话语音报警是十分的无奈…. …. 半夜给你打电话,不是谁都能保持好心情的…. ….
Installation
关于 twilio模块的安装,不多说了
pip install twilio
from twilio.rest import TwilioRestClient #填写你在twilio的账号 account = "ACXXXXXXXXXXXXXXXXX" #填入你在twilio的app token token = "YYYYYYYYYYYYYYYYYY" client = TwilioRestClient(account, token) call = client.calls.create(to="110", from_="110", url="http://twimlets.com/holdmusic?Bucket=com.twilio.music.ambient") print(call.sid)
<Response> <Play> </Play> <Play> </Play> <Play> </Play> <Redirect/> </Response>
对于xml的各个指令的含义
You can use different combinations of the verbs below to create all kinds of interactive voice and SMS applications.
Say – Read text to the caller
Play – Play an audio file for the caller
Dial – Add another party to the call
Record – Record the caller’s voice
Gather – Collect digits the caller types on their keypad
Sms – Send an SMS message during a phone call
Hangup – Hang up the call
Queue – Add the caller to a queue of callers.
Redirect – Redirect call flow to a different TwiML document.
Pause – Wait before executing more instructions
Reject – Decline an incoming call without being billed.
这里是官网发送短信的实例代码
from twilio.rest import TwilioRestClient account = "ACXXXXXXXXXXXXXXXXX" token = "YYYYYYYYYYYYYYYYYY" client = TwilioRestClient(account, token) message = client.messages.create(to="+12316851234", from_="+15555555555", body="Hello there!")
可以借助于twiml来创建xml,更加容易的创建,而不需要自己调用python的xml模块再费劲了。
from twilio import twiml r = twiml.Response() r.say("Welcome to twilio!") print(str(r)) <?xml version="1.0" encoding="utf-8"?> <Response><Say>Welcome to twilio!</Say></Response>
我这里很多例子是来自github,twilio的python模块封装的已经很简单了,大家直接用就可以了。 模块还附带了不少别的功能,比如号码的查询啥的,这些倒是无所谓,毕竟大家自己有cmdb会做记录的。
oops
不错不错