Cant loop through users to send them individual message FB.ui() - javascript

I am looking for a way to send messages to multiple people without creating group chats and I have been working on this for two days now. I am trying to use a little hack where it loops through and ID list and basically takes values in an array and loops through them so one by one it sends a message. Basically, one ID sends message, then it loops back second ID sends message and so on until all parties have received a message. Here is my code so far
tl1.addEventListener("click", function() { //Send Message
try
{
var users = ['100008601850848', '100002242788752'];
for (var i = 0; i < users.length; i++)
{
FB.ui({
app_id: '1226220854077249',
method: 'SEND',
link: 'https://developers.facebook.com/apps/1226220854077249/roles/test-users/',
to: users[i]
});
}
}
catch(error)
{
console.log(error);
}
}, false);
Am I approaching this right? It does not send a message to all of them it only puts the first user in the field. All I am trying to do is make a loop that sends messages to one user then the next and so on. Basically sending it to multiple ID's but without creating groups as the FB.UI() does

There is only one way to use the send dialog, and if it creates a group chat then that´s what you have to live with. Right now you are trying to open several popups at once, which is a pretty ugly workaround - if it even works.

Related

Message Specific PFP. (like TupperBox)

I am looking to send a message that has a different profile picture than the bot. Basically, like Tupper Bot does. I can't seem to figure out how to do it anywhere, I've read the documentation and searched around.
You can use a webhook to do what you describe. If you already have a webhook link see here on how to use it. This works from external programs too.
If you don't, you can create a webhook with your bot and then send messages to it. In this example we will create a simple webhook and send a message to it.
First we should check if the channel already has a webhook. That way you don't create a new one everytime you use the command. You do that with the fetchWebhooks() function which returns a collection if there is a webhook present. If that is the case we will get the first webhook in the collection and use that. Note: if you have more then one webhook you can of course add additional checks after the initial one.
If there are no webhooks present, we create a new one with createWebhook(). You can pass additional options after the name.
let webhook;
let webhookCollection = await message.channel.fetchWebhooks();
if (webhookCollection.first()) {
webhook = webhookCollection.first();
} else {
webhook = await message.channel.createWebhook("new webhook", {
avatar: "https://i.imgur.com/tX5nlQ3.jpeg"
});
}
Note: in this example we use the channel the message came from but you can use any channel you want
Technically we can now send messages already.
webhook.send("this is a test");
What you can also do is create a webhookClient. Thats a little like the discord client you create when you start your bot. It takes the webhook ID and token as well as additional options you can set for clients.
const hookclient = new Discord.WebhookClient(webhook.id, webhook.token);
This lets you use a few additional methods like setInterval.
hookclient.setInterval(() => {
hookclient.send("This is a timer test");
}, 1000)
This lets you use the webhook without creating or fetching one first.

Javascript Unique Array for each Socket User

i have a Problem!
When two users playing a game and they click very near at the same time, all two users get what only the 2nd user must get.
Is it possible to make an array by his userid or something?
I also have for anti spam like that
anti_spam[socket.request.session.passport.user.id]
But thats a json
If i try the same or array, i get error SyntaxError: Unexpected token [
How can i make sure, that each user get only his own items and not items from another user when opening at the same time?
I use sockets, here is a unique userid socket.request.session.passport.user.id
This is the array
var WinItem = [];
And if two users start like milliseconds same time, than the second overwrite the first...
! CODE NOT TESTED!
Wait wait wait wait...
So I only know ws (Node.js) but there you first get a onConnection event.
In that function you add a UID to the connection element. Then if you receive a message from the client you should have you're connection object and therefore an UID. You now can store the won item in the connection object (if you want) maybe like so:
ws.onConnection = con => {
con.UID = generateUID();
con.inventory = [];
con.onMessage = msg => {
[..STUFF/CODE/YAY...]
con.inventory.push(item);
con.send("You've won wooohoo");
});
});
Did you mean something like this??
Otherwise please be more specific about what you want.
(You can also store the stuff somewhere else together with the UID but that would add quiet some code)

How to send a message with a random #mention on Slack using BotKit?

I'm new to developing bots and trying to use BotKit within Slack to mention a random user in the #test channel every week. Here is the code I have so far:
controller.hears('interactive', 'direct_message', function(bot, message) {
// send webhooks
bot.configureIncomingWebhook({url: 'https://hooks.slack.com/services/my/hook/uri'});
bot.sendWebhook({
text: 'Hey!',
channel: '#test',
},function(err,res) {
// handle error
});
});
Right now this sends a message "Hey!" to the #test channel when I direct message the bot "interactive", which is fine. How can I get all the usernames of users, and randomize them so that the text outputs like so: text: '"Hey " + "#" + random.username + " !"',. I would want this message to send every week as well.
Any ideas?
Slack provides a users.list function as described here: Slack API users.list
The members array contains user objects. You can user user[0].name to access the first username you want to #mention. in the demo they provide this would be #bobby.
Potential problems could arise because it returns the list in "no particular order"... you could potentially store it in a separate database and look to add elements if they are new and randomize based on your database entries.

Meteor - sending a message to all clients

I am building an application using Meteor.js with flashMessage to display informative messages for the user. Currently the message is tied to click events of individual users, but I want to display that message for all users.
Is there a way of using Meteor and flashMessages to accomplish this? Or another package should be used?
Cheers.
There are probably multiple ways, but here is one, using a collection of messages:
Common:
Messages = new Meteor.Collection('messages');
Client:
if (Meteor.isClient()) {
Meteor.subscribe('messages');
var msgs = Messages.find();
msgs.observeChanges({
added: function(id, obj) {
FlashMessages.sendInfo(obj.text);
}
});
}
Server:
if (Meteor.isServer()) {
Meteor.publish('messages', function() {
return Messages.find();
});
}
And then just insert messages like {text: "my text"} into the Messages collection and they should be displayed on all clients.
PS: You may want to remove the inserted messages again after a while, or else any newly arriving client will be shown all past messages. Alternatively you could just subscribe to recent messages.

How to sent a website url as link to multiple Facebook Friends?

I am setting up a website. I want users to invite Facebook friends to look at the page the users can make on my website. So i'm trying to get it done. But i think the FB Api is not set up to do what i want to do. I think it is to prevent from spamming, but i really like to know if it can be done or not.
This is what i have so far. Users can login with FB and i present a decent list of FB friends.
Each friend has a checkbox. When a user submits the form, i collect all the checked friends id's:
var sendUIDs = '';
var mfsForm = document.getElementById('mfsForm');
for (var i = 0; i < mfsForm.friends.length; i++) {
if (mfsForm.friends[i].checked) {
sendUIDs += mfsForm.friends[i].value + ',';
}
}
While i was trying, i came up with this "sollution" for sending all the friends the same invite:
FB.ui({
method: 'apprequests',
to: sendUIDs,
message: "Hey come on look at my site",
data: "mywebsite.com/userlink"
});
Looking great I thought. Exactly what i wanted... i thought.
Unfortunately this is only used for games? First of all i could not find the invite. It was there, but it seems to disappear. Then i found out it was in the App/Games/Activity tab.
I managed to get the notification in the "notification" tab by adding a Canvas to my FB App. Almost there i thought. Well, not exactly... I want the friends to visit my page by the given data link and not some weird ?number.
So i gave up this path and tried the other way and came up with this:
for (var i = 0; i < mfsForm.friends.length; i++) {
if (mfsForm.friends[i].checked) {
var uid = mfsForm.friends[i].value;
FB.ui({
to : uid,
method: 'send',
name: 'Visit my page',
link: 'mywebsite.com/userlink'
});
}
}
This works more or less, but produces a popup for every friend with an empty message box. Not very friendly and, well... stupid :)
I also tried the former setup, but instead of "apprequests" i used this:
FB.ui({
method: 'send',
to: sendUIDs,
message: "Hey come on look at my site",
data: "mywebsite.com/userlink"
});
where i tried this for the sendUIDs: [1, 2, ...] and just 1, 2, ...
No luck, because FB only processed one name.
I would think there must be a way to get the job done, but for now i am very confused how to proceed. I would think FB could make things a bit more easy?
The Send and Share Dialogs are built for this (sharing a Website URL on a wall or send it to friends in a PM), just let him choose the friends in the popup. That way, you don´t even need to authorize the user, and you can´t get ALL friends anyway (only those who authorized the app too).
About the message parameter: You are not allowed to prefill the message, it has to be 100% user generated. See the Platform Policy: https://developers.facebook.com/policy
2.3 Ensure that all content in the user message parameter is entered by the user. Don’t pre-fill. This includes posts, messages, comments,
and captions.
The FB.ui Dialogs ignore the message parameter anyway.

Categories