如何使用 AWS SNS 通过 ExAws 发送短信

How to use AWS SNS to send sms usibng ExAws

我正在尝试通过 ExAws, ExAws.SNS

使用 AWS SNS 服务发送短信

我找不到合适的文档。

我尝试了以下代码,但出现错误。

%{name: "message", data_type: :string, value: {:string, "Hello..."}} |> ExAws.SNS.publish(%{phone_number: "+911010101010"})

错误

** (Protocol.UndefinedError) protocol String.Chars not implemented for %{data_type: :string, name: "message", value: {:string, "Hello..."}}. This protocol is implemented for: Atom, BitString, Date, DateTime, Decimal, Ecto.Date, Ecto.DateTime, Ecto.Time, Float, Geo.GeometryCollection, Geo.LineString, Geo.MultiLineString, Geo.MultiPoint, Geo.MultiPolygon, Geo.Point, Geo.PointM, Geo.PointZ, Geo.PointZM, Geo.Polygon, Integer, List, NaiveDateTime, Postgrex.Copy, Postgrex.Query, Postgrex.Stream, Time, URI, Version, Version.Requirement
    (elixir) /private/tmp/elixir-20180130-42559-1d5vx7w/elixir-1.6.1/lib/elixir/lib/string/chars.ex:3: String.Chars.impl_for!/1
    (elixir) /private/tmp/elixir-20180130-42559-1d5vx7w/elixir-1.6.1/lib/elixir/lib/string/chars.ex:22: String.Chars.to_string/1
    (elixir) lib/uri.ex:109: URI.encode_kv_pair/1
    (elixir) lib/enum.ex:1366: anonymous fn/4 in Enum.map_join/3
    (stdlib) lists.erl:1263: :lists.foldl/3
    (elixir) lib/enum.ex:1915: Enum.map_join/3
    (ex_aws) lib/ex_aws/operation/query.ex:24: ExAws.Operation.ExAws.Operation.Query.perform/2

我确定我准备查询的方式不正确。

显然 ExAws.SNS.publish/2 需要两个参数:binary() 类型的强制性 message 和可选的 opts,这是一个关键字列表(包括例如 :phone_number。)

根据 documentation,消息应该是 JSON 编码的字符串。也就是说,您应该按原样将消息传递给 ExAws.SNS.html#publish/2 并且选项应该是 Keyword,而不是 Map:

ExAws.SNS.publish("Hello...", [phone_number: "+911010101010"])