近期接触了一两个接口,一个是twilio短信推送接口,另一个是mailgun的邮件推送接口。
两个网站都在国外,如果在国内直接注册的话会不成功,原因是需要人机验证。 需要科学上网才能弹出人机验证的小窗体。
https://www.twilio.com/ twilio 本篇
https://www.mailgun.com/ mailgun 下一篇
两个网站都是 一步一步注册,碰到一些需要行用卡的地方直接跳过即可。不影响后续的接口调试。
但是如果之后需要应用到正式环境,那么付款等信息还是需要注册的。
Twilio
注册的教程网上有很多,在这里就不写了。
当信息全部填写完成后,你就得到了一个测试手机号码。也就是上图中的trial number。
我得到了一个美国华盛顿特区的号码,还有账户中的14.472刀(总共14.5刀)。
还有账户id 和key
官网上还贴心了给出了不同语言的API接口和说明文档。
调用
C#调用:
首先需要先用nuget 搜索 twilio 第一个就是
安装完成后,引用,
添加完两个引用后,
using Twilio;
using Twilio.Rest.Api.V2010.Account;
try
{
// Find your Account Sid and Token at twilio.com/console
// DANGER! This is insecure. See http://twil.io/secure
string accountSid = "你的id";
string authToken = "你的key";
TwilioClient.Init(accountSid, authToken);
var message = MessageResource.Create(
body: "This is the ship that made the Kessel Run in fourteen parsecs?",
from: new Twilio.Types.PhoneNumber("+twilio分配给你的手机号"),
to: new Twilio.Types.PhoneNumber("+86你的手机号")
);
Console.WriteLine(message.Sid);
}
catch (Exception ex)
{
Console.WriteLine(string.Format("失败,原因:{0}",ex.Message));
//throw;
}
当检查没问题后,运行。但是会报错。错误信息是:upgrade required。 检查了一下没发现问题。猜测是twilio提供的api有问题。
nodejs调用:
后来使用了提供的nodejs的版本。发现是可以使用的。
同样先安装库文件。
cnpm install --save twilio
完成后
const accountSid = '你的id';
const authToken = '你的key';
const client = require('twilio')(accountSid, authToken);
client.messages
.create({
body: 'This is the ship that made the Kessel Run in fourteen parsecs?',
from: '+twilio分配给你的手机号',
to: '+你的手机号'
})
.then(message => console.log(message.sid));
如果成功,返回值就是messagesid
SM7736b8e73381482ca25b415e81f58a27
如果失败,会返回相应的失败信息。
神奇的是,发送的手机号和我接受的手机号不是同一个。我接到了一条浙江嘉兴发来的短信。
发送一条短信的费用是0.028,系统赠送了14.5刀,大概517条短信的样子。