如何使用 Twilio 从用户的手机号码发送文本? [英] How to send a text from the user's cell phone number using Twilio?

查看:57
本文介绍了如何使用 Twilio 从用户的手机号码发送文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过我的 Meteor 应用程序以编程方式发送 SMS/文本消息;许多人为此推荐 twilio,并且有几个 Meteor Twilio 软件包(可通过 atmosphere 和/或谷歌搜索/binging 找到).我不知道这些包中的一个是否明显比另一个好,但至少现在,我正在使用 abhiaayer:meteor-twilio 包.

I want to programatically send SMS / text messages from my Meteor app; many people recommend twilio for this, and there are several Meteor Twilio packages (findable via atmosphere and/or googling/binging). I don't know if one of these packages is decidedly better than the other, but for now, at least, I'm using the abhiaayer:meteor-twilio package.

我担心的是,当您创建 Twilio 帐户时,他们会为您分配一个发件人"电话号码(显然,您不能只使用自己的).也许我误解了它是如何工作的,但在我沿着这条路走得太远之前,我想知道 SMS 的发件人——我的应用程序的用户——是否能够使用他们的em> 电话号码作为发件人"/发件人电话号码.

My concern is that, when you create a Twilio account, they assign you a "from" phone number (you can't, apparently, just use your own). Maybe I'm misunderstanding how this works, but before I go too far down this path, I want to know if the sender of the SMS -- IOW the user of my app -- will be able to use their phone number as the "from"/sender phone number.

毕竟,我的应用程序的全部意义在于允许用户向朋友/家人发送多个相同的文本(例如你在哪里?"或你还好吗?"),然后得到回复从他们那里回来(到他的手机,而不是 Twilio 提供的号码).

After all, the whole point of my app is to allow the user to send out multiple identical texts (such as "where are you?" or "are you okay?") to friends/family, and then get a response back from them (to his phone, not to a Twilio-supplied number).

如果可以将 Twilio 与您自己的电话号码一起用作发件人"/发送号码,是否有人对 Meteor 套餐首选项(abhiaayer、andreioprisan、DispatchMe)以及如何从 Meteor 实现这一点有任何建议?

If it's possible to use Twilio with your own phone number as the "from"/sending number, has anyone got suggestions on Meteor package preferences (abhiaayer, andreioprisan, DispatchMe) and how that can be accomplished from Meteor?

我想基本代码几乎是一样的,不管使用什么包;例如,这是 andreioprisan 包

I imagine the basic code would be pretty much the same, regardless of package used; e.g., here's an example from the andreioprisan package

twilio = Twilio(ACCOUNT_SID, AUTH_TOKEN);
  twilio.sendSms({
    to:'+16515556677', // Any number Twilio can deliver to
    from: '+14506667788', // A number you bought from Twilio and can use for outbound communication
    body: 'word to your mother.' // body of the SMS message
  }, function(err, responseData) { //this function is executed when a response is received from Twilio
    if (!err) { // "err" is an error received during the request, if any
      // "responseData" is a JavaScript object containing data received from Twilio.
      // A sample response from sending an SMS message is here (click "JSON" to see how the data appears in JavaScript):
      // http://www.twilio.com/docs/api/rest/sending-sms#example-1
      console.log(responseData.from); // outputs "+14506667788"
      console.log(responseData.body); // outputs "word to your mother."
    }
});

我确实创建了一个 Twilio 帐户,并且有他们分配给我的帐户的发件人"号码(不是我的实际手机号码)和 SID,但我不知道 AUTH_TOKEN 应该是什么.

I did create a Twilio account, and have the "from" number (not my actual cell phone number) they assigned my account, and the SID, but I don't kow what the AUTH_TOKEN should be.

如果 Twilio 无法实现我的意图,我可以做的就是编写应用程序,以便所有选定的收件人"号码都可以复制到剪贴板,以便用户可以将它们粘贴到他的 SMS 屏幕的收件人"文本框;但我希望让用户能够轻松点击按钮.

If what I intend is not possible from Twilio, what I may do is just write the app so that all the selected "to" numbers can be copied to the clipboard, so that the user can paste them into his SMS screen's "recipients" textbox; but I was hoping to afford the user the luxury of simply tapping a button.

推荐答案

我认为你不能这样做(使用 Twilio 从用户自己的手机号码发送).如果您的应用程序是实际的移动应用程序而不是 Web 应用程序,您可以使用 Android 或 iOS API 从用户的手机发送 SMS.我不确定这件事的细节,当然,对于 iOS,由于显而易见的原因,您似乎无法完全自动执行此操作(例如,您可以向所有用户的联系人发送垃圾邮件,这会给用户带来金钱成本)).

I don't think you can do this (send from the user's own cell number using Twilio). If your app is an actual mobile app as opposed to a web app you can use the Android or iOS APIs for sending SMS from the user's phone. I'm not sure of the details of this and certainly with iOS it looks like you can't do so fully automatically for obvious reasons (you could just spam all the user's contacts for instance, and this would incur a monetary cost to the user).

这里有一个 Cordova 插件可以做你想做的事:https://github.com/cordova-sms/cordova-sms-plugin(我没有使用过,所以不能保证它,但它似乎正在积极维护).

There's a Cordova plugin here which can do what you want: https://github.com/cordova-sms/cordova-sms-plugin (I haven't used it so can't vouch for it but it seems to be actively maintained).

如果它是一个网络应用程序,您建议将数字复制到剪贴板是迄今为止最直接的解决方案,我建议首先这样做.为完整起见,我在下面包含了一些详细信息和注意事项,说明如何以多种方式与 Twilio(或 Nexmo 等替代方案)进行双向通信,以防有人觉得有帮助.但这并不完全是微不足道的.单向沟通很多更容易!

If it is a web app, your suggesting of copying the numbers to clipboard is by far the most straightforward solution and I would recommend that in the first instance. For completeness I've included some details and considerations below of how you can do 2-way communication with Twilio (or alternatives such as Nexmo) in a couple of ways, in case someone finds it helpful. It's not exactly trivial though. 1-way communication is a lot easier!

用户在您的应用中键入消息并选择将其发送给谁.然后,您的应用程序连接到您的服务器,该服务器使用 Twilio API 向 N 个收件人发送 N 条消息.这些消息似乎来自 Twilio 提供的号码.

From your app the user types a message and selects who to send it to. You app then connects to your server which uses the Twilio API to send N messages to the N recipients. These messages will appear to come from the Twilio-provided number.

用户手动向您的 Twilio 号码发送 SMS,然后您会收到来自 Twilio 的带有消息详细信息的 Webhook 到您的服务器.您必须进行处理以确定将消息转发给谁,然后使用 Twilio API 执行此操作.收件人将再次看到来自 Twilio 提供的号码的消息.

The user manually sends an SMS to your Twilio number and then you receive a webhook to your server from Twilio with the message details. You have to do the processing to work out who to forward the message to and then use the Twilio API to do so. Again the recipients will see a message from the Twilio-provided number.

当收件人回复消息时,Twilio 会向您发送一个包含详细信息的网络钩子,您可以确定是谁发送了原始消息并将回复转发回原始发件人.

When a recipient replies to the message, Twilio sends you a webhook with the details and you can determine who sent the original message and forward the reply back to the original sender.

这里的一个明显缺陷是,如果多个用户向同一个人发送消息,则无法知道他们正在回复哪条消息.没有通过 SMS 传递的消息 ID,因此您必须使用多个发送号码(每个唯一发件人一个特定收件人).所需的号码数量基本上是用户必须回复的最多不同发件人的数量(这通常不可能事先解决,因此您必须调用 API 来即时提供新号码).

One of the obvious flaws here is that if multiple users send a message to the same person then there's no way of telling which message they are replying to. There's no message IDs passed with SMS so you have to use multiple sending numbers (one per unique sender to a particular recipient). The amount of numbers required is basically the most number of different senders one user would have to reply to (this is not generally possible to work out beforehand, so you would have to call the API to provision a new number on the fly).

举一个更具体的例子,假设您有 2 个用户(S1 & S2)和 3 个收件人(R1R2 & R3).您有 1 个 Twilio 提供的号码 (N1).

To give a more concrete example say you have 2 users (S1 & S2) and 3 recipients (R1, R2 & R3). You have 1 Twilio-provided number (N1).

  • S1 通过您的应用向 R1 发送消息,您使用 N1 通过 Twilio API 发送消息.R1 接收来自 N1 的消息.如果他们回复,您会收到来自 R1N1 的消息,因此您知道需要将其转发给 S1.

  • S1 sends a message to R1 via your app, you use N1 to send the message via the Twilio API. R1 receives the message from N1. If they reply, you receive a message to N1 from R1 so you know you need to forward it to S1.

S1 通过您的应用向 R2 发送消息,R2 尚未收到任何消息,因此您可以重复使用 >N1 发送消息.R2 回复 N1,您可以再次将其转发到 S1.如果这是在应用内提供的,没有进一步的问题,如果回复是通过 SMS 转发的,那么我们需要提供一个新号码 (N2) 以启用 S1回复R2的回复.

S1 sends a message to R2 via your app, R2 has not yet received any messages so you can reuse N1 to send the message. R2 replies to N1 and again you can forward it to S1. If this is delivered in-app, no further problems, if the reply is forwarded via SMS then we'd need to provision a new number (N2) to enable S1 to reply to R2's reply.

S2 通过您的应用向 R3 发送消息,就像以前一样,您可以重复使用 N1 并仍然正确路由回复.

S2 sends a message to R3 via your app, as before you can reuse N1 and still route the reply correctly.

现在如果S2R1发送消息,我们意识到R1已经收到来自S1<的消息/strong> 使用数字 N1.在这种情况下,我们不能使用 N1,因为我们无法确定回复是针对谁的.如果我们还没有,我们需要提供一个新号码 (N2),现在我们可以发送消息 R1.当R1回复N2时,我们知道回复需要转发到S2.

Now if S2 sends a message to R1, we realise that R1 has already received messages from S1 using number N1. In this case we can't use N1 because we cannot identify who the reply was intended for. If we haven't already, we need to provision a new number (N2) and now we can send the message so R1. When R1 replies to N2 we know the reply needs to be forwarded to S2.

发送给一个收件人的用户越多,您的号码池就越大(也越贵).可能值得实施某种超时(例如 72 小时)以便收件人可以回复.因此,在这种情况下,如果 S1 通过 N1 将消息发送到 R1 后超时已过期,我们可以重用 N1> 用于S2R1 之间的通信.显然,这并非完全万无一失,但可以降低成本.

The more users sending to one recipient, the bigger (and more expensive) your number pool gets. It's probably worth implementing some kind of timeout (say 72 hours) in which the recipient can reply. So in this case if the timeout had expired after S1 sent the message to R1 via N1, we could reuse N1 for the communication between S2 and R1. Obviously this isn't entirely foolproof but it could reduce costs.

从共用号码发送的另一个问题.我通过一个应用向 Dan 发送了一条消息,他从一个随机数接收了这条消息:

The other issue with sending from a pooled number. I send a message to Dan via an app, and he receives that message from a random number:

丹,你好吗?

Dan 怎么知道消息是谁发的?您必须为每条消息(或至少是每个对话线程中的第一条消息)添加一些标识符.

How does Dan know who sent the message? You'd have to add some identifier to every message (or at least the first in each conversation thread).

这篇关于如何使用 Twilio 从用户的手机号码发送文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆