Skip to content

Commit

Permalink
Merge pull request #2 from ramvzz/feature/webhook_based_chatbot
Browse files Browse the repository at this point in the history
Chat-bot Support for Webhooks (MS Bot Framework-based MS Teams Chat-bot)
  • Loading branch information
mmoiyadi authored Oct 10, 2024
2 parents e0f22fd + 19cda2e commit ae2d2ec
Show file tree
Hide file tree
Showing 15 changed files with 333 additions and 1,414 deletions.
1 change: 1 addition & 0 deletions mindsdb/api/executor/datahub/datanodes/mindsdb_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ class ChatbotsTable(MdbTable):
"PARAMS",
"IS_RUNNING",
"LAST_ERROR",
"WEBHOOK_TOKEN",
]

@classmethod
Expand Down
2 changes: 2 additions & 0 deletions mindsdb/api/http/initialize.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
from mindsdb.api.http.namespaces.tree import ns_conf as tree_ns
from mindsdb.api.http.namespaces.views import ns_conf as views_ns
from mindsdb.api.http.namespaces.util import ns_conf as utils_ns
from mindsdb.api.http.namespaces.webhooks import ns_conf as webhooks_ns
from mindsdb.interfaces.database.integrations import integration_controller
from mindsdb.interfaces.database.database import DatabaseController
from mindsdb.interfaces.file.file_controller import FileController
Expand Down Expand Up @@ -246,6 +247,7 @@ def root_index(path):
api.add_namespace(ns)
api.add_namespace(default_ns)
api.add_namespace(auth_ns)
api.add_namespace(webhooks_ns)

@api.errorhandler(Exception)
def handle_exception(e):
Expand Down
3 changes: 3 additions & 0 deletions mindsdb/api/http/namespaces/configs/webhooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from flask_restx import Namespace

ns_conf = Namespace('webhooks', description='API to receive messages from bots')
29 changes: 29 additions & 0 deletions mindsdb/api/http/namespaces/webhooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from flask import request
from flask_restx import Resource

from mindsdb.api.http.namespaces.configs.webhooks import ns_conf
from mindsdb.interfaces.chatbot.chatbot_controller import ChatBotController
from mindsdb.metrics.metrics import api_endpoint_metrics


# Stores the memory of the various chat-bots mapped by their webhook tokens.
# This is required because each time a new request is made, a new instance of the ChatBotTask is created.
# This causes the memory to be lost.
chat_bot_memory = {}


@ns_conf.route('/chatbots/<webhook_token>')
class ChatbotWebhooks(Resource):
@ns_conf.doc('chatbots_webhook')
@api_endpoint_metrics('POST', '/webhooks/chatbots/<webhook_token>')
def post(self, webhook_token: str) -> None:
"""
This endpoint is used to receive messages posted by bots from different platforms.
Args:
webhook_token (str): The token of the webhook. It is used to uniquely identify the webhook.
"""
request_data = request.json

chat_bot_controller = ChatBotController()
return chat_bot_controller.on_webhook(webhook_token, request_data, chat_bot_memory)

This file was deleted.

Loading

0 comments on commit ae2d2ec

Please sign in to comment.