8000 Initial bot placeholder · guidotorresmx/botbuilder-python@509d4aa · GitHub
[go: up one dir, main page]

Skip to content

Commit 509d4aa

Browse files
committed
Initial bot placeholder
1 parent 8406f24 commit 509d4aa

File tree

7 files changed

< 8000 div class="ml-1 text-small text-bold fgColor-success">+183
-0
lines changed

7 files changed

+183
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
FROM tiangolo/uwsgi-nginx-flask:python3.6
5+
6+
# Setup for nginx
7+
RUN mkdir -p /home/LogFiles \
8+
&& apt update \
9+
&& apt install -y --no-install-recommends vim
10+
11+
EXPOSE 3978
12+
13+
COPY /functionaltestbot /functionaltestbot
14+
15+
16+
RUN pip3 install -r /functionaltestbot/requirements.txt
17+
18+
ENV FLASK_APP=/functionaltestbot/main.py
19+
ENV LANG=C.UTF-8
20+
ENV LC_ALL=C.UTF-8
21+
ENV PATH ${PATH}:/home/site/wwwroot
22+
23+
WORKDIR bot
24+
# Initialize models
25+
26+
27+
# For Debugging, uncomment the following:
28+
#ENTRYPOINT ["python3.6", "-c", "import time ; time.sleep(500000)"]
29+
ENTRYPOINT [ "flask" ]
30+
CMD [ "run", "--port", "3978", "--host", "0.0.0.0" ]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Client Driver for Function E2E test
2+
3+
This contains the client code that drives the bot functional test.
4+
5+
It performs simple operations against the bot and validates results.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Console EchoBot
2+
Bot Framework v4 console echo sample.
3+
4+
This bot has been created using [Bot Framework](https://dev.botframework.com), it shows how to create a simple bot that you can talk to from the console window.
5+
6+
This sample shows a simple echo bot and demonstrates the bot working as a console app using a sample console adapter.
7+
8+
## To try this sample
9+
- Clone the repository
10+
```bash
11+
git clone https://github.com/Microsoft/botbuilder-python.git
12+
```
13+
14+
15+
### Visual studio code
16+
- open `botbuilder-python\samples\01.console-echo` folder
17+
- Bring up a terminal, navigate to `botbuilder-python\samples\01.console-echo` folder
18+
- type 'python main.py'
19+
20+
21+
# Adapters
22+
[Adapters](https://docs.microsoft.com/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0#the-bot-adapter) provide an abstraction for your bot to work with a variety of environments.
23+
24+
A bot is directed by it's adapter, which can be thought of as the conductor for your bot. The adapter is responsible for directing incoming and outgoing communication, authentication, and so on. The adapter differs based on it's environment (the adapter internally works differently locally versus on Azure) but in each instance it achieves the same goal.
25+
26+
In most situations we don't work with the adapter directly, such as when creating a bot from a template, but it's good to know it's there and what it does.
27+
The bot adapter encapsulates authentication processes and sends activities to and receives activities from the Bot Connector Service. When your bot receives an activity, the adapter wraps up everything about that activity, creates a [context object](https://docs.microsoft.com/en-us/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0#turn-context), passes it to your bot's application logic, and sends responses generated by your bot back to the user's channel.
28+
29+
30+
# Further reading
31+
32+
- [Azure Bot Service Introduction](https://docs.microsoft.com/azure/bot-service/bot-service-overview-introduction?view=azure-bot-service-4.0)
33+
- [Bot basics](https://docs.microsoft.com/azure/bot-service/bot-builder-basics?view=azure-bot-service-4.0)
34+
- [Channels and Bot Connector service](https://docs.microsoft.com/azure/bot-service/bot-concepts?view=azure-bot-service-4.0)
35+
- [Activity processing](https://docs.microsoft.com/azure/bot-service/bot-builder-concept-activity-processing?view=azure-bot-service-4.0)
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
import asyncio
5+
import sys
6+
from types import MethodType
7+
8+
from flask import Flask, request, Response
9+
from botbuilder.core import (
10+
BotFrameworkAdapter,
11+
BotFrameworkAdapterSettings,
12+
MessageFactory,
13+
TurnContext,
14+
)
15+
from botbuilder.schema import Activity, InputHints
16+
from bot import MyBot
17+
18+
#Create the loop and Flask app
19+
LOOP = asyncio.get_event_loop()
20+
APP = Flask(__name__, instance_relative_config=True)
21+
APP.config.from_object("config.DefaultConfig")
22+
23+
# Create adapter.
24+
# See https://aka.ms/about-bot-adapter to learn more about how bots work.
25+
SETTINGS = BotFrameworkAdapterSettings(APP.config["APP_ID"], APP.config["APP_PASSWORD"])
26+
ADAPTER = BotFrameworkAdapter(SETTINGS)
27+
28+
# Catch-all for errors.
29+
async def on_error(self, context: TurnContext, error: Exception):
30+
# This check writes out errors to console log .vs. app insights.
31+
# NOTE: In production environment, you should consider logging this to Azure
32+
# application insights.
33+
print(f"\n [on_turn_error]: {error}", file=sys.stderr)
34+
35+
# Send a message to the user
36+
error_message_text = "Sorry, it looks like something went wrong."
37+
error_message = MessageFactory.text(
38+
error_message_text, error_message_text, InputHints.expecting_input
39+
)
40+
await context.send_activity(error_message)
41+
42+
43+
ADAPTER.on_turn_error = MethodType(on_error, ADAPTER)
44+
45+
# Create the main dialog
46+
BOT = MyBot()
47+
48+
# Listen for incoming requests on /api/messages.
49+
@APP.route("/api/messages", methods=["POST"])
50+
def messages():
51+
# Main bot message handler.
52+
if "application/json" in request.headers["Content-Type"]:
53+
body = request.json
54+
else:
55+
return Response(status=415)
56+
57+
activity = Activity().deserialize(body)
58+
auth_header = (
59+
request.headers["Authorization"] if "Authorization" in request.headers else ""
60+
)
61+
62+
async def aux_func(turn_context):
63+
await BOT.on_turn(turn_context)
64+
65+
try:
66+
task = LOOP.create_task(
67+
ADAPTER.process_activity(activity, auth_header, aux_func)
68+
)
69+
LOOP.run_until_complete(task)
70+
return Response(status=201)
71+
except Exception as exception:
72+
raise exception
73+
74+
75+
if __name__ == "__main__":
76+
try:
77+
APP.run(debug=False, port=APP.config["PORT"]) # nosec debug
78+
except Exception as exception:
79+
raise exception
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright (c) Microsoft Corporation. All rights reserved.
2+
# Licensed under the MIT License.
3+
4+
from botbuilder.core import ActivityHandler, TurnContext
5+
from botbuilder.schema import ChannelAccount
6+
7+
class MyBot(ActivityHandler):
8+
# See https://aka.ms/about-bot-activity-message to learn more about the message and other activity types.
9+
10+
async def on_message_activity(self, turn_context: TurnContext):
11+
await turn_context.send_activity(f"You said '{ turn_context.activity.text }'")
12+
13+
async def on_members_added_activity(self, members_added: ChannelAccount, turn_context: TurnContext):
14+
for member_added in members_added:
15+
if member_added.id != turn_context.activity.recipient.id:
16+
await turn_context.send_activity("Hello and welcome!")
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License.
4+
5+
import os
6+
7+
""" Bot Configuration """
8+
9+
10+
class DefaultConfig:
11+
""" Bot Configuration """
12+
13+
PORT = 3978
14+
APP_ID = os.environ.get("MicrosoftAppId", "")
15+
APP_PASSWORD = os.environ.get("MicrosoftAppPassword", "")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
botbuilder-core>=4.5.0.b4
2+
flask>=1.0.3
3+

0 commit comments

Comments
 (0)
0