Skip to content
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
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions Part_4/Yonathan/Dockerfile
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
8 changes: 8 additions & 0 deletions Part_4/Yonathan/Readme.md
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 added Part_4/Yonathan/__init__.py
Empty file.
Empty file added Part_4/Yonathan/bot/__init__.py
Empty file.
20 changes: 20 additions & 0 deletions Part_4/Yonathan/bot/bot_instance.py
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.
15 changes: 15 additions & 0 deletions Part_4/Yonathan/bot/callbacks/callback.py
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.
47 changes: 47 additions & 0 deletions Part_4/Yonathan/bot/handlers/message_handlers.py
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.
4 changes: 4 additions & 0 deletions Part_4/Yonathan/bot/keyboards/keyboard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

from aiogram.types import ReplyKeyboardMarkup, InlineKeyboardMarkup, KeyboardButton, InlineKeyboardButton


2 changes: 2 additions & 0 deletions Part_4/Yonathan/config.py
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.
10 changes: 10 additions & 0 deletions Part_4/Yonathan/database/loader.py
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
Copy link
Collaborator

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

Empty file.
16 changes: 16 additions & 0 deletions Part_4/Yonathan/database/models/user_model.py
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.
21 changes: 21 additions & 0 deletions Part_4/Yonathan/database/services/user_services.py
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)
37 changes: 37 additions & 0 deletions Part_4/Yonathan/main.py
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 added Part_4/Yonathan/requirements.txt
Binary file not shown.
Empty file.
8 changes: 8 additions & 0 deletions Part_4/Yonathan/utils/state.py
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()