Skip to content

Commit

Permalink
get chat bot working
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam committed Nov 8, 2019
1 parent bb6ab5c commit 4df3127
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 19 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/api/bin/Debug/netcoreapp2.2/api.dll",
"program": "${workspaceFolder}/api/bin/Debug/netcoreapp2.2/Dta.Frontdoor.Api.dll",
"args": [],
"envFile": "${workspaceFolder}/config/local.env",
"cwd": "",
Expand Down
7 changes: 4 additions & 3 deletions api/Controllers/BotConnectorController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ public BotConnectorController(IConfiguration configuration)
_configuration = configuration;
}

// GET api/values/5
[HttpGet]
public async Task<ActionResult<string>> Get(int id)
public async Task<dynamic> Get()
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Add("Authorization", $"BotConnector {_configuration["botConnector"]}");
var response = await client.GetStringAsync("https://webchat.botframework.com/api/tokens");
return response;
return new {
token = response.Trim('"')
};
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,19 @@ public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

private readonly string _devOrigins = "_devOrigins";
public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(options => {
options.AddPolicy(_devOrigins, builder => {
builder.WithOrigins("http://localhost:3000")
.AllowAnyHeader()
.AllowAnyMethod();
});
});
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

services.AddOptions();
Expand All @@ -44,7 +51,7 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}

app.UseCors(_devOrigins);
app.UseHttpsRedirection();
app.UseMvc();
}
Expand Down
77 changes: 65 additions & 12 deletions ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"@gov.au/grid-12": "^2.1.0",
"@gov.au/header": "^4.1.12",
"@gov.au/main-nav": "^1.0.8",
"axios": "^0.19.0",
"botframework-webchat": "^4.6.0",
"express": "^4.17.1",
"node-sass": "^4.13.0",
Expand All @@ -33,6 +34,7 @@
"lint": "tslint --config ./tslint.json --project ./",
"lint:fix": "tslint --project ./ --fix"
},
"proxy": "http://localhost:5000",
"eslintConfig": {
"extends": "react-app"
},
Expand Down
18 changes: 17 additions & 1 deletion ui/src/pages/buyer/BuyerPage.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@
import React from 'react';
import axios from 'axios';
import { DirectLine } from 'botframework-directlinejs';
import ReactWebChat from 'botframework-webchat';
import React, { useEffect, useRef, useState } from 'react';
import '../../main.scss';

const BuyerPage: React.FC = () => {
const [loaded, setLoaded] = useState(false);
const directLine = useRef<DirectLine | null>(null);
useEffect(() => {
if (!loaded) {
axios.get('/api/BotConnector')
.then((r) => {
directLine.current = new DirectLine({ token: r.data.token });
setLoaded(true);
});
}
});

return (
<div>
{'Buying digital products & services'}
{loaded && <ReactWebChat directLine={directLine.current} />}
</div>
);
};
Expand Down

0 comments on commit 4df3127

Please sign in to comment.