Skip to content

Commit

Permalink
first code
Browse files Browse the repository at this point in the history
  • Loading branch information
valerio-vaccaro committed Aug 13, 2018
1 parent 2324418 commit 4e3e078
Show file tree
Hide file tree
Showing 6 changed files with 336 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# -node-red-contrib-mastodon
Mastodon node for node-red platform.
# node-red-contrib-mastodon
Mastodon node for node-red platform, allow send and receive messages from your account on Mastodon.
72 changes: 72 additions & 0 deletions mastodon/getMessage.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<!--
Copyright 2018 Valerio Vaccaro - www.valeriovaccaro.it
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http:https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<script type="text/x-red" data-template-name="getMessage">
<div class="form-row">
<label for="node-input-topic"><i class="fa fa-tasks"></i> Access token</label>
<input type="text" id="node-input-access_token" placeholder="access_token">
</div>

<div class="form-row">
<label for="node-input-topic"><i class="fa fa-tag"></i> Timeout (in ms)</label>
<input type="text" id="node-input-timeout_ms" placeholder="60000">
</div>

<div class="form-row">
<label for="node-input-topic"><i class="fa fa-tag"></i> API url</label>
<input type="text" id="node-input-api_url" placeholder="https://mastodon.social/api/v1/">
</div>
</script>


<!-- Next, some simple help text is provided for the node. -->
<script type="text/x-red" data-help-name="getMessage">
<p>Scans a message on Mastodon</p>
</script>

<!-- Finally, the node type is registered along with all of its properties -->
<script type="text/javascript">
RED.nodes.registerType('getMessage', {
category: 'Mastodon', // the palette category
color: "#A6BBCF",
defaults: { // defines the editable properties of the node
name: {
value: ""
}, // along with default values.
access_token: {
value: "",
required: true
},
timeout_ms: {
value: "60000",
required: false
},
api_url: {
value: "https://mastodon.social/api/v1/",
required: false
}
},
inputs: 1, // set the number of inputs - only 0 or 1
outputs: 1, // set the number of outputs - 0 to n
icon: "inject.png", // set the icon (held in public/icons)
label: function() { // sets the default label contents
return this.name || this.topic || "getMessage";
},
labelStyle: function() { // sets the class to apply to the label
return this.name ? "node_label_italic" : "";
}
});
</script>
85 changes: 85 additions & 0 deletions mastodon/getMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* getMessage.js
* Requires mastodon
* Copyright 2018 Valerio Vaccaro - www.valeriovaccaro.it
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

module.exports = function(RED) {
var Masto = require('mastodon')

function getMessage(n) {
RED.nodes.createNode(this, n);

var msg = {};
var access_token;
var timeout_ms;
var api_url;
var node = this;

// Get varables from the node
this.access_token = n.access_token;
this.timeout_ms = n.timeout_ms;
this.api_url = n.api_url;

var M = new Masto({
access_token: access_token,
timeout_ms: timeout_ms, // optional HTTP request timeout to apply to all requests.
api_url: api_url, // optional, defaults to https://mastodon.social/api/v1/
})

// Status icon
this.status({
fill: "grey",
shape: "dot",
text: "Waiting"
});

this.on("input", function(msg) {
/*var id;
M.post('media', { file: fs.createReadStream('path/to/image.png') }).then(resp => {
id = resp.data.id;
M.post('statuses', { status: '#selfie', media_ids: [id] })
})*/
try {
/*M.post('statuses', {
status: msg.payload
});
this.status({
fill: "green",
shape: "dot",
text: "sent: " + msg.payload
});*/
} catch (err) {
console.log(err);
}
});

this.on("close", function() {
try {
this.status({
fill: "red",
shape: "dot",
text: "Stopped"
});
} catch (err) {
console.log(err);
}
});
}

// Register the node by name. This must be called before overriding any of the
// Node functions.
RED.nodes.registerType("getMessage", getMessage);
}
72 changes: 72 additions & 0 deletions mastodon/sendMessage.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<!--
Copyright 2018 Valerio Vaccaro - www.valeriovaccaro.it
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http:https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<script type="text/x-red" data-template-name="sendMessage">
<div class="form-row">
<label for="node-input-topic"><i class="fa fa-tasks"></i> Access token</label>
<input type="text" id="node-input-access_token" placeholder="access_token">
</div>

<div class="form-row">
<label for="node-input-topic"><i class="fa fa-tag"></i> Timeout (in ms)</label>
<input type="text" id="node-input-timeout_ms" placeholder="60000">
</div>

<div class="form-row">
<label for="node-input-topic"><i class="fa fa-tag"></i> API url</label>
<input type="text" id="node-input-api_url" placeholder="https://mastodon.social/api/v1/">
</div>
</script>


<!-- Next, some simple help text is provided for the node. -->
<script type="text/x-red" data-help-name="sendMessage">
<p>Scans a message on Mastodon</p>
</script>

<!-- Finally, the node type is registered along with all of its properties -->
<script type="text/javascript">
RED.nodes.registerType('sendMessage', {
category: 'Mastodon', // the palette category
color: "#A6BBCF",
defaults: { // defines the editable properties of the node
name: {
value: ""
}, // along with default values.
access_token: {
value: "",
required: true
},
timeout_ms: {
value: "60000",
required: false
},
api_url: {
value: "https://mastodon.social/api/v1/",
required: false
}
},
inputs: 1, // set the number of inputs - only 0 or 1
outputs: 1, // set the number of outputs - 0 to n
icon: "inject.png", // set the icon (held in public/icons)
label: function() { // sets the default label contents
return this.name || this.topic || "sendMessage";
},
labelStyle: function() { // sets the class to apply to the label
return this.name ? "node_label_italic" : "";
}
});
</script>
85 changes: 85 additions & 0 deletions mastodon/sendMessage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* sendMessage.js
* Requires mastodon
* Copyright 2018 Valerio Vaccaro - www.valeriovaccaro.it
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/

module.exports = function(RED) {
var Masto = require('mastodon')

function sendMessage(n) {
RED.nodes.createNode(this, n);

var msg = {};
var access_token;
var timeout_ms;
var api_url;
var node = this;

// Get varables from the node
this.access_token = n.access_token;
this.timeout_ms = n.timeout_ms;
this.api_url = n.api_url;

var M = new Masto({
access_token: access_token,
timeout_ms: timeout_ms, // optional HTTP request timeout to apply to all requests.
api_url: api_url, // optional, defaults to https://mastodon.social/api/v1/
})

// Status icon
this.status({
fill: "grey",
shape: "dot",
text: "Waiting"
});

this.on("input", function(msg) {
/*var id;
M.post('media', { file: fs.createReadStream('path/to/image.png') }).then(resp => {
id = resp.data.id;
M.post('statuses', { status: '#selfie', media_ids: [id] })
})*/
try {
M.post('statuses', {
status: msg.payload
});
this.status({
fill: "green",
shape: "dot",
text: "sent: " + msg.payload
});
} catch (err) {
console.log(err);
}
});

this.on("close", function() {
try {
this.status({
fill: "red",
shape: "dot",
text: "Stopped"
});
} catch (err) {
console.log(err);
}
});
}

// Register the node by name. This must be called before overriding any of the
// Node functions.
RED.nodes.registerType("sendMessage", sendMessage);
}
20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "node-red-contrib-mastodon",
"version": "0.0.2",
"description": "Mastodon node for node-red platform",
"author": "Valerio Vaccaro <[email protected]>",
"dependencies": {
"mastodon": "latest"
},
"repository": {
"type": "git",
"url": "https://github.com/valerio-vaccaro/node-red-contrib-mastodon"
},
"keywords": ["node-red", "mastodon"],
"node-red": {
"nodes": {
"sendMessage": "mastodon/sendMessage.js",
"getMessage": "mastodon/getMessage.js"
}
}
}

0 comments on commit 4e3e078

Please sign in to comment.