botframework-sdk icon indicating copy to clipboard operation
botframework-sdk copied to clipboard

TOKEN of Direct Line API

Open eddie1976 opened this issue 10 years ago • 12 comments

Now I am using Direct Line API in this way.

  1. get 'Credentials' from portal
  2. POST to '/api/conversations' using 'Credentials', and 'conversationId' return
  3. POST message to '/api/conversations/{conversationId}/messages' using 'Credentials'
  4. GET message from '/api/conversations/{conversationId}/messages' using 'Credentials'

[Question 1] I don't know if I am doing the right way?

[Question 2] So when the TOKEN should be use? I found that once the 'conversationId' is generated, it is valid forever, right? Why TOKEN is needed and need to renew? GET '/api/tokens' will return TOKEN POST '/api/conversations' will also return TOKEN GET '/api/tokens/{conversationId}/renew' won't return TOKEN POST ' /api/tokens/conversation' won't return TOKEN

[Question 3] Where are these Conversations Data saved? Will these data saved in Server forever or they will be deleted after a period of time?

eddie1976 avatar May 10 '16 05:05 eddie1976

Last question first since that's easiest, the messages will stay around temporarily but are not permanent. Don't take a dependency on them sticking around.

You can decide whether you want to use a SECRET or a TOKEN to authenticate your application.

Pros of using your secret: easier to code, secret is valid forever for all conversations Cons us using your secret: if you're using your secret in a client application, users may be able to see and steal your secret. The secret can also be used to inspect other conversations (although you have to know/guess the other conversation ID.)

Pros of using a token: valid only for one conversation, valid only for a short period of time Cons of using a token: must exchange secret for token, must refresh token while you want to use it

You can use either a secret or a token -- both are valid. You decide based on what security characteristics are important to you.

Most server-to-server applications calling Direct Line use their secret. Most client-to-server applications calling Direct Line use a token.

dandriscoll avatar May 10 '16 14:05 dandriscoll

Hello, I am would like to start write code using DirectLineAPI to develop a client for my bot. It would be great, if someone shares the basic code to start with...Having some doubts like....it is mentioned that, "get credentials from portal"..so credentials here means...the Bots primary App secret or webchat generated secret....

Looking to see the sample code to start with.

Regards, Giri G.

giriganapathy avatar May 13 '16 19:05 giriganapathy

Hello Team, Can u share some sample code for directline api for me to start...

Regards, Giri G.

giriganapathy avatar May 15 '16 19:05 giriganapathy

Actually i followed the first points from the eddie, but i am always getting empty messages after polling the get messages for a long time....Any guess?

Regards, Giri G.

giriganapathy avatar May 15 '16 19:05 giriganapathy

What you will see when you get message is something like this. { "messages":[ { "id": "DzKFNgal|131067343321045758", "conversationId": "DzKFNgal", "created": "2016-05-03T07:32:12.1045758Z", "from": "GXXPBGLn", "text": "こんにちは", "images": [], "attachments": [], "eTag": "W/"datetime'2016-05-03T07%3A32%3A12.5890281Z'"" }, { "id": "DzKFNgal|131067343351887485", "conversationId": "DzKFNgal", "created": "2016-05-03T07:32:15.1887485Z", "from": "bot-staging", "text": "こんにちは", "images": [], "attachments": [], "eTag": "W/"datetime'2016-05-03T07%3A32%3A15.6679583Z'"" }], "watermark": "1310674335187485" }

eddie1976 avatar May 17 '16 03:05 eddie1976

sorry for the belated response. I am getting an empty array: { "messages": [] }

I posted like this: https://directline.botframework.com/api/conversations/45I5pWxuJbK/messages/ Post message: Content-type: application/json { "id": "string", "conversationId": "45I5pWxuJbK", "created": "2016-05-29T05:17:31.651Z", "from": "string", "text": "status of train 12680", "channelData": "string", "images": [ "string" ], "attachments": [ { "url": "string", "contentType": "string" } ], "eTag": "string" }

got response as 204.

Anything I missed.

giriganapathy avatar May 29 '16 06:05 giriganapathy

I am able to call the directline api now..however I am always getting the first message (as responses)from my bot, every time I post new messages. Can any one help me here....? Eagerly waiting for the reply.

giriganapathy avatar May 31 '16 12:05 giriganapathy

Hi Giri, are you sending the watermark parameter when you GET /api/conversations/id/messages? The watermark is the paging parameter for the messages.

dandriscoll avatar May 31 '16 15:05 dandriscoll

Yes. Basically, I am reusing the chat.js, and here is the code snippet:

            //setting up the url.
    baseUrl = "https://directline.botframework.com";

    var tokenNeedsRenewal = "true";
    var tokenOrSecret = "****************************";
    var botId = "***********";

    var callback = function() {

    }
    var client = new ChatLib.ChatClient(tokenNeedsRenewal, tokenOrSecret, botId, callback);         
    $(document).ready(function() {
        $(document).off('click', '.btn-send');
        $(document).on('click', '.btn-send', function() {               
            var msg = new ChatLib.ChatMessage();
            msg.text = $(".user-message").val();
            client.sendMessage(msg);
        });

                     //I am trying to read the messages stored in conversation array.           
        function readMessages(startMsgIndex) {
            var idx=startMsgIndex;  
            if(idx < client.conversation.length) {
                for(; idx < client.conversation.length; idx++) {
                    $("#messages").html($("#messages").html() + client.conversation[idx].text + "<br/>");
                }
                startMsgIndex = idx;
            }               
            setTimeout(function() {
                readMessages(startMsgIndex);
            }, 500);
        }
        readMessages(0);
    });

I can see the watermark as part of the url... I am building a prototype to prove that the website context can be passed to bot, to respond context based messages.

So I appreciate, if you could help me here... Thanks.

giriganapathy avatar May 31 '16 15:05 giriganapathy

Any suggestions from your end? On 31-May-2016 8:37 PM, "dandriscoll" [email protected] wrote:

Hi Giri, are you sending the watermark parameter when you GET /api/conversations/id/messages? The watermark is the paging parameter for the messages.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/Microsoft/BotBuilder/issues/254#issuecomment-222718289, or mute the thread https://github.com/notifications/unsubscribe/AR4V4KiR0GvzHMNVxkmX5OuavTlHWOaJks5qHE62gaJpZM4IaxLf .

giriganapathy avatar May 31 '16 17:05 giriganapathy

hello dandriscoll, did u get a chance to debug this issue..? I am waiting for your response....Thanks.

giriganapathy avatar Jun 01 '16 11:06 giriganapathy

directline_same_message

Attached a snapshot of the scenario for your reference.

Regards, Giri G.

giriganapathy avatar Jun 01 '16 15:06 giriganapathy