cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              .content
  #editor-wrapper
    #editor.node-editor
  #telegram
    .messages(ref="messages")
      .message(v-for="message in messages", :class="{owner: users[message.user].owner}")
        .avatar
          img(:src="users[message.user].avatar")
        .text-wrap
          .text(v-html="formatMsg(message.text)")
    .form.row.s12
      .mui-textfield.mui-col-md-8
        input.validate(id="first_name", v-model="textfield")
      .mui-col-md-4
        button.mui-btn.s12(@click="sendOwner(textfield); textfield=''") Send
            
          
!
            
              body,html
  margin: 0
  height: 100%
  
$margin: 15px  
$formheight: 80px
  
.content
  display: flex
  height: 100%
  #editor-wrapper
    flex: 2
    #editor
      .socket.string
        background: #6f377e
      .socket.action
        background: white
        border-color: grey
        border-radius: 3px
        width: 15px
      .node.message-event 
        background: #767676
      .input-control input
        width: 140px
  #telegram
    margin: $margin
    flex: 1
    height: calc(100% - 2 * #{$margin})
    border: 2px solid blue
    background: url('http://78.media.tumblr.com/913fc95846350c30232a5608a322b78e/tumblr_obykzyjxZt1vbllj8o4_1280.png')
    background-size: cover
    .messages
      height: calc(100% - #{$formheight})
      max-height: calc(100% - #{$formheight})
      overflow: auto
      .message
        display: flex
        padding: 10px
        &.owner
          flex-direction: row-reverse
        .avatar
          display: inline-block
          border-radius: 100%
          flex-grow: 0
          flex-shrink: 0
          height: 40px
          margin: 0 12px
          overflow: hidden
          img
            height: 40px
            width: 40px
        .text-wrap
          display: inline-block
          .text
            padding: 5px
            background: rgba(255,255,255,0.6)
            border-radius: 12px
            a
              color: #4f6
    .form
      background: #839aed
      height: $formheight
            
          
!
            
              var botAvatar =
  "https://robohash.org/liberovelitdolores.bmp?size=50x50&set=set1";
var userAvatar =
  "http://icons.iconarchive.com/icons/visualpharm/must-have/256/User-icon.png";

var telegram = new Vue({
  el: "#telegram",
  data: {
    botSleep: 1000,
    textfield: "",
    users: [
      { avatar: botAvatar, username: "Halmstad" },
      { avatar: userAvatar, username: "Wako", owner: true }
    ],
    messages: [
      {
        user: 0,
        text:
          "Hello, I'm a bot in a Messenger. I can be programmed without writing any code. Just use the node editor on the left"
      },
      {
        user: 0,
        text:
          "This example is a demonstration of [Rete.js](https://github.com/retejsrete) library. Based on it you can make your powerful editor"
      },
      {
        user: 0,
        text:
          "If you like the project, you can [support it](https://github.com/retejs/rete#donate)"
      }
    ]
  },
  methods: {
    formatMsg(msg) {
      return msg.replace(
        /\[(.+?)\]\((.+?)\)/g,
        '<a target="_blank" href="$2">$1</a>'
      );
    },
    onMessage() {
      var ms = this.$refs.messages;
      setTimeout(() => {
        ms.scrollTop = ms.scrollHeight;
      }, 100);
    },
    sendOwner(message) {
      this.messages.push({ user: 1, text: message });
      receiveBot(message);
      this.onMessage();
    },
    sendBot(message) {
      this.messages.push({ user: 0, text: message });
      this.onMessage();
    }
  }
});

var onMessageTask = null;
function receiveBot(msg) {
  setTimeout(async () => {
    await onMessageTask.run(msg);
  }, telegram.botSleep);
}

function receiveUser(msg) {
  telegram.sendBot(msg);
}

var actSocket = new Rete.Socket("Action");
var strSocket = new Rete.Socket("String");

const JsRenderPlugin = {
  install(editor, params = {}) {
    editor.on("rendercontrol", ({ el, control }) => {
      if (control.render && control.render !== "js") return;

      control.handler(el, editor);
    });
  }
};

class InputControl extends Rete.Control {
  constructor(key) {
    super();
    this.render = "js";
    this.key = key;
  }

  handler(el, editor) {
    var input = document.createElement("input");
    el.appendChild(input);

    var text = this.getData(this.key) || "Some message..";

    input.value = text;
    this.putData(this.key, text);
    input.addEventListener("change", () => {
      this.putData(this.key, input.value);
    });
  }
}

class MessageEventComponent extends Rete.Component {
  constructor() {
    super("Message event");
    this.task = {
      outputs: { act: "option", text: "output" },
      init(task) {
        onMessageTask = task;
      }
    };
  }

  builder(node) {
    var out1 = new Rete.Output("act", "Action", actSocket);
    var out2 = new Rete.Output("text", "Text", strSocket);
    return node.addOutput(out1).addOutput(out2);
  }

  worker(node, inputs, msg) {
    return { text: msg };
  }
}

class MessageSendComponent extends Rete.Component {
  constructor() {
    super("Message send");
    this.task = {
      outputs: {}
    };
  }

  builder(node) {
    var inp1 = new Rete.Input("act", "Action", actSocket, true);
    var inp2 = new Rete.Input("text", "Text", strSocket);

    var ctrl = new InputControl("text");
    inp2.addControl(ctrl);

    return node.addInput(inp1).addInput(inp2);
  }

  worker(node, inputs) {
    var text = inputs["text"] ? inputs["text"][0] : node.data.text; //default text
    console.log("msg send");
    receiveUser(text);
  }
}

class MessageMatchComponent extends Rete.Component {
  constructor() {
    super("Message match");
    this.task = {
      outputs: { t: "option", f: "option" }
    };
  }

  builder(node) {
    var inp1 = new Rete.Input("act", "Action", actSocket);
    var inp2 = new Rete.Input("text", "Text", strSocket);
    var out1 = new Rete.Output("t", "True", actSocket);
    var out2 = new Rete.Output("f", "False", actSocket);
    var ctrl = new InputControl("regexp");

    return node
      .addControl(ctrl)
      .addInput(inp1)
      .addInput(inp2)
      .addOutput(out1)
      .addOutput(out2);
  }
  worker(node, inputs) {
    var text = inputs["text"] ? inputs["text"][0] : "";

    if (!text.match(new RegExp(node.data.regexp, "gi"))) this.closed = ["t"];
    else this.closed = ["f"];
  }
}

class MessageComponent extends Rete.Component {
  constructor() {
    super("Message");
    this.task = {
      outputs: { text: "output" }
    };
  }

  builder(node) {
    var out = new Rete.Output("text", "Text", strSocket);
    var ctrl = new InputControl("text");

    return node.addControl(ctrl).addOutput(out);
  }

  worker(node, inputs) {
    return { text: node.data.text };
  }
}

var components = [
  new MessageEventComponent(),
  new MessageSendComponent(),
  new MessageMatchComponent(),
  new MessageComponent()
];

var container = document.getElementById("editor");
var editor = new Rete.NodeEditor("demo@0.1.0", container);
editor.use(AlightRenderPlugin);
editor.use(ConnectionPlugin);
editor.use(ContextMenuPlugin);
editor.use(JsRenderPlugin);
editor.use(TaskPlugin);

var engine = new Rete.Engine("demo@0.1.0");

components.map(c => {
  editor.register(c);
  engine.register(c);
});

editor
  .fromJSON({
    id: "demo@0.1.0",
    nodes: {
      "1": {
        id: 1,
        data: {},
        group: null,
        inputs: {},
        outputs: {
          act: { connections: [{ node: 4, input: "act" }] },
          text: { connections: [{ node: 4, input: "text" }] }
        },
        position: [44, 138],
        name: "Message event"
      },
      "2": {
        id: 2,
        data: {},
        group: null,
        inputs: {
          act: { connections: [{ node: 4, output: "f" }] },
          text: { connections: [{ node: 3, output: "text" }] }
        },
        outputs: {},
        position: [673.2072854903905, 194.82554933538893],
        name: "Message send"
      },
      "3": {
        id: 3,
        data: { text: "ッ" },
        group: null,
        inputs: {},
        outputs: { text: { connections: [{ node: 2, input: "text" }] } },
        position: [334.3043696236001, 298.2715347978209],
        name: "Message"
      },
      "4": {
        id: 4,
        data: { regexp: ".*hello.*" },
        group: null,
        inputs: {
          act: { connections: [{ node: 1, output: "act" }] },
          text: { connections: [{ node: 1, output: "text" }] }
        },
        outputs: {
          t: { connections: [{ node: 5, input: "act" }] },
          f: { connections: [{ node: 2, input: "act" }] }
        },
        position: [333.40730287320383, 22.1000138522662],
        name: "Message match"
      },
      "5": {
        id: 5,
        data: {},
        group: null,
        inputs: {
          act: { connections: [{ node: 4, output: "t" }] },
          text: { connections: [{ node: 6, output: "text" }] }
        },
        outputs: {},
        position: [670.6284575254812, -103.66713461561366],
        name: "Message send"
      },
      "6": {
        id: 6,
        data: { text: "Hello!" },
        group: null,
        inputs: {},
        outputs: { text: { connections: [{ node: 5, input: "text" }] } },
        position: [317.85328833563574, -143.3955998177927],
        name: "Message"
      }
    },
    groups: {}
  })
  .then(() => {
    editor.on("error", err => {
      alertify.error(err.message);
    });

    editor.on(
      "process connectioncreated connectionremoved nodecreated",
      async function() {
        if (engine.silent) return;
        console.log("process");
        await engine.abort();
        await engine.process(editor.toJSON());
      }
    );

    editor.trigger("process");
    editor.view.resize();
    AreaPlugin.zoomAt(editor);
  });

            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console