简体   繁体   English

Nexmo接收SMS Java

[英]Nexmo Receiving SMS Java

I was wondering what approach I should use to be able to receive messages through Nexmo. 我想知道应该使用哪种方法才能通过Nexmo接收消息。 Has anybody had any experience on this issue because Nexmo doesn't seem to have clear documentation on how to receive messages through there libraries. 是否有人对此问题有任何经验,因为Nexmo似乎没有关于如何通过那里的库接收消息的清晰文档。 Any help would be wonderful. 任何帮助都会很棒。

For each Nexmo number you own, you can configure a URL which will be called by Nexmo when an SMS is received at that number. 对于您拥有的每个Nexmo号码,您可以配置一个URL,当以该号码接收到SMS时,Nexmo将调用该URL。 The GET request will contain information about the received SMS as request params. GET请求将包含有关接收到的SMS的信息作为请求参数。

A little complexity is added (while you're developing) because Nexmo needs to be able to reach a URL that is hosted on your development machine, which is probably not publicly available on the Internet! (在开发过程中)增加了一些复杂性,因为Nexmo需要能够访问开发计算机上托管的URL,该URL可能在Internet上不公开! For this, you'll want to run something like Ngrok which will provide a tunnel to a port on your local machine with a public URL. 为此,您需要运行Ngrok之类的东西 ,它将使用公共URL提供到本地计算机端口的隧道。

I'd recommend starting with a simple servlet that prints out its params: 我建议从一个简单的servlet开始,该servlet打印出其参数:

public class InboundSMSServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req,
                         HttpServletResponse resp)
            throws ServletException,
                   java.io.IOException {
        System.out.println("Received: " + req.getMethod());
        for (String param : Collections.list(req.getParameterNames())) {
            String value = req.getParameter(param);
            System.out.println(param + ": " + value);
        }
    }
}

... configure it to a convenient URL ... ...将其配置为方便的URL ...

<servlet>
    <servlet-name>inbound-sms</servlet-name>
    <servlet-class>getstarted.InboundSMSServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>inbound-sms</servlet-name>
    <url-pattern>/inbound</url-pattern>
</servlet-mapping>

Run both your servlet container and ngrok at the same time and check that the ngrok URL with /YOUR_PROJECT_NAME/inbound at the end works as expected. 同时运行您的servlet容器和ngrok,并检查末尾带有/YOUR_PROJECT_NAME/inbound的ngrok URL是否按预期工作。 Then go into the Nexmo dashboard, Your Numbers , and hit Edit on the number you want to receive SMS messages to. 然后进入Nexmo仪表板Your Numbers ,然后在要接收SMS消息的号码上单击Edit。 Enter the Ngrok URL you tested above. 输入您在上面测试的Ngrok URL。

Now send an SMS to the number you configured, and you should see the contents of your message printed to the console; 现在,将SMS发送到您配置的号码,您应该看到打印到控制台的消息内容。 something like: 就像是:

Received: GET
messageId: 0B0000004A2D09D9
to: 447520666777
text: Hello Nexmo!
msisdn: 447720123123
type: text
keyword: HELLO
message-timestamp: 2017-04-27 14:41:32

The details of how this works are documented on the Nexmo site Nexmo网站上记录了如何工作的详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM