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

时间:2018-07-31 07:34:55

标签: elixir amazon-sns

我正在尝试通过ExAwsExAws.SNS

使用AWS SNS服务发送SMS

我找不到适合的文档。

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

%{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

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

1 个答案:

答案 0 :(得分:2)

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

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

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