Week3_ Twitter Bot

I tried to use GIPHY API  file for this week Bot but it’s not working well.

Below is bot.js

 

var twitterAPI = require(‘node-twitter-api’);
var RiTa = require(‘rita’).RiTa;
var giphy = require(‘giphy-api’)(‘dc6zaTOxFJmzC’);

var consumerKey = “PdoX0jXn0HPipFX28alhE4MlF”;
var consumerSecret = “FBh7UCZzsFyop4soTzV6T1KRNoeNH5jDKuw6R6Sw4IJR437YlJ”;
var accessToken = “2311345171-x1yz8K4NiVGG6qVVpZOkK0B9u247x9MEHfDXZth”;
var tokenSecret = “sc6D31EDVJ8Z6I3SBFcVrXYqyiymsQdwcjHTRlpALlNmJ”;
var myScreenName = “diana”;
var twitter = new twitterAPI({
consumerKey: consumerKey,
consumerSecret: consumerSecret});

twitter.getStream(“user”, {}, accessToken, tokenSecret, onData);

function onData(error, streamEvent) {

// check object
if(Object.keys(streamEvent).length === 0) {
// do nothing
console.log(“obj is empty”);
return;
}

if (streamEvent.hasOwnProperty(‘direct_message’)) {
var dmText = streamEvent[‘direct_message’][‘text’];
var senderName = streamEvent[‘direct_message’][‘sender’][‘screen_name’];
// streaming API sends us our own direct messages! skip if we’re
// the sender.
if (senderName == myScreenName) {
return;
}
//var outgoingText;
var noun = getRandomNoun(dmText);
if (noun) {

getGiphy(noun,senderName);
//dmText = “”;
// console.log(“outgoingText: ” + outgoingText);
// if(outgoingText != “”) {
// twitter.direct_messages(
// ‘new’,
// {
// “screen_name”: senderName,
// “text”: outgoingText
// },
// config.accessToken,
// config.tokenSecret,
// function (err, data, resp) { console.log(err); }
// );
//}
// dmText = “”; // clear dmText;
// outgoingText = “”; // clear outgoingText
//outgoingText = “Keep calm and love ” + noun + “. “;
//var anotherNoun = getRandomNoun(dmText);
//outgoingText += “tell me more about ” + anotherNoun + “?”;
}
else {
outgoingText = “haha okay tell me more?”;
}
}
}

// parameter keyword = noun
// return giphy url
function getGiphy(keyWord, senderName) {
var rtnGif = “”;
giphy.search({
q: keyWord,
limit:100,
rating: ‘g’
//fmt: ‘json’
}, function(err, res) {
//console.log(“data”,res);
// parse data
// get bitly_url
var num = Math.floor(Math.random()*100); // generate 0-99
rtnGif = (res.data[0].bitly_gif_url).toString();
console.log(“return: “+rtnGif);
if(rtnGif!=””) {
twitter.direct_messages(
‘new’,
{
“screen_name”: senderName,
“text”: rtnGif
},
accessToken,
tokenSecret,
function (err, data, resp) { console.log(err); }
);
}

rtnGif =””;

});
}

function getRandomNoun(text) {
var tagged = RiTa.getPosTagsInline(text);
var taggedWords = tagged.split(” “);
var nouns = [];
for (var i = 0; i < taggedWords.length; i++) {
var parts = taggedWords[i].split(“/”);
if (parts[1] == ‘nn’ || parts[1] == ‘nns’) {
nouns.push(parts[0]);
}
}
if (nouns.length > 0) {
return nouns[Math.floor(Math.random()*nouns.length)];
}
else {
return “”;
}
}

 

My concept is when I message any noun or any sentence in Twitter Message, Twitter bot will response random GIF file from GIPHY. But it doesn’t response or giving tones of GIF files.  So, This week my Twitter bot is not working.  I was asking for my class mates, but they couldn’t find solution. Hopefully, after class I’d like to have perfect GIPHY Bot.

 

 

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *