Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New template based "custom" channel to replace external channels #380

Open
rowanseymour opened this issue Oct 5, 2021 · 1 comment
Open

Comments

@rowanseymour
Copy link
Member

rowanseymour commented Oct 5, 2021

The existing external/EX channel type is getting a bit unwieldy and still lacks attachment support. We could introduce a new "custom" channel type which uses the same templates/expressions that users are familiar with from flows...

  • incoming payloads are assumed to be JSON or converted to a JSON representation, e.g. a URL encoded payload like ?text=Hello+World can be converted to {"text": "Hello World"}
  • fields are extracted from that JSON using excellent expressions

For example a channel sends...

{
    "from": "+124215151",
    "text": "Hello",
    "media": [{"content_type": "image", "url": "http://example.com/test.jpg"}]
}

And the channel extracts from, text and attachments from that using 3 expressions:

  • from: @("tel:" & body.from)
  • text: @body.text
  • attachments: @(foreach(body.media, extract, "url"))

For outgoing messages, users again use expressions for the URL and body, e.g.

  • url: http://mychannel.com/send/@channel.uuid
  • body:
@(json(object(
  "from": channel.address,
  "to": msg.to.path,
  "text": msg.text,
  "media": foreach(msg.attachments, attachment_parts)
)))
@rowanseymour
Copy link
Member Author

Maybe simpler to say that the channel expects a payload for incoming messages like..

{
    "from": "tel:+124215151",
    "text": "Hello",
    "attachments": [{"type": "image", "url": "http://example.com/test.jpg"}]
}

And the user provides an Excellent template which constructs that from whatever the channel actually sends. If the user controls the other end of the channel they can send that as the body and omit the template. The template is a transformation, same as the template used to construct the body for outgoing messages.

URL params could be made accessible in the template via the object @url which simplifies access to single vs multiple values. For example if the channel receives a request like...

http://app.rapidpro.io/c/cus/1234/receive?text=Hello&from=%2B124215151&attachment=http%3A%2F%2Fexample.com%2Ftest1.jpg&attachment=http%3A%2F%2Fexample.com%2Ftest2.jpg

Then @url will be set to...

{
  "__default__": "http://app.rapidpro.io/c/cus/123...com%2Ftest2.jpg",
  "params": {
    "text":  {
      "__default__": "Hello",
      "all": ["Hello"]
    },
    "from": {
      "__default__": "+124215151",
      "all": ["+124215151"]
    },
    "attachment":  {
      "__default__": "http://example.com/test1.jpg",
      "all": ["http://example.com/test1.jpg", "http://example.com/test2.jpg"],
    }
  }
}

And the user could provide a template as follows to transform that accordingly...

@(json(object(
  "from": "tel:" & url.params.from,
  "text": url.params.text,
  "attachments": foreach(url.params.attachment.all, (a) => object("type", "image", "url": a))
)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant