How to get the message sender UUID/metadata from pubnub history? - pubnub

I want to know who sent the messages when retrieved from the pubnub.history!
pubnub.history() only returns timetoken and the message.

PubNub Sender UUID: realtime vs history
The sender (publisher) UUID is provided in the message you receive in realtime as a subscriber, but PubNub only stores the actual message you published along with any meta data that provided:
PubNub JavaScript SDK publish docs sample code
pubnub.publish(
{
channel: 'my_channel',
message: {
such: 'object'
},
meta: {
"cool": "meta"
}
},
function (status, response) {
if (status.error) {
// handle error
console.log(status)
} else {
console.log("message Published w/ timetoken", response.timetoken)
}
}
);
Best practice would be to add the publisher's UUID to the meta parameter which will not only allow you to get this value using history API, but you can also use it to filter out messages that the client has sent (don't receive your own messages using Stream Filter).

Related

How change friendly name notify twilio sms

I am having some problems, I want to change the name of the sender. I mean, it is possible to assign an Alphanumeric Sender ID, I reviewed the documentation and followed the guidelines, in the response of the twilio api the name goes but when in the messages I receive it sends them to the same number. I know that it is not something due to the regulations of the country because according to the twilio documentation, it is possible. (https://support.twilio.com/hc/en-us/articles/223133767-International-support-for-Alphanumeric-Sender-ID) What is happening? How can I fix? Do I have to do any configuration?
How I want the sender ID to be seen
As I receive the sender ID
UPDATING QUESTION
Ok, the way I have structured the code is as follows:
I am working on a nodejs project, I need to send a message to multiple phone numbers so in order to do it I used the SMS notification service offered by Twilio, this is the method that was created:
async sendSMSAsNotify(req: Request, res: Response) {
try {
console.log("req.body:", req.body);
let messageBody = req.body.body;
console.log(messageBody);
let numberList = req.body.toBinding;
let extractBody = messageBody.replace(/<[^>]+>/g, '');
console.log(extractBody);
var decodedStripedHtml = he.decode(extractBody);
//console.log(decodedStripedHtml);
//console.log(`Body: ${messageBody}`);
var numbers = [];
for (let i = 0; i < numberList.length; i++) {
numbers.push(
JSON.stringify({
binding_type: "sms",
address: numberList[i],
})
);
}
const notificationOpts = {
toBinding: numbers,
body: decodedStripedHtml,
title: 'MyCompany'
};
// console.log("numbers:", notificationOpts.toBinding);
// console.log("body", notificationOpts.body);
const response = await this.client.notify
.services(process.env.SERVICE_SID_NTF)
.notifications.create(notificationOpts);
console.log('response', response);
res.json({
msg: `Message sent successfully! ${response}`,
});
} catch (e) {
throw new HttpException(HttpErrors.NOT_FOUND_ERROR, HttpStatus.NOT_FOUND);
}
}
The sendSMSAsNotify() method works great, I can send the same SMS to multiple numbers. But now what I want to achieve is that every message I send shows the sender id. I didn't find how to do it in the documentation of the SMS notification service, so I tried to change it and use a very simple method to send SMS via twilio to a single number just for testing.
async sendSMS(sms: SMSDto) {
try {
return await this.client.messages.create({
body: sms.message,
from: 'MyCompany',
to: sms.number,
});
} catch (e) {
return e
}
}
But in neither of the two methods in which I tried to change the sender identification it did not allow me and that is what brings me here, I really need help, it is a requirement that I need to fulfill and I cannot find a way to help me.
First up, while the list of countries that support alphanumeric sender IDs does contain Honduras there are further guidelines for SMS in Honduras that say:
Dynamic Alphanumeric Sender IDs are not fully supported for Honduras mobile operators. Sender IDs may be overwritten with a local long code or short code outside the Twilio platform.
So, even if you set everything up as I am about to explain, it is still possible that your sender ID may be overwritten with a local long code or short code and that Twilio is unable to do anything about that.
That being said, here's how to set up for alphanumeric sender IDs.
Since you are using Notify to send the messages, you will have set up a Messaging Service to use with Notify.
The Messaging Service controls how the SMS messages are sent out from Notify, from a pool of numbers. That pool can also contain your alphanumeric sender ID
So, to send from an alphanumeric sender ID you need to go to your Sender Pool within your Messaging Service and add an alpha sender.
Once you have the alpha sender set in your Messaging Service's pool, it will be used to send your messages out. You can even remove any long code numbers you have in the pool, if you do not plan to use them, though they are useful to fallback to if you do send to a country that doesn't support alphanumeric sender IDs.
Is it possible that you are in a country that does not support alphanumeric sender IDs and that Twilio falls back on a short code then?
PS: It would be helpful if you could add a code snippet, that shows the code you run, to your question.

Direct Messaging custom slack bot built on node.js

I have built a slack bot using the slack/bots apis in node.js: https://slack.dev/bolt-js/tutorial/getting-started
Currently, it is working fine when I type <bot> help in a channel I have set up for using webhooks. I am trying to run those same commands in a DM with the bot using the app.event('app_mention',...) method but it is not working. its like the message doesn't register in a DM with the bot for some reason but it works in a public channel. code snippet below:
app.event('app_mention', async ({ event, client}) => {
console.log(event);
const text = event.text;
const parentMessageId = event.ts;
const eventChannel = event.channel;
if (text.includes("help")) {
console.log(event);
try {
await client.chat.postMessage({
channel: eventChannel,
text: helpMessage,
thread_ts: parentMessageId
});
} catch (err) {
console.error(err);
}
I should have permissions set up correctly as well. I basically have all the permissions that I can add for the bot
The documentation of the app_mention api specifically mentions that this event does not work with DMs.
Messages sent to your app in direct message conversations are not
dispatched via app_mention, whether the app is explicitly mentioned or
otherwise. Subscribe to message.im events to receive messages directed
to your bot user in direct message conversations.
Check here : https://api.slack.com/events/app_mention

How to set up email notifications about new messages in Twilio Programmable chat?

What is a correct way to synchronize Twilio chat consumption horizon and send an email notification with a list of new messages from our own server?
I can use kind of pre-hooks / post-hooks for new messages, but I don't want to keep every message and its reading status in my database.
Is there a smarter way to set up notifications on my server?
Twilio developer evangelist here.
You wouldn't need to store all of this. You can just use the REST API to get all of this information.
What you need to do is store your user's Channel Member Sid. You can then make a call to the Member resource and get their last_consumed_message_index.
With the index, which is an integer representing index of the last message the member has read within the channel, you can then call on the Message resource to list all the messages since that index. In Node, that would be so
service
.channels(CHANNEL_SID)
.members(MEMBER_SID)
.fetch()
.then(member => {
const lastConsumedMessageIndex = member.lastConsumedMessageIndex;
return service.channels(CHANNEL_SID).messages.list({
pageSize: lastConsumedMessageIndex,
limit: lastConsumedMessageIndex
});
})
.then(messages => {
console.log(messages);
// do something with unread messages
})
.catch(error => {
console.error(error);
});
Let me know if that helps at all.

Slack Bot Replying to his own message in Node js

I am trying to make a slack bot in nodejs that replys the user based on their input. But As far now the bot keeps replying to his own messages
this is the code for my bot
let Bot = require('slackbots');
// create a bot
let settings = {
token: 'xoxb-10584202949',
name: 'BotHelp'};
let bot = new Bot(settings);
bot.on('start', function() {
bot.postMessageToChannel('general', 'At your service');
});
bot.on('message',function (data) {
console.log(data);
if (data.username != "BotHelp" && data.subtype != 'bot_message'){
bot.postMessageToChannel('general', 'Yoooo');
}
});
The Console log for data Prints
{ type: 'hello' }
{ text: 'At your service',
username: 'BotHelp',
bot_id: 'B336WGVSM',
type: 'message',
subtype: 'bot_message',
team: 'T2ZAW44P3',
user_team: 'T2ZAW44P3',
channel: 'C303W2D4M',
ts: '1479877794.000266' }
{ type: 'presence_change',
presence: 'active',
user: 'U33QS0VEF' }
So why my validation is failing to check that the message has been sent from bot itself or not?
Thank you for your time
I had the same issue. The message event in slackbots fires for every event ie, user_typing, message_marked, desktop_notification. All events can be found here.
Now what i did to ensure my bot didn't send a message after receiving its own was:
bot.on('message',function (data) {
if (data.user != "Uxxxxxx" && data.message === 'message'){
bot.postMessageToChannel('general', 'Yoooo');
}
the data.user gives a unique id, you can get this by console.log(data) and find your user id.
Also, if you want to specify which message you want to listen to, say direct message. channel id inside data will start with a D. For private channels, the channel id starts with G
Hope this helps. I know there is much more to do here but this is the basic. And you can grow on this. I know I will be trying to :D
I know that the question is already answered but I will like to add my answer, because the slack api has made some changes.
if (data.type !== "message" || data.subtype === 'bot_message' ){
return;
}
else{
bot.postMessageToChannel('general', 'Yoooo');
}
If I were you I'd check if the parameters are really user_name and subtype . If it's right, I would try to check the type of the objects that are coming(Maybe there is a cast in objects coming from the Slack api). I've done a small project very similar to yours, using the same library slackbots and also a library I built myself,nodejslack(https://github.com/marcogbarcellos/nodejslack) but didn't get this issue.

How to be notified of a response message when using RabbitMQ RPC and ServiceStack

Under normal circumstances messages with a response will be published to the response.inq, I understand that and it's a nifty way to notify other parties that "something" has happened. But, when using the RPC pattern, the response goes back to the temp queue and disappears. Is this correct? Is there a convenient way, short of publishing another message, to achieve this behavior, of auto-notification?
The Message Workflow docs in describes the normal message workflow for calling a Service via ServiceStack.RabbitMQ:
Request / Reply
The Request/Reply alters the default message flow by specifying its own ReplyTo address to change the queue where the response gets published, e.g:
const string replyToMq = mqClient.GetTempQueueName();
mqClient.Publish(new Message<Hello>(new Hello { Name = "World" }) {
ReplyTo = replyToMq
});
IMessage<HelloResponse> responseMsg = mqClient.Get<HelloResponse>(replyToMq);
mqClient.Ack(responseMsg);
responseMsg.GetBody().Result //= Hello, World!
When using the Request/Reply pattern no other message is published in any other RabbitMQ topic/queue, to alert other subscribers the client would need to republish the message.
RabbitMqServer callbacks
Another way to find out when a message has been published or received is to use the PublishMessageFilter and GetMessageFilter callbacks on the RabbitMqServer and Client which lets you inspect each message that they sent or received, e.g:
var mqServer = new RabbitMqServer("localhost")
{
PublishMessageFilter = (queueName, properties, msg) => {
//...
},
GetMessageFilter = (queueName, basicMsg) => {
//...
}
};

Resources