Skip to content

Commit 530b7a8

Browse files
committed
Adding bot waypoint
1 parent 4adb09d commit 530b7a8

File tree

149 files changed

+27723
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

149 files changed

+27723
-0
lines changed

chapter-14/Shakespearebot-waypoint/bot/__init__.py

Whitespace-only changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from import_export.admin import ImportExportModelAdmin
2+
from django.contrib import admin
3+
from .models import Text
4+
5+
@admin.register(Text)
6+
7+
class TextAdmin(ImportExportModelAdmin):
8+
pass
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from django.apps import AppConfig
2+
3+
4+
class BotConfig(AppConfig):
5+
name = 'bot'
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Generated by Django 3.0.7 on 2020-06-27 18:17
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
initial = True
9+
10+
dependencies = [
11+
]
12+
13+
operations = [
14+
migrations.CreateModel(
15+
name='Text',
16+
fields=[
17+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
18+
('PlayerLine', models.CharField(max_length=1000)),
19+
],
20+
),
21+
]

chapter-14/Shakespearebot-waypoint/bot/migrations/__init__.py

Whitespace-only changes.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.db import models
2+
3+
class Text(models.Model):
4+
PlayerLine = models.CharField(max_length=1000)
5+
def __str__(self):
6+
return self.PlayerLine
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<!DOCTYPE html>
2+
3+
<html>
4+
5+
<head>
6+
<style>
7+
textarea {
8+
height: 500px;
9+
width: 300px;
10+
}
11+
</style>
12+
</head>
13+
14+
<body>
15+
<form method="POST" type="" id="chat">
16+
<input type="text" id="chattext"></textarea>
17+
<button id="submit">Chat</button>
18+
<textarea id="chatresponse"></textarea>
19+
</form>
20+
<script>
21+
document.getElementById('submit').addEventListener('click', (e) => {
22+
e.preventDefault()
23+
24+
let term = document.getElementById('chattext').value.split(' ')
25+
term = term[term.length - 2] || term[0]
26+
27+
fetch("/api", {
28+
method: "POST",
29+
headers: {
30+
'Content-Type': 'application/json'
31+
},
32+
body: JSON.stringify({ chattext: term })
33+
})
34+
.then(response => response.text())
35+
.then(data => document.querySelector('#chatresponse').value += `\n${data}\n`)
36+
})
37+
</script>
38+
39+
</body>
40+
41+
</html>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.urls import path
2+
from . import views
3+
4+
urlpatterns = [
5+
path('', views.index, name='index'), path('api', views.api, name='api'),
6+
]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from django.http import HttpResponse
2+
from django.template import Context, loader
3+
from bot.models import Text
4+
import random
5+
import json
6+
7+
8+
def index(request):
9+
template = loader.get_template("bot/index.html")
10+
11+
return HttpResponse(template.render())
12+
13+
14+
def api(request):
15+
if request.method == 'POST':
16+
data = json.loads(request.body.decode("utf8"))
17+
query = data['chattext']
18+
responses = Text.objects.filter(PlayerLine__contains=" %s " % (query))
19+
20+
if len(responses) > 0:
21+
return HttpResponse(responses[random.randint(0, len(responses))])
22+
23+
else:
24+
return HttpResponse("Get thee to a nunnery!")

0 commit comments

Comments
 (0)