-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Yonathan.Task.4 #48
Open
yonaries
wants to merge
1
commit into
main
Choose a base branch
from
Yonathan.Task.4
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Yonathan.Task.4 #48
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Choosing an image for you container. | ||
FROM python:3.11.0 | ||
# Setting your working directory | ||
WORKDIR /app | ||
# This command would copy EVERY FILE from your project folder into your container, so be careful. | ||
COPY . /app | ||
# Installing needed packages and dependencies.** | ||
RUN pip install -r requirements.txt | ||
# This command basically executes your main file with Python. | ||
CMD ["python", "main.py"] | ||
# Setting a port for your app communications with Telegram servers. | ||
|
||
EXPOSE 80/tcp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Implementation of this example bot is deployed and it is found by the telegram handle [@A2SV_Remote_Part_4_Example_bot](https://t.me/A2SV_Remote_Part_4_Example_bot) | ||
|
||
⚠ It is not deployed, but you can run the code on codespaces or localy and use the bot | ||
# Done by: Blen | ||
# Bot Commands implemented: `/add_user` | ||
# : `/all_users` | ||
# : `/get_user_by_id` | ||
# |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import os | ||
from aiogram import Bot, types | ||
|
||
from config import TOKEN_API | ||
# Use the commented code below if you store the token api in .env | ||
# from dotenv import load_dotenv | ||
# load_dotenv() | ||
|
||
# For hosting on pythonanywhere use the following commented code | ||
# from aiogram.client.session.aiohttp import AiohttpSession | ||
# bot = Bot( | ||
# token=TOKEN_API, | ||
# parse_mode='HTML', | ||
# session=AiohttpSession(proxy='http://proxy.server:3128') | ||
# ) | ||
|
||
bot = Bot( | ||
token=TOKEN_API, | ||
parse_mode='HTML' | ||
) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
from aiogram.filters import Command | ||
from aiogram import Router, types | ||
from aiogram.utils.keyboard import InlineKeyboardBuilder | ||
|
||
|
||
callback_router = Router() | ||
|
||
|
||
@callback_router.callback_query(lambda c: c.data.startswith("action_1")) | ||
async def process_callback_respond_to_action1(callback_query: types.CallbackQuery): | ||
await callback_query.message.answer(f"you picked {callback_query.data}") | ||
|
||
@callback_router.callback_query(lambda c: c.data.startswith("action_2")) | ||
async def process_callback_respond_to_action1(callback_query: types.CallbackQuery): | ||
await callback_query.message.answer(f"you picked {callback_query.data}") |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from aiogram.filters import Command | ||
from aiogram import Router, types, F | ||
from aiogram.utils.keyboard import InlineKeyboardBuilder | ||
from datetime import datetime | ||
|
||
from ..keyboards import keyboard | ||
from database.services.user_services import get_users, get_user_by_id, create_user | ||
|
||
message_router = Router() | ||
|
||
@message_router.message(Command('all_users')) | ||
async def all_users(message: types.Message): | ||
try: | ||
users = await get_users() | ||
await message.answer(f"{users}") | ||
except Exception as e: | ||
await message.answer(f"Some error occurred: {e}") | ||
|
||
@message_router.message(Command('add_user')) | ||
async def add_users(message: types.Message): | ||
try: | ||
print("Adding user...") | ||
users = await create_user(int(message.chat.id), name = message.chat.full_name, date= datetime.now()) | ||
print("done") | ||
await message.answer(f"{users}") | ||
except Exception as e: | ||
await message.answer(f"Some error occurred:\n {e}") | ||
|
||
@message_router.message(Command('get_user_by_id')) | ||
async def get_by_id(message: types.Message): | ||
try: | ||
await message.answer("What is the id of the user you are looking for?") | ||
except: | ||
await message.answer("Some error occurred") | ||
|
||
@message_router.message() | ||
async def retrive_user_by_id(message: types.Message): | ||
try: | ||
print("searching user...") | ||
user = await get_user_by_id(int(message.text)) | ||
print("searching complete...") | ||
await message.answer(f"{user}") | ||
except Exception as e: | ||
await message.answer(f"Some error occurred {e}") | ||
|
||
|
||
# Same way you can update and delete users |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
from aiogram.types import ReplyKeyboardMarkup, InlineKeyboardMarkup, KeyboardButton, InlineKeyboardButton | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
TOKEN_API="6712112472:AAFQOvBA5T6-FTD4Cz2XmjFy14NVyPRiY8I" | ||
MONGO_URL="mongodb+srv://A2SVian:[email protected]/?retryWrites=true&w=majority" |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
from aiogram import Bot, Dispatcher | ||
import motor.motor_asyncio | ||
from aiogram.fsm.storage.memory import MemoryStorage | ||
|
||
from config import MONGO_URL | ||
|
||
|
||
|
||
cluster = motor.motor_asyncio.AsyncIOMotorClient(MONGO_URL) | ||
collection = cluster.Blen.myCollection | ||
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from pydantic import BaseModel | ||
|
||
from ..loader import collection | ||
from datetime import datetime | ||
|
||
|
||
class User(BaseModel): | ||
_id: int | ||
name: str | ||
date: datetime | ||
bio: str | ||
phone_number: str | ||
|
||
|
||
|
||
users_collection = collection.user |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from ..models.user_model import User, users_collection | ||
|
||
|
||
async def get_users() -> list[User]: | ||
users = users_collection.find() | ||
return [User(**u) async for u in users] | ||
|
||
|
||
async def get_user_by_id(id: int) -> User or None: | ||
user = await users_collection.find_one({'_id': id}) | ||
return User(**user) if user else None | ||
|
||
|
||
async def create_user(id: int, **kwargs) -> User: | ||
user = await users_collection.insert_one({'_id': id, **kwargs}) | ||
return await get_user_by_id(user.inserted_id) | ||
|
||
|
||
async def update_user(id: int, **kwargs) -> User: | ||
user = await users_collection.find_one_and_update({'_id': id}, {'$set': kwargs}, return_document=True) | ||
return User(**user) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import asyncio | ||
|
||
from aiogram import Dispatcher | ||
|
||
from bot.bot_instance import bot | ||
from bot.handlers.message_handlers import message_router | ||
from bot.callbacks.callback import callback_router | ||
|
||
|
||
def register_routers(dp: Dispatcher) -> None: | ||
"""Registers routers""" | ||
|
||
dp.include_router(message_router) | ||
|
||
# callback routers | ||
dp.include_router(callback_router) | ||
|
||
|
||
|
||
|
||
|
||
async def main() -> None: | ||
"""The main function which will execute our event loop and start polling.""" | ||
try: | ||
dp = Dispatcher() | ||
|
||
print('Bot Starting....') | ||
register_routers(dp) | ||
print("Polling ....") | ||
|
||
await dp.start_polling(bot) | ||
except: | ||
print("Some error occurred") | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
Binary file not shown.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
from aiogram.fsm.state import StatesGroup, State | ||
|
||
class Form(StatesGroup): | ||
name = State() | ||
phone_number = State() | ||
role = State() | ||
photo = State() | ||
bio = State() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you should use your own name here