Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bot/bot_templates/main.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import asyncio
import logging
from aiogram import Bot, Dispatcher, F
import re
from aiogram.types import Message
from aiogram.types import Message, CallbackQuery
from aiogram.client.session.aiohttp import AiohttpSession
from aiogram.fsm.storage.memory import MemoryStorage
from aiogram.client.telegram import TelegramAPIServer
Expand Down
27 changes: 19 additions & 8 deletions bot/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,40 @@ def generate_code(bot: Bot) -> str:
bot_id=bot.id,
component_type=Component.ComponentType.TRIGGER,
)
bot_component_codes = ""
bot_component_codes = []
raw_state_check = ""
for component in components:
if component.component_content_type.model != "onmessage":
raise ValidationError(
"Only OnMessage trigger components are supported. you requested {component.__class__.__name__}",
)

for next_component in component.get_all_next_components():
# if next_component.id == component.id:
# continue
object = next_component.component_content_type.model_class().objects.get(
pk=next_component.pk,
)
bot_component_codes += object.generate_code()
bot_component_codes += "\n" * 2
code_result = object.generate_code()
if isinstance(code_result, tuple):
keyboard, callback_code = code_result
if keyboard:
bot_component_codes.append(keyboard)
if callback_code:
bot_component_codes.append(callback_code)
else:
if "raw_state" in code_result:
raw_state_check += code_result + "\n"
else:
bot_component_codes.append(code_result)

if raw_state_check:
bot_component_codes.append(raw_state_check)

with open("bot/bot_templates/main.txt") as f:
base = f.read()

code = base.format(
FUNCTION_CODES=bot_component_codes,
FUNCTION_CODES="\n\n".join(bot_component_codes),
TOKEN=bot.token,
BASE_URL=settings.BALE_API_URL,
)
print("FOOOOO", code)

return black.format_str(code, mode=black.Mode())
181 changes: 150 additions & 31 deletions bot/test/tests.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import unittest
from io import BytesIO

from django.core.files.base import ContentFile
from django.test import TestCase
from django.urls import reverse
from PIL import Image
from rest_framework import status

from bot.models import Bot
from component.models import Component, Markup, OnMessage, SetState
from component.models import (
CodeComponent,
Component,
Markup,
OnMessage,
SetState,
SwitchComponent,
)
from component.telegram.models import SendMessage, SendPhoto
from iam.models import IamUser
from iam.utils import create_token_for_iamuser
Expand All @@ -35,64 +38,180 @@ def setUp(self):
component_type=Component.ComponentType.TRIGGER,
text="/start",
)
send_message_comp = SendMessage.objects.create(
send_message_start = SendMessage.objects.create(
bot=self.bot,
chat_id=693259126,
text="Hello, World!",
chat_id=".from_user.id",
text="Hello welcome to the bot",
position_x=1,
position_y=1,
previous_component=on_message_component,
)
SendMessage.objects.create(
CodeComponent.objects.create(
bot=self.bot,
chat_id=693259126,
text="Bye World",
code="print('This is log message to validate code')",
position_x=1,
position_y=1,
previous_component=on_message_component,
)

image = Image.new("RGB", (100, 100), color="blue")
image_io = BytesIO()
image.save(image_io, "PNG")
image_content = ContentFile(image_io.getvalue(), "test.png")
photo_component = SendPhoto.objects.create(
send_support = SendMessage.objects.create(
bot=self.bot,
chat_id=".chat.id",
photo=image_content,
chat_id=".from_user.id",
text="Send your message then i want to send to admin",
position_x=1,
position_y=1,
)
SetState.objects.create(
bot=self.bot,
state="support",
position_x=1,
position_y=1,
previous_component=send_support,
)

send_message_help = SendMessage.objects.create(
bot=self.bot,
chat_id=".from_user.id",
text="This is test help message",
position_x=1,
position_y=1,
)

send_message_accept_terms = SendMessage.objects.create(
bot=self.bot,
chat_id=".from_user.id",
text="Some text to accept terms and conditions... Do you accept?, send Yes or No",
position_x=1,
position_y=1,
previous_component=on_message_component,
)
SetState.objects.create(
bot=self.bot,
state="accept_terms",
position_x=1,
position_y=1,
previous_component=send_message_accept_terms,
)

Markup.objects.create(
parent_component=photo_component,
parent_component=send_message_start,
markup_type=Markup.MarkupType.ReplyKeyboard,
buttons=[["Button 1", "Button 2"], ["Button 3", "Button 4"]],
buttons=[
[{"value": "Help", "next_component": send_message_help.id}],
[
{
"value": "Accept terms and conditions",
"next_component": send_message_accept_terms.id,
},
],
[{"value": "Support", "next_component": send_support.id}],
],
)

SetState.objects.create(
handle_support = OnMessage.objects.create(
bot=self.bot,
state="state",
state="support",
position_x=1,
position_y=1,
previous_component=on_message_component,
)
send_message_to_admin = SendMessage.objects.create(
bot=self.bot,
chat_id="693259126",
text="text send from user",
position_x=1,
position_y=1,
previous_component=handle_support,
)
send_message_to_user = SendMessage.objects.create(
bot=self.bot,
chat_id=".from_user.id",
text="your message send to admin",
position_x=1,
position_y=1,
previous_component=send_message_to_admin,
)

on_state_component = OnMessage.objects.create(
handle_accept_terms = OnMessage.objects.create(
bot=self.bot,
state="state",
state="accept_terms",
position_x=1,
position_y=1,
)
send_message_accept_terms_yes = SendMessage.objects.create(
bot=self.bot,
chat_id=".from_user.id",
text="You accepted terms and conditions, send your phone number start with +98",
position_x=1,
position_y=1,
)

SendMessage.objects.create(
send_message_accept_terms_no = SendMessage.objects.create(
bot=self.bot,
chat_id=693259126,
text="State set",
chat_id=".from_user.id",
text="You did not accept terms and conditions",
position_x=1,
position_y=1,
previous_component=on_state_component,
)

SwitchComponent.objects.create(
bot=self.bot,
position_x=1,
position_y=1,
previous_component=handle_accept_terms,
expression=".text",
values=["Yes", "No"],
next_components=[
send_message_accept_terms_yes.id,
send_message_accept_terms_no.id,
],
)

phone_number_hanndle = OnMessage.objects.create(
bot=self.bot,
regex=True,
text="^\\+98\\d+$",
position_x=1,
position_y=1,
)
send_message_phone_number = SendMessage.objects.create(
bot=self.bot,
chat_id=".from_user.id",
text="Is your phone number correct?",
position_x=1,
position_y=1,
previous_component=phone_number_hanndle,
)
phone_number_handle_yes_no = SendMessage.objects.create(
bot=self.bot,
chat_id=".from_user.id",
text=".text",
position_x=1,
position_y=1,
previous_component=phone_number_hanndle,
)

handle_yes = SendMessage.objects.create(
bot=self.bot,
chat_id=".from_user.id",
text="OK, thanks",
position_x=1,
position_y=1,
)

handle_no = SendMessage.objects.create(
bot=self.bot,
chat_id=".from_user.id",
text="OK, you lose..., send /start to start again",
position_x=1,
position_y=1,
)

Markup.objects.create(
parent_component=phone_number_handle_yes_no,
markup_type=Markup.MarkupType.InlineKeyboard,
buttons=[
[{"value": "Yes", "next_component": handle_yes.id}],
[{"value": "No", "next_component": handle_no.id}],
],
)

def test_create_bot(self):
Expand All @@ -106,5 +225,5 @@ def test_create_bot(self):
)
self.assertEqual(response.status_code, status.HTTP_200_OK, response.content)
# print(response.content.decode())
with open("code.py", "w") as f:
with open("code_1.py", "w") as f:
f.write(response.content.decode())
Loading