User96
User96

Reputation: 53

How to add an activity when 'DIRECT_LINE/CONNECT_FULFILLED' in Directline Speech webchat

I tried this example for sending an activity to bot when the connection is established, Its working fine. Now I want use Directline speech instead of webchat, It also has a sample here. But here how can I add a welcome event? Here is the code I tried but it is not sending any activity

<script>
    (async function () {

        const adapters = await window.WebChat.createDirectLineSpeechAdapters({
            fetchCredentials: {
                region: 'region',
                subscriptionKey: 'my subscription key'
            }
        }, ({ dispatch }) => next => action => {
            if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
                dispatch({
                    type: 'WEB_CHAT/SEND_EVENT',
                    payload: {
                        name: 'webchat/join',
                        value: 'my username'
                    }
                });
            }

            return next(action);
        });

        window.WebChat.renderWebChat(
            {

                ...adapters
            },
            document.getElementById('webchat')
        );

        document.querySelector('#webchat > *').focus();
    })().catch(err => console.error(err));
</script>

Upvotes: 0

Views: 715

Answers (1)

User96
User96

Reputation: 53

I got it working by adding a store, here is the code

<script>
    (async function () {

        const store = window.WebChat.createStore({}, ({ dispatch }) => next => action => {
            if (action.type === 'DIRECT_LINE/CONNECT_FULFILLED') {
                dispatch({
                    type: 'WEB_CHAT/SEND_EVENT',
                    payload: {
                        name: 'webchat/join',
                        value: 'my username'
                    }
                });
            }

            return next(action);
        });

        const adapters = await window.WebChat.createDirectLineSpeechAdapters({
            fetchCredentials: {
                region: 'region',
                subscriptionKey: 'subscription key'
            }
        });


        window.WebChat.renderWebChat(
            {

                ...adapters, store
            },
            document.getElementById('webchat')
        );

        document.querySelector('#webchat > *').focus();
    })().catch(err => console.error(err));
</script>

Upvotes: 0

Related Questions