UNPKG

node-red-contrib-hangouts

Version:
226 lines (210 loc) 9.91 kB
<script type="text/javascript"> RED.nodes.registerType("hangouts-config", { category: "config", defaults: { name: {value:"", required: false}, debug: {value:false, required: true} }, credentials: { token: {type: "text"}, cookiejar: {type: "password"} }, label: function() { return this.name ? this.name : "Hangouts Config"; } }); </script> <script type="text/x-red" data-template-name="hangouts-config"> <div class="form-tips"> To login, you need to login using OAuth and provide the access token to the API. Google has changed the way to authenticate and the way to optain this key got complicated. Open the link and follow the steps: <a target="_blank" href="https://accounts.google.com/o/oauth2/programmatic_auth?hl=en&scope=https%3A%2F%2Fwww.google.com%2Faccounts%2FOAuthLogin+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email&client_id=936475272427.apps.googleusercontent.com&access_type=offline&delegated_client_id=183697946088-m3jnlsqshjhh5lbvg05k46q1k4qqtrgn.apps.googleusercontent.com&top_level_cookie=1">Link</a> <ol> <li>Enter your username, click next.</li> <li>Right click page background, inspect</li> <li>Go to the network tab.</li> <li>Enter your password, click sign in</li> <li>Click the first row in the network panel, the one that says "programmatic_auth"</li> <li>Scroll down in the right-side panel, find "set-cookie"</li> <li>Your code should be there, after "oauth_code=", up to but not including the semicolon.</li> <li>Copy it and use it.</li> </ol> </div> <div class="form-row"> <label for="node-config-input-name"><i class="icon-bookmark"></i>Name</label> <input type="text" id="node-config-input-name" /> </div> <div class="form-row"> <label for="node-config-input-token"><i class="icon-bookmark"></i>Token</label> <input type="text" id="node-config-input-token" /> </div> <div class="form-row"> <label>&nbsp;</label> <input type="checkbox" id="node-config-input-debug" placeholder="" style="display: inline-block; width: auto; vertical-align: top;"> <label for="node-config-input-debug" style="width: 70%;">Debug mode</label> </div> </script> <script type="text/javascript"> RED.nodes.registerType("hangouts-in", { category: "input", color: "#7bc54a", icon: "hangouts.png", defaults: { config: {type:"hangouts-config",required:true}, name: {value:"", required: false}, topic: { value:"", required: false}, conversationId: { value:"", required: false}, suppress: {value:true, required: true} }, inputs:0, outputs:1, label: function() { return this.name ? this.name : "Hangouts"; }, oneditprepare: function() { function formatConversations(conversation) { var string = ""; if (conversation.name !== null) { string += conversation.name+" | "; }; string += conversation.participants.join(", "); return string; } function toggleSelect() { var current = $('#node-input-conversationId').val(); $('#node-input-conversationId').replaceWith('<input type="text" id="node-input-conversationId" style="width: 60%;"/>'); $('#node-input-conversationId').val(current); } function toggleInput() { var current = $('#node-input-conversationId').val(); $('#node-input-conversationId').replaceWith('<select id="node-input-conversationId" style="width: 60%;"></select>'); $.get( 'hangouts/conversations', { id: $('#node-input-config').val() }) .done( function(data) { var conversations = JSON.parse(data); if(conversations.length <= 0) { RED.notify("The configuration node is either not deployed or you don't have any conversations within this account.", "error"); } conversations.forEach(function(conversation) { $('#node-input-conversationId').append('<option value="' + conversation.id + '">' + formatConversations(conversation) + '</option>'); }); $('#node-input-conversationId').val(current); }) .fail(function() { RED.notify("Something went wrong.", "error"); }); } $('#node-input-refreshbtn').click(function() { if ($('#node-input-conversationId').prop("tagName") === "INPUT") { toggleInput(); } else { toggleSelect(); } }); } }); </script> <script type="text/x-red" data-template-name="hangouts-in"> <div class="form-row"> <label for="node-input-config"><i class="fa fa-user"></i> Config</label> <input type="text" id="node-input-config"> </div> <div class="form-row"> <label for="node-input-name"><i class="fa fa-header"></i> Name</label> <input type="text" id="node-input-name" /> </div> <div class="form-row"> <label for="node-input-topic"><i class="icon-bookmark"></i> Topic</label> <input type="text" id="node-input-topic" /> </div> <div class="form-row"> <label for="node-input-conversationId"><i class="fa fa-comments"></i> Chat</label> <input type="text" id="node-input-conversationId" style="width: 60%;"/> <a id="node-input-refreshbtn" class="editor-button"><i class="fa fa-refresh"></i></a> </div> <div class="form-row"> <label>&nbsp;</label> <input type="checkbox" id="node-input-suppress" placeholder="" style="display: inline-block; width: auto; vertical-align: top;"> <label for="node-input-suppress" style="width: 70%;">Suppress own messages</label> </div> </script> <script type="text/x-red" data-help-name="hangouts-in"> <p>Receive messages from Google hangouts</p> <p>The input node is used to receive messages from the hangouts network. You can use the conversation id to filter incoming messages by conversation. If the field is empty, all messages are accepted. Make sure to use a valid conversation id. Since the node will also receive messages send from the account, you can suppress this messages over the checkbox. The message contains the message in <b>msg.payload</b>, the conversation id in <b>msg.conversationId</b> and the complete message event object in <b>msg.event</b>.</p> </script> <script type="text/javascript"> RED.nodes.registerType("hangouts-out", { category: "output", color: "#7bc54a", icon: "hangouts.png", defaults: { config: {type:"hangouts-config",required:true}, name: {value:"", required: false}, conversationId: {value:"", required: false} }, inputs:1, outputs:0, label: function() { return this.name ? this.name : "Hangouts"; }, oneditprepare: function() { function formatConversations(conversation) { var string = ""; if (conversation.name !== null) { string += conversation.name+" | "; }; string += conversation.participants.join(", "); return string; } function toggleSelect() { var current = $('#node-input-conversationId').val(); $('#node-input-conversationId').replaceWith('<input type="text" id="node-input-conversationId" style="width: 60%;"/>'); $('#node-input-conversationId').val(current); } function toggleInput() { var current = $('#node-input-conversationId').val(); $('#node-input-conversationId').replaceWith('<select id="node-input-conversationId" style="width: 60%;"></select>'); $.get( 'hangouts/conversations', { id: $('#node-input-config').val() }) .done( function(data) { var conversations = JSON.parse(data); if(conversations.length <= 0) { RED.notify("The configuration node is either not deployed or you don't have any conversations within this account.", "error"); } conversations.forEach(function(conversation) { $('#node-input-conversationId').append('<option value="' + conversation.id + '">' + formatConversations(conversation) + '</option>'); }); $('#node-input-conversationId').val(current); }) .fail(function() { RED.notify("Something went wrong.", "error"); }); } $('#node-input-refreshbtn').click(function() { if ($('#node-input-conversationId').prop("tagName") === "INPUT") { toggleInput(); } else { toggleSelect(); } }); } }); </script> <script type="text/x-red" data-template-name="hangouts-out"> <div class="form-row"> <label for="node-input-config"><i class="fa fa-user"></i> Config</label> <input type="text" id="node-input-config"> </div> <div class="form-row"> <label for="node-input-name"><i class="fa fa-header"></i> Name</label> <input type="text" id="node-input-name" /> </div> <div class="form-row"> <label for="node-input-conversationId"><i class="fa fa-comments"></i> Chat</label> <input type="text" id="node-input-conversationId" style="width: 60%;"/> <a id="node-input-refreshbtn" class="editor-button"><i class="fa fa-refresh"></i></a> </div> </script> <script type="text/x-red" data-help-name="hangouts-out"> <p>Receive messages from Google hangouts</p> <p>The output node is used to send messages to an existing conversation. You can use the conversation id on the properties or provide a valid conversation id in <b>msg.conversationId</b>. If <b>msg.links</b> contains an array of hrefs they will be appended to the message. If <b>msg.image</b> is a filepath it will be appended to the message. The <b>msg.payload</b> will be send to the contacts.</p> </script>