Get latest tweet from user_timeline with twit npm - javascript

I'm trying to create a discord bot with notification about new tweets.
I am using T.stream('statuses/filter', { follow : ['798934987978510337'] }); for this, but it show mentions too.
Can I get only tweets from user with twit?

No, the statuses/filter streaming API does contain retweets as well. You will need to filter out retweets in your own code if you do not want to pass them over to Discord.

Following code works for me. Reference https://twittercommunity.com/t/only-receive-latest-tweet-from-specific-user-twit-npm/151478
var twitParams =
{
exclude_replies: false,
count: 1,
screen_name: "your screen name excluding #"
}
T.get("statuses/user_timeline", twitParams, function(err, data, response) {
console.log(data);
});

Related

Integrating GiroPay and SOFORT via Stripe

I tried to integrate the other offered payments offered by stripe. I followed the docs but it seems I overread something (read everthing like 10 times).
First I activated the payment methods.
I added the library: <script src="https://js.stripe.com/v3/"></script>
I created a Stripe client: var stripe = Stripe('pk_live_512dfvL8d63Kcs9d5Lsp548c6Sp'); (tried this with both test and publishable key)
I added an instance of Elements: var elements = stripe.elements(); (Not using it though)
Depending on the choosen payment something else gets triggered. Well here is my code:
function buyingProcess() {
console.log(choosenPaymentMethod);
if (choosenPaymentMethod == "mastervisa") {
// This works like a charm
} else if (choosenPaymentMethod == "giropay") {
stripe.createSource({
type: 'giropay',
amount: 1099,
currency: 'eur',
owner: {
name: 'Jenny Rosen',
},
redirect: {
return_url: 'https://shop.example.com/crtA6B28E1',
},
}).then(function(result) {
console.log(result);
// handle result.error or result.source
});
}
}
The last console.log function shows me the source object. While using the test key console.log(result.error) stated out, that I have to use the publishable key.
Thanks for any help. I wrote the Stripe Team. yesterday at this time but I apreciate any help. Thanks.
If you are seeing a source object in your console, then you have successfully created a source! The next step is to set up a webhook endpoint, and listen for a source.chargeable event.
Once you have received that your customer has authorized the source to be charged, and you can create a charge on your server using your secret key:
https://stripe.com/docs/sources/best-practices#charge-creation
You can delete var elements = stripe.elements(); as you don't need Elements to create sources, just the Stripe.js library.

How to retrieve both caller-name and carrier from Twilio in node.js?

Im having problems working with the twilio-api for node.
i wrote this code:
let typeArray = ['caller-name','carrier'];
this.client.phoneNumbers(phoneNumberToCheck).get({
type: typeArray
}, (error, number) => {
// working on the number data results
// ...
});
The problem is that i dont get ANY of them(carrier/caller-name) - although passing array to argument 'type' is the way to do it in other languages(php,c#..) but it doesnt work on node.js, instead i get this:
// -> get
{
"caller_name":null,
"country_code":"US",
"phone_number":"+123456789",
"national_format":"(248) 123-456",
"carrier":null,
"add_ons":null,
"url":"https://lookups.twilio.com/v1/PhoneNumbers/+123456789",
"callerName":null,
"countryCode":"US",
"phoneNumber":"+123456789",
"nationalFormat":"(248) 123-456",
"addOns":null
}
note: if i send each one separately (only carrier or only caller-name) - i get the partial information for each.
how can i get both in one request in node.js?
Twilio developer evangelist here.
You should be calling the Lookups API in Node this way:
client.lookups.phoneNumbers.get(phoneNumber)
.fetch({
type: ['carrier', 'caller-name']
},
function(err, result) {
// do something
}
)
The docs are a little lacking in Node.js on the Lookups documentation and I will raise that with the team.

Slack API (JSON Data)

I'm trying to use this method provided via the Slack API to grab the "name" attribute of the "user" object and the code I'm using to do that is as follows:
controller.hears(['users'], 'direct_message, direct_mention, mention', function(bot, message){
bot.api.users.info({}), function(err, response) {
bot.reply(message, response);
var slack_username = message["user"][2];
console.log(">>>>> " + slack_username);
});
});
I'm not sure what I'm doing wrong/missing and any help would be greatly appreciated! Thanks in advance!
EDIT:
Expected Output: "dkulas"
Terminal Output" "U"
debug: Got response null {"OK":false,"error":"user_not_found"}
degub: SAY { ok: false, error: 'user_not_found', channel" 'D0UV5S7MZ' }
The Slack API method users.info requires you to provide the Slack User ID in the query. The format is U12345678. (also see here for the usage documentation)
If you want to get the user info for a user by name, use the API method users.list to get the list of all users including name and user ID and then search that list for the right match. users.list provides all information about a user, so you don't need to call user.info.
See here for the documentation on this method.

Elasticsearch pagination with MongoDB and ExpressJS

I tried to search online for a solution but to be honest I still didn't found anything that help me to achieve this.
It is the first time I use ElasticSearch and I'm also pretty new with Node and MongoDB.
So, I followed a kind of tutorial and implemented Mongooastic from NPM to let ElasticSearch work with Node.
It seems to work fine even if on a total of 12 users indexed, if I type in the search "user" in the search list view I can find 12 records, it show 10 in a for each and the first one has missing values...
But the main problem for me is the pagination... or a sort of... it will be also nice to implement infinite scroll on it but I don't really know how to handle it.
So, the controller that handle it is the following at the moment:
exports.searchUsers = function(req, res) {
User.search({
query_string: {
query: req.query.q
}
},
{ hydrate: true },
function(err, results) {
if (err) res.send(err);
res.render('search-results', {
results: results,
users: results.hits.hits
});
});
};
I don't really know where to put size and from in here... and after understanding this I also would like to know how to implement, if possible, a sort of infinite scroll... And also how to handle link for the pagination... i.e.: prev, 1, 2, 3, 4, ..., next
The view has a simple input text for the search, after pressing submit it open a new page with the list of hits, so it should be nothing complex...
I hope you may help. Thanks
By default elasticsearch returns the 10 first results: you will have to set the size parameter if you want more.
Also, in order to add the size and from parameters, you need to write your query like so:
User.search({
query: {
query_string: {
query: req.query.q
}
},
size: 30,
from: 30
},
{hydrate: true},
function (err, results) {
if (err) res.send(err);
res.render('search-results', {
results: results,
users: results.hits.hits
});
});
(see here: https://github.com/taterbase/mongoosastic/issues/123 )
This will give you user n°30 up to user n°60 (more info here: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-request-from-size.html). You will have to play with your frontend to get the values you want for your size and from parameters.

Mongo check if a document already exists

In the MEAN app I'm currently building, the client-side makes a $http POST request to my API with a JSON array of soundcloud track data specific to that user. What I now want to achieve is for those tracks to be saved to my app database under a 'tracks' table. That way I'm then able to load tracks for that user from the database and also have the ability to create unique client URLs (/tracks/:track)
Some example data:
{
artist: "Nicole Moudaber"
artwork: "https://i1.sndcdn.com/artworks-000087731284-gevxfm-large.jpg?e76cf77"
source: "soundcloud"
stream: "https://api.soundcloud.com/tracks/162626499/stream.mp3?client_id=7d7e31b7e9ae5dc73586fcd143574550"
title: "In The MOOD - Episode 14"
}
This data is then passed to the API like so:
app.post('/tracks/add/new', function (req, res) {
var newTrack;
for (var i = 0; i < req.body.length; i++) {
newTrack = new tracksTable({
for_user: req.user._id,
title: req.body[i].title,
artist: req.body[i].artist,
artwork: req.body[i].artwork,
source: req.body[i].source,
stream: req.body[i].stream
});
tracksTable.find({'for_user': req.user._id, stream: req.body[i].stream}, function (err, trackTableData) {
if (err)
console.log('MongoDB Error: ' + err);
// stuck here - read below
});
}
});
The point at which I'm stuck, as marked above is this: I need to check if that track already exists in the database for that user, if it doesn't then save it. Then, once the loop has finished and all tracks have either been saved or ignored, a 200 response needs to be sent back to my client.
I've tried several methods so far and nothing seems to work, I've really hit a wall and so help/advice on this would be greatly appreciated.
Create a compound index and make it unique.
Using the index mentioned above will ensure that there are no documents which have the same for_user and stream.
trackSchema.ensureIndex( {for_user:1, stream:1}, {unique, true} )
Now use the mongoDB batch operation to insert multiple documents.
//docs is the array of tracks you are going to insert.
trackTable.collection.insert(docs, options, function(err,savedDocs){
//savedDocs is the array of docs saved.
//By checking savedDocs you can see how many tracks were actually inserted
})
Make sure to validate your objects as by using .collection we are bypassing mongoose.
Make a unique _id based on user and track. In mongo you can pass in the _id that you want to use.
Example {_id : "NicoleMoudaber InTheMOODEpisode14",
artist: "Nicole Moudaber"
artwork: "https://i1.sndcdn.com/artworks-000087731284-gevxfm-large.jpg?e76cf77"
source: "soundcloud"
stream: "https://api.soundcloud.com/tracks/162626499/stream.mp3? client_id=7d7e31b7e9ae5dc73586fcd143574550"
title: "In The MOOD - Episode 14"}
_id must be unique and won't let you insert another document with the same _id. You could also use this to find the record later db.collection.find({_id : NicoleMoudaber InTheMOODEpisode14})
or you could find all tracks for db.collection.find({_id : /^NicoleMoudaber/}) and it will still use the index.
There is another method to this that I can explain if you dont' like this one.
Both options will work in a sharded environment as well as a single replica set. "Unique" indexes do not work in a sharded environment.
Soundcloud API provides a track id, just use it.
then before inserting datas you make a
tracks.find({id_soundcloud : 25645456}).exec(function(err,track){
if(track.length){ console.log("do nothing")}else {//insert}
});

Categories

Resources