[CCSDK-28] populated the seed code for dgbuilder
[ccsdk/distribution.git] / dgbuilder / core_nodes / social / 27-twitter.html
1 <!--
2   Copyright 2013 IBM Corp.
3
4   Licensed under the Apache License, Version 2.0 (the "License");
5   you may not use this file except in compliance with the License.
6   You may obtain a copy of the License at
7
8   http://www.apache.org/licenses/LICENSE-2.0
9
10   Unless required by applicable law or agreed to in writing, software
11   distributed under the License is distributed on an "AS IS" BASIS,
12   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   See the License for the specific language governing permissions and
14   limitations under the License.
15 -->
16
17 <script type="text/x-red" data-template-name="twitter-credentials">
18     <div class="form-row" id="node-config-twitter-row"></div>
19     <input type="hidden" id="node-config-input-screen_name">
20 </script>
21
22 <script type="text/javascript">
23 (function() {
24     var twitterConfigNodeId = null;
25     var twitterConfigNodeIntervalId = null;
26
27     function showTwitterAuthStart() {
28         var pathname = document.location.pathname;
29         if (pathname.slice(-1) != "/") {
30             pathname += "/";
31         }
32         var callback = encodeURIComponent(location.protocol+"//"+location.hostname+":"+location.port+pathname+"twitter-credentials/"+twitterConfigNodeId+"/auth/callback");
33         $("#node-config-dialog-ok").button("disable");
34         $("#node-config-twitter-row").html('<div style="text-align: center; margin-top: 20px; "><a class="btn" id="node-config-twitter-start" href="twitter-credentials/'+twitterConfigNodeId+'/auth?callback='+callback+'" target="_blank">Click here to authenticate with Twitter.</a></div>');
35         $("#node-config-twitter-start").click(function() {
36             twitterConfigNodeIntervalId = window.setTimeout(pollTwitterCredentials,2000);
37         });
38     }
39     function updateTwitterScreenName(sn) {
40         $("#node-config-input-screen_name").val(sn);
41         $("#node-config-twitter-row").html('<label><i class="fa fa-user"></i> Twitter ID</label><span class="input-xlarge uneditable-input">'+sn+'</span>');
42     }
43     function pollTwitterCredentials(e) {
44         $.getJSON('credentials/twitter-credentials/'+twitterConfigNodeId,function(data) {
45             if (data.screen_name) {
46                 updateTwitterScreenName(data.screen_name);
47                 twitterConfigNodeIntervalId = null;
48                 $("#node-config-dialog-ok").button("enable");
49             } else {
50                 twitterConfigNodeIntervalId = window.setTimeout(pollTwitterCredentials,2000);
51             }
52         })
53     }
54     RED.nodes.registerType('twitter-credentials',{
55         category: 'config',
56         defaults: {
57             screen_name: {value:""}
58         },
59         credentials: {
60             screen_name: {type:"text"},
61             access_token: {type: "password"},
62             access_token_secret: {type:"password"}
63         },
64             
65         label: function() {
66             return this.screen_name;
67         },
68         exportable: false,
69         oneditprepare: function() {
70             twitterConfigNodeId = this.id;
71             if (!this.screen_name || this.screen_name == "") {
72                 showTwitterAuthStart();
73             } else {
74                 if (this.credentials.screen_name) {
75                     updateTwitterScreenName(this.credentials.screen_name);
76                 } else {
77                     showTwitterAuthStart();
78                 }
79             }
80         },
81         oneditsave: function() {
82             if (twitterConfigNodeIntervalId) {
83                 window.clearTimeout(twitterConfigNodeIntervalId);
84             }
85         },
86         oneditcancel: function(adding) {
87             if (twitterConfigNodeIntervalId) {
88                 window.clearTimeout(twitterConfigNodeIntervalId);
89             }
90         }
91     });
92 })();
93 </script>
94
95 <script type="text/x-red" data-template-name="twitter in">
96     <div class="form-row">
97         <label for="node-input-twitter"><i class="fa fa-user"></i> Log in as</label>
98         <input type="text" id="node-input-twitter">
99     </div>
100     <div class="form-row">
101         <label for="node-input-user"><i class="fa fa-search"></i> Search</label>
102         <select type="text" id="node-input-user" style="display: inline-block; vertical-align: middle; width:60%;">
103             <option value="false">all public tweets</option>
104             <option value="true">the tweets of who you follow</option>
105             <option value="user">the tweets of specific users</option>
106             <option value="dm">your direct messages</option>
107         </select>
108     </div>
109     <div class="form-row" id="node-input-tags-row">
110         <label for="node-input-tags"><i class="fa fa-tags"></i> <span id="node-input-tags-label">for</span></label>
111         <input type="text" id="node-input-tags" placeholder="comma-separated words, @ids, #tags">
112     </div>
113     <div class="form-row">
114         <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
115         <input type="text" id="node-input-name" placeholder="Name">
116     </div>
117     <div class="form-tips">Tip: Use commas without spaces between multiple search terms. Comma = OR, Space = AND.
118     <br/>The Twitter API WILL NOT deliver 100% of all tweets.
119     <br/>Tweets of who you follow will include their retweets and favourites.</div>
120 </script>
121
122 <script type="text/x-red" data-help-name="twitter in">
123     <p>Twitter input node. Can be used to search either:
124     <ul><li>the public or a user's stream for tweets containing the configured search term</li>
125         <li>all tweets by specific users</li>
126         <li>direct messages received by the authenticated user</li>
127     </ul></p>
128     <p>Use space for <i>and</i> and comma , for <i>or</i> when searching for multiple terms.</p>
129     <p>Sets the <b>msg.topic</b> to <i>tweets/</i> and then appends the senders screen name.</p>
130     <p>Sets <b>msg.location</b> to the tweeters location if known.</p>
131     <p>Sets <b>msg.tweet</b> to the full tweet object as documented by <a href="https://dev.twitter.com/docs/platform-objects/tweets">Twitter</a>.
132     <p><b>Note:</b> when set to a specific user's tweets, or your direct messages, the node is subject to
133       Twitter's API rate limiting. If you deploy the flows multiple times within a 15 minute window, you may
134       exceed the limit and will see errors from the node. These errors will clear when the current 15 minute window
135       passes.</p>
136 </script>
137
138 <script type="text/javascript">
139     RED.nodes.registerType('twitter in',{
140         category: 'social-input',
141         color:"#C0DEED",
142         defaults: {
143             twitter: {type:"twitter-credentials",required:true},
144             tags: {value:"",validate:function(v) { return this.user == "dm" || v.length > 0;}},
145             user: {value:"false",required:true},
146             name: {value:""},
147             topic: {value:"tweets"}
148         },
149         inputs:0,
150         outputs:1,
151         icon: "twitter.png",
152         label: function() {
153             if (this.name) {
154                 return this.name;
155             }
156             if (this.user == "dm") {
157                 var user = RED.nodes.node(this.twitter);
158                 return (user?user.label()+" ":"")+"DMs";
159             } else if (this.user == "user") {
160                 return this.tags+" tweets";
161             }
162             return this.tags;
163         },
164         labelStyle: function() {
165             return this.name?"node_label_italic":"";
166         },
167         oneditprepare: function() {
168             $("#node-input-user").change(function() {
169                 var type = $("#node-input-user option:selected").val();
170                 if (type == "user") {
171                     $("#node-input-tags-row").show();
172                     $("#node-input-tags-label").html("User");
173                     $("#node-input-tags").attr("placeholder","comma-separated @twitter handles");
174                 } else if (type == "dm") {
175                     $("#node-input-tags-row").hide();
176                 } else {
177                     $("#node-input-tags-row").show();
178                     $("#node-input-tags-label").html("for");
179                     $("#node-input-tags").attr("placeholder","comma-separated words, @ids, #hashtags");
180                 }                
181
182             });
183             $("#node-input-user").change();
184         
185         }
186     });
187 </script>
188
189
190 <script type="text/x-red" data-template-name="twitter out">
191     <div class="form-row">
192         <label for="node-input-twitter"><i class="fa fa-user"></i> Twitter</label>
193         <input type="text" id="node-input-twitter">
194     </div>
195     <div class="form-row">
196         <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
197         <input type="text" id="node-input-name" placeholder="Name">
198     </div>
199 </script>
200
201 <script type="text/x-red" data-help-name="twitter out">
202     <p>Twitter out node. Tweets the <b>msg.payload</b>.</p>
203     <p>If <b>msg.media</b> exists and is a Buffer object, this node will treat it
204        as an image and attach it to the tweet.</p>
205 </script>
206
207 <script type="text/javascript">
208     RED.nodes.registerType('twitter out',{
209         category: 'social-output',
210         color:"#C0DEED",
211         defaults: {
212             twitter: {type:"twitter-credentials",required:true},
213             name: {value:"Tweet"}
214         },
215         inputs:1,
216         outputs:0,
217         icon: "twitter.png",
218         align: "right",
219         label: function() {
220             return this.name;
221         }
222     });
223 </script>