Skip to content

Commit

Permalink
typing action added
Browse files Browse the repository at this point in the history
  • Loading branch information
brainboost committed Apr 8, 2023
1 parent 210a82b commit f4a5eee
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 22 deletions.
16 changes: 9 additions & 7 deletions lambda/bing_gpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
logging.basicConfig()
logging.getLogger().setLevel("INFO")


class BingGpt:
ref_link_pattern = re.compile(r"\[(.*?)\]\:\s?(.*?)\s\"(.*?)\"\n?")
esc_pattern = re.compile(f"(?<!\|)([{re.escape(r'.-+#|{}!=()')}])(?!\|)")

def __init__(self) -> None:
_ssm_client = boto3.client(service_name="ssm")
s3_path = _ssm_client.get_parameter(Name="COOKIES_FILE")["Parameter"]["Value"]
Expand Down Expand Up @@ -46,8 +48,7 @@ def read_plain_text(cls, response: dict) -> str:
@classmethod
def read_markdown(cls, response: dict) -> str:
message = response["item"]["messages"][1]["adaptiveCards"][0]["body"][0]["text"]
text = BingGpt.replace_references(text=message)
return BingGpt.escape_markdown_v2(text=text)
return BingGpt.replace_references(text=message)

@classmethod
def read_html(cls, response: dict) -> str:
Expand All @@ -57,20 +58,21 @@ def read_html(cls, response: dict) -> str:

@classmethod
def replace_references(cls, text: str) -> str:
ref_link_pattern = re.compile(r"\[(.*?)\]\:\s?(.*?)\s\"(.*?)\"\n?")
ref_links = re.findall(pattern=ref_link_pattern, string=text)
ref_links = re.findall(pattern=cls.ref_link_pattern, string=text)
text = re.sub(pattern=cls.ref_link_pattern, repl="", string=text)
text = BingGpt.escape_markdown_v2(text=text)
for link in ref_links:
link_label = link[0]
link_ref = link[1]
inline_link = f" [\[{link_label}\]]({link_ref})"
text = re.sub(pattern=rf"\[\^{link_label}\^\]\[\d+\]",
repl=inline_link, string=text)
return re.sub(pattern=ref_link_pattern, repl="", string=text)
return text

@classmethod
def remove_links(cls, text: str) -> str:
return re.sub(pattern=r"\[\^\d+\^\]\s?", repl="", string=text)

@classmethod
def escape_markdown_v2(cls, text: str) -> str:
return re.sub(f"([{re.escape(r'.')}])", r"\\\1", text)
return re.sub(pattern=cls.esc_pattern, repl=r"\\\1", string=text)
4 changes: 2 additions & 2 deletions lambda/chatbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
filters,
)
from user_config import UserConfig
from utils import generate_transcription
from utils import generate_transcription, send_typing_action

example_tg = '''
*bold \*text*
Expand Down Expand Up @@ -114,7 +114,7 @@ async def process_message(update, context: ContextTypes.DEFAULT_TYPE):
logging.error(e)


# @send_typing_action
@send_typing_action
async def processing_internal(update, context: ContextTypes.DEFAULT_TYPE):
# chat_id = update.message.chat_id
chat_text = update.message.text.replace(bot.name, "")
Expand Down
40 changes: 27 additions & 13 deletions lambda/utils.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
import uuid
import os
import json
import logging
import os
import uuid
from functools import wraps

import boto3
import wget
import logging
from telegram import constants
from functools import wraps

logging.basicConfig()
logging.getLogger().setLevel("INFO")


async def send_typing_action(func):
"""Sends typing action while processing func command."""
def send_action(action):
"""Sends `action` while processing func command."""

def decorator(func):
@wraps(func)
async def command_func(update, context, *args, **kwargs):
await context.bot.send_chat_action(chat_id=update.effective_message.chat_id,
action=action)
return await func(update, context, *args, **kwargs)
return command_func

return decorator

@wraps(func)
async def command_func(update, context, *args, **kwargs):
await context.bot.send_chat_action(
chat_id=update.effective_message.chat_id, action=constants.ChatAction.TYPING
)
return func(update, context, *args, **kwargs)
send_typing_action = send_action(constants.ChatAction.TYPING)
# async def send_typing_action(func):
# """Sends typing action while processing func command."""

return command_func
# @wraps(func)
# async def command_func(update, context, *args, **kwargs):
# await context.bot.send_chat_action(
# chat_id=update.effective_message.chat_id, action=constants.ChatAction.TYPING
# )
# return func(update, context, *args, **kwargs)

# return command_func

def generate_transcription(file):
s3_client = boto3.client("s3")
Expand Down
15 changes: 15 additions & 0 deletions stacks/database_stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,19 @@ def __init__(self, scope: Construct, id: str, **kwargs) -> None:
),
removal_policy=RemovalPolicy.RETAIN,
billing_mode=dynamodb.BillingMode.PAY_PER_REQUEST
)

dynamodb.Table(
self, "conversations-table",
table_name="conversations",
partition_key=dynamodb.Attribute(
name="conversation_id",
type=dynamodb.AttributeType.STRING
),
sort_key=dynamodb.Attribute(
name="user_id",
type=dynamodb.AttributeType.NUMBER
),
removal_policy=RemovalPolicy.RETAIN,
billing_mode=dynamodb.BillingMode.PAY_PER_REQUEST
)

0 comments on commit f4a5eee

Please sign in to comment.