user validation error with mailgun connector loopback: email exists - node.js

I took the example project here and added the loopback-connector-mailgun. I configured it like this:
In datasources.json added the following:
{
"mailgun": {
"connector": "loopback-connector-mailgun",
"apikey": "[my api key here]",
"domain": "[my domain here]"
}
}
then in model-config.json:
{
"Email": {
"dataSource": "mailgun",
"public": false
}
}
When I register a client by doing a post in localhost:3000/api/clients the verification email is sent successfully but the server does not redirect me to the home page and after some time (it hangs for around 30 seconds) I get the following error:
{"name":"ValidationError","status":422,"message":"The `client` instance is not valid. Details: `email` Email already exists (value: \"a#b.com\") ...
The full stack is reported in this gist.
My client.js function:
Client.afterRemote('create', function(context, client, next) {
console.log('> client.afterRemote triggered');
//console.log(typeof client);
//var auth = dsConfig.emailDs.transports[0].auth;
var options = {
type: 'email',
to: client.email,
from: "noreply#loopback.com",
subject: 'Thanks for registering.',
template: path.resolve(__dirname, '../../server/views/verify.handlebars'),
redirect: '/verified',
user: client
};
client.verify(options, function(err, response) {
console.log(err + 'Debug point 1');
if (err) return next(err);
console.log('> verification email sent:', response);
context.res.render('response', {
title: 'Signed up successfully',
content: 'Please check your email and click on the verification link ' +
'before logging in.',
redirectTo: '/',
redirectToLinkText: 'Log in'
});
});
});
The execution never reaches Debug Point 1. Thanks!

Related

Nodemailer "connect ETIMEDOUT" with custom domain

I'm making a forgot password backend route in Node.js and I'm attempting to use nodemailer to send the email from a custom domain I purchased from namecheap.com, along with the email domain. I'm not sure if it's a problem with the host, the port/security, or the auth. However, when I change the host it gives a a ECONREFUSED error instead so I believe that part is working. My firewall is (as far as I can tell) disabled and I restarted, however it is harder to tell because Norton Antivirus controls it.
This is my code, taken from a router.get route in my back-end.
The full error is "connect ETIMEDOUT" then an ip address with :587 at the end.
const transporter = createTransport({
host: 'axotl.com',
port: 587,
secure: false,
auth: {
user: config.get('emailUser'),
pass: config.get('emailPass')
}
});
let resetLink = '';
let authToken = '';
await jwt.sign({ email: req.params.email }, config.get('JWTSecret'), { expiresIn: 10800000 }, (err, token) => {
if (err) throw err;
authToken += token;
})
resetLink = await `${config.get('productionLink')}/recipients/resetpassword/${authToken}`
console.log(`resetlink : ${resetLink}`)
const mailOptions = {
from: '"Axotl Support" <support#axotl.com>',
to: req.params.email,
subject: "Forgot Password",
text: `Hello ${req.name},\n\nHere is the password reset link you requested (expires in 3 hours): ${resetLink}\nIf you did not request this, please notify us at http://axotl.com/support\n\nThanks!\n-Axotl Support`
}
try {
console.log('trycatch entered')
// const verified = await transporter.verify()
// console.log(`verified : ${verified}`)
const res = await transporter.sendMail(mailOptions)
console.log('email completed')
console.log(res)
res.json({ msg: "email sent" })
} catch (err) {
console.error(err.message);
res.status(500).send("Server Error")
}
Turns out I was using the wrong host domain-- the service doesn't directly host the email domain on the website. I don't know enough about this specific section. I changed it to the email host and it worked.

Node JS - nodemailer & imap-simple - Identify threads

(I feel sorry for my poor english, but i'll do my best !) :)
I'm trying to setup a local mailbox for a personnal project, and i'm trying to use imap-simple and nodemailer to do that.
I want to be able to identify a thread, when i send an e-mail.
Here's what i exactly want to do :
In my application, i'll be able to send an e-mail to a specific person (let's admit foo#bar.com)
When the mail is sent, a callback function will store mail content and subject in DB (for example, in a CRM app, i will store the sent mail which will be related to a specific record in my database).
The complex part is just after that :
When the person replies to this e-mail, i want using IMAP to identify that this person is answering to my previous mail, and then store it in DB too, also linked to the same record i used on my first e-mail.
I actually have this in a sandbox folder (For IMAP) :
var imaps = require('imap-simple');
var nodemailer = require('nodemailer');
var config = {
imap: {
user: 'catchall#xxxxxxxx.fr',
password: 'xxxxxxxx',
host: 'imap.gmail.com',
port: 993,
tls: true,
authTimeout: 3000
}
};
imaps.connect(config).then(function (connection) {
return connection.openBox('INBOX').then(function () {
var searchCriteria = [
'UNSEEN'
];
var fetchOptions = {
bodies: ['HEADER', 'TEXT'],
markSeen: false,
};
console.log('Passing there');
return connection.search(searchCriteria, fetchOptions).then(function (results) {
var subjects = results.map(function (res) {
return res.parts.filter(function (part) {
return part.which === 'HEADER';
})[0].body.subject[0];
});
console.log('BASE');
console.log(results[0]);
console.log('FIRST');
console.log(results[0].parts[0]);
console.log('SECOND');
console.log(results[0].parts[1]);
});
});
});
And here is the SMTP part :
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
user: 'catchall#xxxxxxxx.fr',
pass: 'xxxxxxxx'
}
});
const mailOptions = {
from: 'catchall#xxxxxxxx.fr', // sender address
to: 'xxxxxxxx#gmail.com', // list of receivers
subject: 'Subject of your email', // Subject line
html: '<p>Here is my test !</p>',// plain text body
headers: {
'collection': 'Pipelines',
'doc-ID': 'mydocid'
}
};
transporter.verify(function(error, success) {
if (error) {
console.log(error);
} else {
console.log("Server is ready to take our messages");
}
});
transporter.sendMail(mailOptions, function (err, info) {
if(err)
console.log(err)
else
console.log(info);
});
Here is a screen of what i want to be able to :
On IMAP, i'm getting exactly the information i want in console :
BASE
{ attributes:
{ date: 2019-12-05T16:53:07.000Z,
flags: [],
uid: 94,
modseq: '27800',
'x-gm-labels': [ '\\Important' ],
'x-gm-msgid': '1652099423356172171',
'x-gm-thrid': '1652099362185438260' },
x-gm-thrid allow me to identify a thread. Perhaps, i can't find this information in the nodemailer callback function :
Server is ready to take our messages
{ accepted: [ 'xxxxxxxxxxxxxxxxx#gmail.com' ],
rejected: [],
envelopeTime: 400,
messageTime: 819,
messageSize: 346,
response: '250 2.0.0 OK 1575566223 m3sm12955793wrs.53 - gsmtp',
envelope:
{ from: 'catchall#xxxxxxxxxx.fr',
to: [ 'xxxxxxxxxxxx#gmail.com' ] },
messageId: '<f93b3970-e17a-84b5-d0d1-ebb4fd6efe46#xxxxxxxxxx.fr>' }
Does anyone have an idea of how i could proceed ?
Thanks a lot btw !
Happy coding :)
For those who will read this post, here the answer, thanks to #arnt,
As #arnt said, there is for IMAP protocol, a reference attribute which is storing the previous message ID stored in results[0].parts[1] :
references: [ '<23df0af1-8ff1-0ffa-091e-a645a38e4a67#foobar.fr>' ],
'in-reply-to': [ '<23df0af1-8ff1-0ffa-091e-a645a38e4a67#foobar.fr>' ],
This one is also available when you send a message, in info :
envelope:
{ from: 'foobar#foobar.fr',
to: [ 'foobar#gmail.com' ] },
messageId: '<23df0af1-8ff1-0ffa-091e-a645a38e4a67#foobar.fr>' }
This way, we're able to correlate mail conversation.
Thanks a lot for help :)
Topic closed

Azure BotBuilder - How to get the user information of the OAuth Connection Settings

I've created a Azure Web App Bot and added a OAuth Connection Setting which takes the user to Salesforce. Everything works well, I'm able to authenticate the user through my bot and also, I can get the access token from Salesforce.
Problem
Can someone help me to get the user information from Salesforce? Because, I am able to get the access token alone and not sure, how to get the user id from Salesforce.
I've written the below code,
var salesforce = {};
salesforce.signin = (connector, session, callback) => {
builder.OAuthCard.create(connector,
session,
connectionName,
"Sign in to your Salesforce account",
"Sign in",
(createSignInErr, createSignInRes) => {
if (createSignInErr) {
callback({
status: 'failure',
data: createSignInErr.message
});
return;
}
callback({
status: 'success',
data: createSignInRes
});
});
};
salesforce.getUserToken = (connector, session, callback) => {
connector.getUserToken(session.message.address,
connectionName,
undefined,
(userTokenErr, userTokenResponse) => {
if (userTokenErr) {
callback({
status: 'failure',
data: userTokenErr.message
});
return;
}
callback({
status: 'success',
data: userTokenResponse
});
});
};
salesforce.accessToken = (connector, session, callback) => {
salesforce.getUserToken(connector, session, (userTokenResponse) => {
if (userTokenResponse.status == 'failure') {
// If the user token is failed, then trigger the sign in card to the user.
salesforce.signin(connector, session, (signinResponse) => {
// If the sign in is failed, then let the user know about it.
if (signinResponse.status == 'failure') {
session.send('Something went wrong, ', signinResponse.message);
return;
}
// If the sign in is success then get the user token and send it to the user.
salesforce.getUserToken(connector, session, (newUserTokenResponse) => {
if (newUserTokenResponse.status == 'failure') {
session.send('Something went wrong, ', newUserTokenResponse.message);
return;
}
callback(newUserTokenResponse);
return;
});
});
}
callback(userTokenResponse);
});
};
I can get the userTokenResponse here. But I need Salesforce user id so that I can start interacting with Salesforce behalf of the user.
If you have only OAuth access token you may query details about the user by invoking http GET against:
https://login.salesforce.com/services/oauth2/userinfo for PROD or
https://test.salesforce.com/services/oauth2/userinfo for sandbox
Add only Authorization: Bearer Y0UR0AUTHTOKEN to the header of the http GET request.
Based on my recent test the result returned from the server looks like:
{
"sub": "https://test.salesforce.com/id/[organizationid]/[userId]",
"user_id": "000",
"organization_id": "000",
"preferred_username": "me#mycompany.com",
"nickname": "myNick",
"name": "name lastname",
"urls": {
...
},
"active": true,
"user_type": "STANDARD",
...
}
You don't need a userId to get the user information where an accessToken is enough. I've installed jsforce and used the below code to get the identity information.
Solved by doing,
const jsforce = require('jsforce');
var connection = new jsforce.Connection({
instanceUrl: instanceUrl,
sessionId: accessToken
});
connection.identity((error, response) => {
if(error) {
callback({
status: 'failure',
message: error.message
});
return;
}
callback({
staus: 'success',
data: response
});
});

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

Nodemailer not able to create transport / 502 error

I am having contiunual woes with getting nodemailer to send. I have now found the lowest security email account I can to see if I can get the thing working with a view to increasing security once something is getting through. My code is:
app.post("/contact-us", function(req, res){
var mailOpts, smtpTrans;
smtpTrans = nodemailer.createTransport({
host: 'smtp.123-reg.co.uk',
port: 25,
secure: false, // upgrade later with STARTTLS
auth: {
user: 'enquiries#*********.co.uk',
pass: '**********'
}
});
mailOpts = {
from: "enquiries#*******.co.uk",
to: "**********#mac.com",
subject: 'Website contact form',
text: "test email"
};
smtpTrans.sendMail(mailOpts, function (error, response) {
//Email not sent
if (error) {
console.log(error);
res.render('contact', { title: '********', msg: 'Error occured, message not sent.', err: true, page: 'contact-us' });
}
//Yay!! Email sent
else {
console.log("message sent");
res.render('/');
}
});
});
Neither of the console.logs are coming back, instead the request is met with a timeout & 502 message,
Thanks
Just in case anyone searches for this in the future; the problem was within cloud 9 which wouldn't allow access to the email account to prevent spam.

Resources