Discord bot not starting up without error message - node.js

When i try to start up my bot via node the program quits again without any errors
var Discord = require('discord.io');
var bot = new Discord.Client({
token: "nope"
});
bot.connect();
console.log("hi");
bot.on('ready', function (evt) {
console.log('Connected');
});
bot.on('message', function (user, userID, channelID, message, event) {
bot.sendMessage({
to:channelID,
message:"user, userID, channelID, message, evt:"+user + userID + channelID+ message+ event
});
});

My issue was the token which was somehow breaking stuff.
My workaround, was by just coping my token from the ./auth.json and place in it manually inside the client variable. (which was retriving from the file)
before:
var client = new Discord.Client({
token: auth.token,
autorun: true
});
after:
var client = new Discord.Client({
token: "REPLACE_THIS_WITH_YOUR_TOKEN",
autorun: true
});

Related

Authentication and publih-subscription with nodejs mosca

I am creating a iot service with mosca on node js. It crashes constantly to the subscription area and I cannot show the incoming message.
How do I prevent it from crashing.
How can I see the incoming message?
How can I see the incoming message in the authorizeSubscribe field?
Also authorizeSubscribe field crashes
const mosca = require('mosca');
const settings = {
port: 1883,
};
const server = new mosca.Server(settings);
server.on('ready', setup);
function setup() {
server.authenticate = authenticate;
server.authorizePublish = authorizePublish;
server.authorizeSubscribe = authorizeSubscribe;
console.log('Mosca server is up and running');
}
const authenticate = function(client, username, password, callback) {
console.log("authenticatealanı", username + " " + password);
const authorized = (username === 'alice' && password.toString() === 'secret');
if (authorized) client.user = username;
callback(null, authorized);
};
// In this case the client authorized as alice can publish to /users/alice taking
// the username from the topic and verifing it is the same of the authorized user
const authorizePublish = function(client, topic, payload, callback) {
console.log("authorizePublish " + topic + " "+ payload);
//callback(null, client.user === topic.split('/')[1]);
};
// In this case the client authorized as alice can subscribe to /users/alice taking
// the username from the topic and verifing it is the same of the authorized user
const authorizeSubscribe = function(client, topic, message, callback) {
console.log("new Data Auth subscribe"+ topic );
console.log(message);
//callback(null, client.user === topic.split('/')[1]);
};
If your authorize functions never call the passed in callback they will never authorize anything...

If MFA enabled in AWS cognito, do I need to create js on client side to call cognitoUser.authenticateUser() because of the promt for code?

I am using reactjs and node for server side.
As you can see in the "mfa required" part of the code below, if this is all on node, then I can't really do "prompt" the user for the code, I have to pass this back to the front end.
Tried solution: If I do pass the MFA required to front end and get the users input then send it back to node to call "respondToAuth" I am getting two MFA codes in my SMS message.
Have I tried other solutions?
I am hesitant to use amplify because everything is on the front end, I would ideally like to do my authentication on the back end (thus node).
Another option I am leaning towards is just using initiateAuth api instead of "cognitoUser.AuthenticateUser". This way I can get the challenge response and pass it on in sequence. But as per my initial question, I am wondering if I can implement the below code and be able to route users to input MFA code (without duplicating MFA sms message)
AWS.config.update({
region: process.env.Region
});
var AmazonCognitoIdentity = require('amazon-cognito-identity-js');
const poolData = { //--Moved to env variables
UserPoolId: process.env.UserPoolId, // your user pool id here
ClientId: process.env.ClientId // your app client id here
};
const userPool = new AmazonCognitoIdentity.CognitoUserPool(poolData);
router.post('/api/authenticateuser', (req, res) => {
const val = req.body;
var userData = {
Username: val.value.user, // your username here
Pool: userPool
};
var authenticationData = {
Username: val.value.user, // your username here
Password: val.value.pass, // your password here
};
const authenticationDetails = new AmazonCognitoIdentity.AuthenticationDetails(authenticationData);
const cognitoUser = new AmazonCognitoIdentity.CognitoUser(userData);
cognitoUser.authenticateUser(authenticationDetails, {
onSuccess: function(result) {
console.log('You are now logged in.');
console.log(result);
const accessToken = result.getAccessToken().getJwtToken();
const idToken = result.getIdToken().getJwtToken();
res.json({
accessToken,
idToken
});
},
onFailure: function(err) {
res.json(err);
},
mfaRequired: function(codeDeliveryDetails) {
// console.log("mfa enabled");
// var verificationCode = prompt('Please input verification code' ,'');
// cognitoUser.sendMFACode(verificationCode, this);
// res.json({ MFA:codeDeliveryDetails})
}
});
})

Microsoft Bot displays unnecessary duplicated messages in WebChat?

When a user visits my chat for the first time they are greeted with welcome message and immediately asked to provide their first name. As soon as the user inputs their first name the welcome message is and text prompt for their first name is displayed once again. Only after they input their first name for the second time, the bot moves on to the next question about their last name.
Additionally when user finally enters their first and last name in first chat and they come back again to the same chat, welcome message & first name prompt is displayed, only when user provides some input bot sends welcome back message.
This is minimal code required to reproduce this problem.
let restify = require('restify');
let builder = require('botbuilder');
// Setup Restify Server
let server = restify.createServer();
let connector = new builder.ChatConnector({
appId: process.env.MICROSOFT_APP_ID,
appPassword: process.env.MICROSOFT_APP_PASSWORD
});
server.post('/api/messages', connector.listen());
let bot = new builder.UniversalBot(connector);
// Define default Dialog
bot.dialog('/', [
function (session) {
if (!session.userData.firstName) {
// firstName used as a flag to assess whether the user is coming back
// or new user - we can use it because the very first dialog is asking them
// for their first name.
session.send("Welcome!");
} else {
session.send("Welcome back %s!", session.userData.firstName);
}
session.beginDialog('mainConversationFlow');
}
])
bot.dialog('mainConversationFlow', [
function(session, args, next) {
if (!session.userData.firstName)
session.beginDialog('getFirstName');
else
next();
},
function(session, args, next) {
if(!session.userData.lastName)
session.beginDialog('getLastName');
else
next();
},
function(session, args, next) {
session.endConversation('The End');
}
])
bot.dialog('getFirstName', [
function(session, args) {
let msg = "What's your first name?";
builder.Prompts.text(session, msg);
},
function(session, results) {
session.userData.firstName = results.response.trim();
session.endDialog();
}
])
bot.dialog('getLastName', [
function(session, args) {
let msg = builder.Message.composePrompt(session,
["Hi %s, what's your last name?"], session.userData.firstName);
builder.Prompts.text(session, msg);
},
function(session, results) {
session.userData.lastName = results.response.trim();
session.endDialog();
}
])
bot.on('conversationUpdate', function (message) {
if (message.membersAdded) {
message.membersAdded.forEach(function (identity) {
if (identity.id === message.address.bot.id) {
bot.beginDialog(message.address, '/');
}
});
}
})
server.listen(process.env.port || process.env.PORT || 3978, function () {
console.log('%s listening to %s', server.name, server.url);
})
User on the first run of the app should be shown welcome message and asked for their first name. As soon as they input their first name, the bot should move right away to the next question about their last name and after the user answers that bot should end conversation.
When user entered their first & last names and they come back, the bot should only display welcome back message and end conversation.
This is snippet of my client code using BotFramework-WebChat:
let directLineSecret = 'mysecret';
let directLineSpecUrl = 'https://docs.botframework.com/en-us/restapi/directline3/swagger.json';
BotChat.App({
directLine: {secret: directLineSecret},
user: { id: localStorage.getItem('email')},
bot: { id: 'testbot' },
resize: 'detect'
}, this.botChatContainer.nativeElement);
let directLineClient = rp(directLineSpecUrl)
.then(function(spec) {
return new Swagger({
spec: JSON.parse(spec.trim()),
usePromise: true
});
})
.then(function(client) {
return rp({
url: 'https://directline.botframework.com/v3/directline/tokens/generate',
method: 'POST',
headers: {
'Authorization': 'Bearer ' + directLineSecret
},
json: true
}).then(function(response) {
let token = response.token;
client.clientAuthorizations.add('AuthorizationBotConnector', new Swagger.ApiKeyAuthorization('Authorization', 'Bearer ' + token, 'header'));
return client;
});
})
.catch(function(err) {
console.error('Error initializing DirectLine client', err);
throw err;
});
These screenshots were taken in dev.botframework.com test window. However the same behaviour applies in my web app which uses WebChat.
Can you please help me to solve this issue?
Update
Logs:
2018-01-13 19:29:46.876 INFO - Container logs 2018-01-13T19:29:45.006255595Z
UserConversation message: , user: undefined 2018-01-13T19:29:45.006543896Z {"typ
e":"conversationUpdate","timestamp":"2018-01-13T19:29:44.4543348Z","membersAdded
":[{"id":"mybot#j4OUxKYkEpQ","name":"MyBot"}],"text":"","attachments":[],"entiti
es":[],"address":{"id":"C27bFaQ1Ohr","channelId":"webchat","user":{"id":"8f8399d
115774c86b83634bf7086f354"},"conversation":{"id":"8f8399d115774c86b83634bf7086f3
54"},"bot":{"id":"mybot#j4OUxKYkEpQ","name":"MyBot"},"serviceUrl":"https://webch
at.botframework.com/"},"source":"webchat","agent":"botbuilder","user":{"id":"8f8
399d115774c86b83634bf7086f354"}}
2018-01-13T19:29:45.006562196Z ----------------------------
2018-01-13T19:29:45.937402126Z Incoming message:
2018-01-13T19:29:45.937559026Z ----------------------------
2018-01-13T19:29:46.291227879Z Outgoing message: Welcome!
2018-01-13T19:29:46.291465679Z {"type":"message","agent":"botbuilder","source":"
webchat","address":{"id":"C27bFaQ1Ohr","channelId":"webchat","user":{"id":"8f839
9d115774c86b83634bf7086f354"},"conversation":{"id":"8f8399d115774c86b83634bf7086
f354"},"bot":{"id":"mybot#j4OUxKYkEpQ","name":"MyBot"},"serviceUrl":"https://web
chat.botframework.com/"},"text":"Welcome!"}
2018-01-13T19:29:46.291479179Z ----------------------------
2018-01-13T19:29:46.291708779Z Outgoing message:
What's your first name? 2018-01-13T19:29:46.291740980Z {"text":"What's your
first name?","inputHint":"expectingInput","type":"message","address":{"id":"C27b
FaQ1Ohr","channelId":"webchat","user":{"id":"8f8399d115774c86b83634bf7086f354"},
"conversation":{"id":"8f8399d115774c86b83634bf7086f354"},"bot":{"id":"mybot#j4OU
xKYkEpQ","name":"MyBot"},"serviceUrl":"https://webchat.botframework.com/"}}
2018-01-13T19:29:46.291759880Z ----------------------------
2018-01-13 19:29:56.876 INFO - Container logs 2018-01-13T19:29:53.471348251Z
UserConversation message: , user: undefined 2018-01-13T19:29:53.471657052Z {"typ
e":"conversationUpdate","timestamp":"2018-01-13T19:29:53.3233269Z","membersAdded
":[{"id":"AvfenKwcS1o","name":"You"}],"text":"","attachments":[],"entities":[],"
address":{"id":"DbpPwxf2m7T","channelId":"webchat","user":{"id":"8f8399d115774c8
6b83634bf7086f354"},"conversation":{"id":"8f8399d115774c86b83634bf7086f354"},"bo
t":{"id":"mybot#j4OUxKYkEpQ","name":"MyBot"},"serviceUrl":"https://webchat.botfr
amework.com/"},"source":"webchat","agent":"botbuilder","user":{"id":"8f8399d1157
74c86b83634bf7086f354"}}
2018-01-13T19:29:53.471672552Z ----------------------------
2018-01-13T19:29:53.515781796Z UserConversation
message: John, user: You 2018-01-13T19:29:53.515792596Z {"type":"message","times
tamp":"2018-01-13T19:29:53.1827153Z","textFormat":"plain","text":"John","entitie
s":[{"type":"ClientCapabilities","requiresBotState":true,"supportsTts":true,"sup
portsListening":true}],"textLocale":"en","sourceEvent":{"clientActivityId":"1515
871784086.6213104132628995.0"},"attachments":[],"address":{"id":"8f8399d115774c8
6b83634bf7086f354|0000002","channelId":"webchat","user":{"id":"AvfenKwcS1o","nam
e":"You"},"conversation":{"id":"8f8399d115774c86b83634bf7086f354"},"bot":{"id":"
mybot#j4OUxKYkEpQ","name":"MyBot"},"serviceUrl":"https://webchat.botframework.co
m/"},"source":"webchat","agent":"botbuilder","user":{"id":"AvfenKwcS1o","name":"
You"}}
2018-01-13T19:29:53.515801796Z ----------------------------
2018-01-13T19:29:53.545361425Z Incoming message: John
2018-01-13T19:29:53.545373525Z ----------------------------
2018-01-13T19:29:53.802571982Z Outgoing message: Welcome!
2018-01-13T19:29:53.802593382Z {"type":"message","agent":"botbuilder","source":"
webchat","textLocale":"en","address":{"id":"8f8399d115774c86b83634bf7086f354|000
0002","channelId":"webchat","user":{"id":"AvfenKwcS1o","name":"You"},"conversati
on":{"id":"8f8399d115774c86b83634bf7086f354"},"bot":{"id":"mybot#j4OUxKYkEpQ","n
ame":"MyBot"},"serviceUrl":"https://webchat.botframework.com/"},"text":"Welcome!
"}
2018-01-13T19:29:53.802600382Z ----------------------------
2018-01-13T19:29:53.802602782Z Outgoing message: What's your first name?
2018-01-13T19:29:53.802604982Z {"text":"What's your first name?","inputHint":"ex
pectingInput","type":"message","address":{"id":"8f8399d115774c86b83634bf7086f354
|0000002","channelId":"webchat","user":{"id":"AvfenKwcS1o","name":"You"},"conver
sation":{"id":"8f8399d115774c86b83634bf7086f354"},"bot":{"id":"mybot#j4OUxKYkEpQ
","name":"MyBot"},"serviceUrl":"https://webchat.botframework.com/"},"textLocale"
:"en"}
2018-01-13T19:29:53.802610082Z ----------------------------
2018-01-13 19:30:01.878 INFO - Container logs 2018-01-13T19:29:57.806548081Z
UserConversation message: John, user: You 2018-01-13T19:29:57.809735285Z {"type"
:"message","timestamp":"2018-01-13T19:29:57.6990081Z","textFormat":"plain","text
":"John","textLocale":"en","sourceEvent":{"clientActivityId":"1515871784086.6213
104132628995.2"},"attachments":[],"entities":[],"address":{"id":"8f8399d115774c8
6b83634bf7086f354|0000005","channelId":"webchat","user":{"id":"AvfenKwcS1o","nam
e":"You"},"conversation":{"id":"8f8399d115774c86b83634bf7086f354"},"bot":{"id":"
mybot#j4OUxKYkEpQ","name":"MyBot"},"serviceUrl":"https://webchat.botframework.co
m/"},"source":"webchat","agent":"botbuilder","user":{"id":"AvfenKwcS1o","name":"
You"}}
2018-01-13T19:29:57.809755085Z ----------------------------
2018-01-13T19:29:57.828015903Z Incoming message: John
2018-01-13T19:29:57.828028303Z ----------------------------
2018-01-13T19:29:58.122706697Z Outgoing message: Got response as: John
2018-01-13T19:29:58.122972998Z {"type":"message","agent":"botbuilder","source":"
webchat","textLocale":"en","address":{"id":"8f8399d115774c86b83634bf7086f354|000
0005","channelId":"webchat","user":{"id":"AvfenKwcS1o","name":"You"},"conversati
on":{"id":"8f8399d115774c86b83634bf7086f354"},"bot":{"id":"mybot#j4OUxKYkEpQ","n
ame":"MyBot"},"serviceUrl":"https://webchat.botframework.com/"},"text":"Got
response as: John"}
2018-01-13T19:29:58.122997998Z ----------------------------
2018-01-13T19:29:58.123366398Z Outgoing message: Hello John! What is your last
name? 2018-01-13T19:29:58.123377798Z {"text":"Hello John! What is your last name
?","inputHint":"expectingInput","type":"message","address":{"id":"8f8399d115774c
86b83634bf7086f354|0000005","channelId":"webchat","user":{"id":"AvfenKwcS1o","na
me":"You"},"conversation":{"id":"8f8399d115774c86b83634bf7086f354"},"bot":{"id":
"mybot#j4OUxKYkEpQ","name":"MyBot"},"serviceUrl":"https://webchat.botframework.c
om/"},"textLocale":"en"}
2018-01-13T19:29:58.123395698Z ----------------------------
2018-01-13T19:30:00.551811524Z UserConversation
message: Doe, user: You 2018-01-13T19:30:00.552098924Z {"type":"message","timest
amp":"2018-01-13T19:30:00.4252782Z","textFormat":"plain","text":"Doe","textLocal
e":"en","sourceEvent":{"clientActivityId":"1515871784086.6213104132628995.4"},"a
ttachments":[],"entities":[],"address":{"id":"8f8399d115774c86b83634bf7086f354|0
000008","channelId":"webchat","user":{"id":"AvfenKwcS1o","name":"You"},"conversa
tion":{"id":"8f8399d115774c86b83634bf7086f354"},"bot":{"id":"mybot#j4OUxKYkEpQ",
"name":"MyBot"},"serviceUrl":"https://webchat.botframework.com/"},"source":"webc
hat","agent":"botbuilder","user":{"id":"AvfenKwcS1o","name":"You"}}
2018-01-13T19:30:00.552114924Z ----------------------------
2018-01-13T19:30:00.590356662Z Incoming message: Doe
2018-01-13T19:30:00.590371762Z ----------------------------
2018-01-13T19:30:00.857187129Z Outgoing message: Got last name as: Doe
2018-01-13T19:30:00.857206229Z {"type":"message","agent":"botbuilder","source":"
webchat","textLocale":"en","address":{"id":"8f8399d115774c86b83634bf7086f354|000
0008","channelId":"webchat","user":{"id":"AvfenKwcS1o","name":"You"},"conversati
on":{"id":"8f8399d115774c86b83634bf7086f354"},"bot":{"id":"mybot#j4OUxKYkEpQ","n
ame":"MyBot"},"serviceUrl":"https://webchat.botframework.com/"},"text":"Got last
name as: Doe"}
2018-01-13T19:30:00.857220329Z ----------------------------
2018-01-13T19:30:00.857222929Z Outgoing message: End of "mainConversationFlow"
dialog. 2018-01-13T19:30:00.857225229Z {"type":"message","agent":"botbuilder","s
ource":"webchat","textLocale":"en","address":{"id":"8f8399d115774c86b83634bf7086
f354|0000008","channelId":"webchat","user":{"id":"AvfenKwcS1o","name":"You"},"co
nversation":{"id":"8f8399d115774c86b83634bf7086f354"},"bot":{"id":"mybot#j4OUxKY
kEpQ","name":"MyBot"},"serviceUrl":"https://webchat.botframework.com/"},"text":"
End of \"mainConversationFlow\" dialog."}
2018-01-13T19:30:00.857230729Z ----------------------------
Code I have used for logs:
const logUserConversation = (event) => {
console.log('UserConversation message: ' + event.text + ', user: ' + event.address.user.name);
console.log(JSON.stringify(event));
console.log('----------------------------');
};
const logIncomingMessage = function (session) {
console.log('Incoming message: ' + session.message.text);
console.log(JSON.stringify(session.user));
console.log('----------------------------');
};
const logOutgoingMessage = function (event) {
console.log('Outgoing message: ' + event.text);
console.log(JSON.stringify(event));
console.log('----------------------------');
};
bot.use({
receive: function (event, next) {
logUserConversation(event);
next();
},
botbuilder: function (session, next) {
logIncomingMessage(session);
next();
},
send: function (event, next) {
logOutgoingMessage(event);
next();
}
})
Actually, when the bot connecter first connect to bot server, the bot firstly joins in the conversation, so conversationUpdate event will be triggerred for your bot, which do not contain the session.userData object.
And once the user inputs someting in the bot webchat, when there will be a second conversationUpdate for the user. At this time, bot beginDialog '\' inside the conversationUpdate event with the session contains session.userData object.
you can add the following middleware to detect this issue:
bot.use({
receive: function (event, next) {
console.log(event)
next();
},
send: function (event, next) {
console.log(event)
next();
}
});
Unfortunately, I cannot find a method to let bot triggers conversationUpdate for user when the webchat init.
update
You can leverage webchat js sdk on website and the backchannel mechanism to achieve your requirement.
website client:
//define a user
const user = {id:'userid',name:'username'};
const botConnection = new BotChat.DirectLine({
domain: params['domain'],
secret: '<secrect>',
webSocket: params['webSocket'] && params['webSocket'] === 'true' // defaults to true
});
botConnection .postActivity({ type: "event", from: user, name: "ConversationUpdate", value: "" }) .subscribe(id => console.log("Conversation updated"));
BotChat.App({
botConnection: botConnection,
bot: bot,
user: user,
resize: 'detect'
}, document.getElementById("BotChatGoesHere"));
bot server:
bot.on('event',(event)=>{
console.log(event)
if(event.name==='ConversationUpdate'){
bot.beginDialog(event.address, '/');
}
})
To the people who still get the issue
When the first conversation is established between the web channel and the bot, the ConversationUpdate activity is raised twice. One by the user and another by the channel, therefore we get a welcome message twice. We need to make sure that we send the welcome message for the activity raised by the user. Find the working code here

Slackbots node.js get usernames

Using Slackbots(https://www.npmjs.com/package/slackbots) I was hoping to build a simple message forwarding bot (i.e. when the bot receives a direct message, it shoots it into a chat room and says who it is from) but I can't seem to get it to push out user names, only user ID's. Any idea here?
var SlackBot = require('slackbots');
var bot = new SlackBot({
token: 'xoxb-',
name: 'helper'
});
bot.on('start', function() {
var params = {
icon_emoji: ':cat:'
};
bot.postMessageToChannel('helpertest', 'meow!', params);
});
bot.on('message', function(data) {
bot.postMessageToChannel('helpertest', data.text + data.user.name);
})

issue while authentication and registration node-xmpp

I am trying to register a new user through node-xmpp from node.js to an ejabberd but the authentication and registration events are not invoking neither it is not connecting.
var clientXMPP = require('node-xmpp-client');
var serverXMPP = require('node-xmpp-server');
var c2s = new serverXMPP.C2SServer({
jid: 'testuser#192.168.1.1',
password: 'test'
});
c2s.on("connect", function(client) {
console.log(client)
c2s.on('register', function(opts, cb) {
console.log('REGISTER')
cb(false)
})
c2s.on("authenticate", function(opts, cb) {
console.log("AUTH" + opts.jid + " -> " +opts.password);
cb(false);
});
c2s.on('disconnect', function () {
console.log('DISCONNECT')
})
});
How can I register a new user and authenticate them in node-xmpp.
If you just need to create new user from nodejs to ejabberd server please make sure you have in-band registration enabled in ejabberd.
you don't need node-xmpp-server, you can only us node-xmpp-client. Client library will help you to connect with ejabberd admin user.
here is an example
const Client = require('node-xmpp-client');
let admin = new Client({
jid: '{adminJID}',
password: '{adminPassword}'
});
// check for errors to connect with socket
admin.connection.socket.on('error', function (error) {
// handle error
});
admin.on('online', function (data) {
// Register stanza
let stanza = new Client.Stanza('iq', {type: 'set', id: 'reg1', to: '192.168.1.1'})
.c('query', {xmlns: 'jabber:iq:register'})
.c('username').t('{username}').up() // Give a new username
.c('password').t('{password}') // Give a new user's password
admin.send(stanza); // send a stanza
});
// response stanzas
admin.on('stanza', function (stanza) {
// check for error/success
});

Resources