2016-03-03 68 views
1

我試圖使用REST API使用說明,將增加一個用戶的身份到通道:https://www.twilio.com/docs/api/ip-messaging/rest/members#action-createTwilio IP消息用戶沒有找到

我張貼到/Channels/channelId/Members終點 - 我敢肯定我的要求是結構正確。

我得到一個錯誤的Twilio IP消息回話說:

{"code": 50200, "message": "User not found", "more_info": "https://www.twilio.com/docs/errors/50200", "status": 400} 

我的理解是,我們可以提供我們自己的身份,當我們想某人添加到一個頻道。如何在將用戶添加到頻道之前「註冊」用戶(使用電子郵件)?

編輯 - 代碼:

var _getRequestBaseUrl = function() { 
    return 'https://' + 
    process.env.TWILIO_ACCOUNT_SID + ':' + 
    process.env.TWILIO_AUTH_TOKEN + '@' + 
    TWILIO_BASE + 'Services/' + 
    process.env.TWILIO_IPM_SERVICE_SID + '/'; 
}; 

var addMemberToChannel = function(memberIdentity, channelId) {           
    var options = { 
    url: _getRequestBaseUrl() + 'Channels/' + channelId + '/Members',        
    method: 'POST',                     
    headers: { 
     'content-type': 'application/x-www-form-urlencoded',           
    }, 
    form: { 
     Identity: memberIdentity,                  
    }, 
    };                       
    request(options, function(error, response, body) { 
    if (error) { 
     // Getting the error here 
    } 
    // do stuff with response. 
    }); 
};                
addMemberToChannel('[email protected]', <validChannelId>); 
+0

您可以分享您用於發佈POST請求的代碼嗎? – philnash

+0

@philnash:完成。 – sparkFinder

回答

0

Twilio開發者傳道這裏。

爲了添加用戶成爲頻道的成員,您確實需要先註冊它們。查看creating a user in IP Messaging的文檔。

與您的代碼你需要像一個函數:

var createUser = function(memberIdentity) { 
    var options = { 
    url: _getRequestBaseUrl() + 'Users', 
    method:'POST', 
    headers: { 
     'content-type': 'application/x-www-form-urlencoded', 
    }, 
    form: { 
     Identity: memberIdentity,    
    } 
    }; 

    request(options, function(error, response, body) { 
    if (error) { 
     // User couldn't be created 
    } 
    // do stuff with user. 
    }); 
} 

難道我也建議你看一看的Twilio helper library for Node.js。它處理像你爲你做的URL的創建。代碼看起來更乾淨,你可以創建一個像這樣的幫助程序庫的用戶:

var accountSid = 'ACCOUNT_SID'; 
var authToken = 'AUTH_TOKEN'; 
var IpMessagingClient = require('twilio').IpMessagingClient; 

var client = new IpMessagingClient(accountSid, authToken); 
var service = client.services('SERVICE_SID'); 

service.users.create({ 
    identity: 'IDENTITY' 
}).then(function(response) { 
    console.log(response); 
}).fail(function(error) { 
    console.log(error); 
}); 

讓我知道這是否有幫助。

+0

絕對有幫助。我曾嘗試使用twilio客戶端 - (npm [email protected]) - 但它沒有名爲IpMessagingClient的對象。 – sparkFinder

+0

將其降級爲[email protected],您應該再次找到與文檔相匹配的文檔。 3.0.0系列預發佈,文檔還沒有趕上。 – philnash

+0

很酷。謝謝您的幫助! – sparkFinder