Bot emulator works fine with the clear text responses but when I try to do the same via Skype the bot response with;
I don't understand, please try again
I found out that Skype auto-formats the email by wrapping it in <a /> tags - XMM format and I am a bit stuck what's supposed to do. I think changing the incoming text format to plain-text would fix this.
I found this like for a similar issue on Github but this is on C# and I am using Node.JS.
How do I change the default text format of the bot/skype channel to plain text instead of markdown so Skype auto-formatting would not happen?
Updated according to #ezequiel-jadib but still no luck. Maybe I am doing it wrong?
// bot.js
bot.use(...[logger]);
// logger.js
module.exports = exports = {
receive: (e, next) => {
e.textFormat('plain');
logConversation(e);
next();
},
send: (e, next) => {
logConversation(e);
next();
}
};
To get around this situation, you need to validate if you have an HTML wrapped email address, then extract the email portion from the string before saving it to session.dialogData.
For example replace line 40:
const usernameOrEmail = session.dialogData.usernameOrEmail = results.response;
with:
// where results.response is in the format 'hello#world.com'
var exampleEmailWrappedInHtml = results.response;
// validate if we have email wrapped in HTML from Skype
if(exampleEmailWrappedInHtml.match(/<a href="mailto:/i)) {
console.log('HTML wrapped email detected!');
const usernameOrEmail = session.dialogData.usernameOrEmail = exampleEmailWrappedInHtml.split('>')[1].split('<')[0];
} else {
console.log('no match.');
}
You can just call to the textFormat method of the Message like in C#
The accepted values are:
export var TextFormat = {
plain: 'plain',
markdown: 'markdown',
xml: 'xml'
};
Related
I am trying to implement openpgpjs on my application because I need to encrypt a string using a public key (PGP). I tested this jsfiddle (https://jsfiddle.net/gu72bzm8/) which encrypts a string using a public key, it works very well. I even tested it with different keys and strings.
var message = "secret message";
const encryptMessage = async() => {
if(window.crypto.getRandomValues){
if(message != ""){
const publicKeyString = document.getElementById("pubkey").innerHTML;
var options = {
message: openpgp.message.fromText(message),
publicKeys: (await openpgp.key.readArmored(publicKeyString)).keys
};
openpgp.encrypt(options).then(ciphertext => {
alert(ciphertext.data);
})
}
} else{
window.alert("This browser does not support basic cryptography!");
}
}
encryptMessage();
However, if I copy exactly that code and try to run it locally (using the same cdn of that fiddle) I get the following error:
Uncaught (in promise) Error: Error encrypting message: No keys, passwords, or session key provided.
How can I fix it?
I am using twit npm package to read direct messages
var stream = T.stream('user', { stringify_friend_ids: true })
stream.on('direct_message', function (directMsg) {
console.log(directMsg)
}
I want to reply the directMsg received , is there any method call of twit which can be used to achieve or any other nodejs package recommended to achieve the same.
Can't provide the exact code solution, but the way is to implement the following steps
Get USER_ID property from directMsg object
Use Twit Rest API method T.post(path, [params], callback) to send direct message to the user with USER_ID
Use Twitter API documentation to understand what you need to send direct messages, you need to properly provide parameters like in documentation
So the code will look like this
T.post("direct_messages/new", {
user_id: USER_ID, // USER_ID is parameter from directMsg object
text: 'YOUR_REPLY'
});
Hope it will help you.
Even i ran into it, here is what you have to do.
Listen to the user stream for direct_message and retrieve the sender.screen_name and recipient.screen_name and verify it's not equal + you need the sender.id as stated by #RashadIbrahimov above.
// Reply to Twitter messages
function replyToDirectMessage(){
//get the user stream
var stream = T.stream('user');
stream.on('direct_message', function (eventMsg) {
var msg = eventMsg.direct_message.text;
var screenName = eventMsg.direct_message.sender.screen_name;
var userId = eventMsg.direct_message.sender.id;
// reply object
var replyTo = { user_id: userId,
text: "Thanks for your message :)",
screen_name: screenName };
console.log(screenName + " says: " + msg );
// avoid replying to yourself when the recipient is you
if(screenName != eventMsg.direct_message.recipient_screen_name){
//post reply
T.post("direct_messages/new",replyTo, function(err,data,response){
console.info(data);
});
}
});
}
and call it
replyToDirectMessage();
I am using nativescript to build an app that will programmatically send a pre-built text to multiple preset parties in case of emergency.
I have an array of phone numbers and want to iterate over each one, using SMSmanager to send the text and the sentIntent argument seen in android docs to verify that the text was sent before moving on to the next array item.
I have created the pendingIntent variable to pass into "sms.sendTextMessage" as follows:
var sms = android.telephony.SmsManager.getDefault();
var utils = require("utils/utils");
//Gets application's current state
var context = utils.ad.getApplicationContext();
//Create a replica of Android's intent object
var intent = new android.content.Intent(context, com.tns.NativeScriptActivity.class);
//Create a replica of Android's pendingIntent object using context and intent
var pendingIntent = android.app.PendingIntent.getActivity(context, 1, intent, android.app.PendingIntent.FLAG_UPDATE_CURRENT);
I then send the text, passing in the pending intent var:
sms.sendTextMessage("5555555555", null, "hello", pendingIntent, null);
I then attempt to make a basic broadcast receiver using the information I found in the nativescript docs which should just log something to the console when it recieves the expected data.
app.android.registerBroadcastReceiver(pendingIntent, function() {
console.log("##### text sent #####");
});
The problem is: nothing happens. I'd expect to get ""##### text sent #####" logged to the console. I've googled a lot and am thinking maybe I need to add something about this broadcast reciever in the manifest, or perhaps my implimentation is wrong somewhere, but this is my first crack at an android app and I'm at a bit of a loss. Any help would be appreciated.
I'm going to answer my own question here in case anyone else runs into this.
The code that worked is:
var app = require("application");
var utils = require("utils/utils");
var context = utils.ad.getApplicationContext();
var sms = android.telephony.SmsManager.getDefault();
var SendMessages = {
init: function() {
var id = "messageSent";
this.sendText(id, this.pendingIntent(id));
},
sendText: function(id, pendingIntent) {
sms.sendTextMessage("5555555555", null, "Hello :)", pendingIntent, null);
this.broadcastReceiver(id, function() {
console.log("$$$$$ text sent $$$$$");
});
},
pendingIntent: function(id) {
var intent = new android.content.Intent(id);
return android.app.PendingIntent.getBroadcast(context, 0, intent, 0);
},
broadcastReceiver: function(id, callback) {
app.android.registerBroadcastReceiver(id, function() {
callback();
});
}
};
module.exports = SendMessages;
To explain: it seems as #Mike M mentioned each intent object needs some string as an id.
Then to make the "pendingIntent" object, again as #Mike M. mentioned I needed to hook to "getBroadcast" method, then I needed to pass pending intent the app context as the first argument, then 0, then the intent object with the id.
The pending intent then is receivable in a simple broadcast receiver function by simply passing the intent id as the first argument and the callback as the second. I've tested and it's working perfectly.
Following is a simplified version, no bs code approach to what you need to make it run. You need permissions and to make sure the user accepts those permissions. This is also one of the two ways (this one is the context-registered receiver way) to create a broadcast receiver, read more about both types here: https://developer.android.com/guide/components/broadcasts#receiving-broadcasts
Info on registering broadcast receiver: https://docs.nativescript.org/api-reference/classes/application.androidapplication.html#registerbroadcastreceiver
AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="__PACKAGE__"
android:versionCode="10000"
android:versionName="1.0">
<!-- ...more code -->
<uses-permission android:name="android.permission.SEND_SMS" />
<!-- ...more code -->
</manifest>
JavaScript:
import * as application from 'tns-core-modules/application';
import * as platform from 'tns-core-modules/platform';
import * as utils from 'tns-core-modules/utils/utils';
import * as permissions from 'nativescript-permissions';
// ...more code
try {
await permissions.requestPermission(
android.Manifest.permission.SEND_SMS,
'Need to send.'
);
console.log('SEND_SMS permission accepted.');
const text = 'Herro.';
const mobileNumber = '55555555';
const intentFilter = 'something_here';
const context = utils.ad.getApplicationContext();
const intent = new android.content.Intent(intentFilter);
const pendingIntent = android.app.PendingIntent.getBroadcast(context, 0, intent, 0);
const sms = android.telephony.SmsManager.getDefault();
application.android.registerBroadcastReceiver(intentFilter, function() {
console.log(`Text has been sent: ${text}`);
});
setTimeout(() => {
console.log('Sending text.');
sms.sendTextMessage(mobileNumber, null, text, pendingIntent, null);
}, 5000);
} catch (error) {
console.log('Permission error:', error);
}
The code above is created inside the activity and uses the main ui thread. This means that if the user exits the activity, the broadcast receiver will linger in limbo and android can destroy it.
i found issue when sending email to multiple recipients using sendgrid.
i got blank value when adding substitution.
technologies
node.js
sendgrid (v2)
==== my sample code (node.js) ====
const SENDGRID_API_KEY = 'KEY'
const sendgrid = require('sendgrid')(SENDGRID_API_KEY)
function sendEmailToSupport() {
const email = new sendgrid.Email({
from: 'jaewwalletsupport#paysbuy.co.th',
to: ['user_a#gmail.com', 'user_b#gmail.com', 'user_c#gmail.com']
html: '<div>test = :test</div>',
subject: 'dummy'
})
email.addSubstitution(':test', 'ddddddddddddd')
sendgrid.send(email, (err, response) => {
if (err) {
console.log(err)
} else {
console.log('Yay! Our templated email has been sent')
}
})
}
module.exports = {
sendEmailToSupport
}
====== result ======
user_a#gmail.com gets email with correct content test = ddddddddddddd
user_b#gmail.com, user_c#gmail.com get email with blank value test =
it looks like the first email in the email.to array will get the correct content, others get blank data.
in the sendgrid web admin, there is no error, everying is fiine.
how to fix this issue?
thanks
after analyse the sendgrid lib, i found the solution now by editing this line
email.addSubstitution(':test', new Array(email.to.length).fill('ddddddddddddd'))
I have used pouchDB in one application and now I want to introduce couchDB to sync the document to remote server. Hence i followed this link http://pouchdb.com/getting-started.html i used the below code to replicate the data to couchDB
var db2 = new PouchDB('todos');
var remoteCouch = 'http://localhost:5984/_utils/database.html?couchdb_sample';
db2.changes({
since: 'now',
live: true
}).on('change', showTodos);
sync();
function sync() {
//alert("sync");
//syncDom.setAttribute('data-sync-state', 'syncing');
//var opts = {live: true};
db2.replicate.to(remoteCouch).on('complete', function () {
console.log("done");
}).on('error', function (err) {
console.log(err);
});
function addTodo(text) {
var todo = {
_id: $("#eid").val()+$("#version").val(),
title: text,
name: $("#nameid").val(),
version: $("#version").val(),
completed: false
};
db2.put(todo, function callback(err, result) {
if (!err) {
console.log('Successfully posted a todo!');
}
else{
console.log(err);
}
});}
here the title has an xml string as value. But i am facing below error
SyntaxError: Unexpected token <
at Object.parse (native)
for this line db2.replicate.to(remoteCouch). I manually created a new document in couchDb database and entered the same data it gave no error but when i try replicating it shows syntax error. Can anyone please hint me where I have gone wrong
http://localhost:5984/_utils/database.html?couchdb_sample
Points to a HTML site (copied over from the browsers address bar, right?). Remove the middle part:
http://localhost:5984/couchdb_sample
It look like you have not defined the remote database in the way PouchDb is expecting. You should use the "new PouchDb" call. The second line of your code is:
var remoteCouch = 'http://localhost:5984/_utils/database.html?couchdb_sample';
but I think it should be like this:
var remoteCouch = new PouchDB('http://localhost:5984/couchdb_sample');
I am not clear from your code what the name of the remote database is, but it would not normally end in ".html" as Ingo Radatz pointed out, so I have assumed it is couchdb_sample above. There is more information about replication on the PouchDb site.