0.0.41 • Published 7 years ago
nexmo-conversation-js v0.0.41
Conversation SDK for JavaScript
Deprecated (Moved to nexmo-conversation) Please npm install nexmo-conversation
instead
Quick Steps
- Create your Nexmo account
- Install
nexmo-cli
tool - Create a new application
- Generate a JWT token
- Register your new user to the Conversation Service (Step 4)
- Generate a JWT token (with sub=
[username]
)
Setup
$ npm install nexmo-conversation
create a nexmo app using the nexmo-cli
$ nexmo app:create "Application name" --keyfile private.key
get a token to create a user:
$ nexmo jwt:generate private.key application_id=[application_id]
create a user in your app
$ curl -X POST -H 'Authorization: Bearer [JWT]' -H 'Content-Type:application/json' -d '{"name":"[username]"}' https://api.nexmo.com/beta/users
include the script in your web page
<script src="node_modules/nexmo-conversation/dist/conversationClient.js"></script>
get a user's token
$ nexmo jwt:generate private.key application_id=[application_id] sub=[username]
Create an instance and login
var rtc = new ConversationClient({debug:false});
//var token = request login token as above, with sub=<username>
rtc.login(token).then(
function(application){
//use the application object to manage the conversations
//access the available conversations
console.log(application.conversations);
});
Enable notifications
rtc.notifications({
state: "on",
dir: "auto",
lang: "EN",
tag: "notificationPopUp",
icon: "https://int-dir.s3.amazonaws.com/uploads/282_282_social_icon_360x360.png"
});
Show a notification
rtc.notify("Title", "Message");
Create a new Conversation
var conversationData = {name:'My Nexmo Conversation'};
application.newConversation(conversationData).then(
function(conversation) {
//join the created conversation
conversation.join(application.me).then(
function(member) {
console.log("Joined as " + member.name);
}).catch(
function(error) {
console.log(error);
});
}).catch(function(error) {
console.log(error);
});
Get a conversation you are a member
application.getConversation(conversation_id).then(
function(conv) {
conversation = conv;
});
Set up Text listener for incoming Text Events
conversation.on("text", function(sender, textEvent){
if (textEvent.cid === conversation.id){
//if (rtc.isVisible) { textEvent.seen(); }
console.log("my message was:", textEvent, sender);
}else{
console.log("got a message from another member:", textEvent, sender);
}
});
Sending a Text Event
conversation.sendText("Hi Nexmo").then(
function(){
console.log('message was sent');
}).catch(function(error){
console.log('error sending the message', error);
});
Sending an Image
conversation.sendImage(fileInput.files[0]);
Setup an ImageEvent listener and download the image file
conversation.on("image", function(sender, imageEvent){
imageEvent.fetchImage().then(function(imagedata) {
message.body.thumbnail = imagedata;
});
}
Build
$ npm install
$ cd node_modules/nexmo-conversation
$ grunt
0.0.41
7 years ago
0.0.40
7 years ago
0.0.39
7 years ago
0.0.38
8 years ago
0.0.37
8 years ago
0.0.36
8 years ago
0.0.35
8 years ago
0.0.34
8 years ago
0.0.33
8 years ago
0.0.32
8 years ago
0.0.31
8 years ago
0.0.30
8 years ago
0.0.29
8 years ago
0.0.28
8 years ago
0.0.27
8 years ago
0.0.26
8 years ago
0.0.25
8 years ago
0.0.24
8 years ago
0.0.23
8 years ago
0.0.22
8 years ago