Last active
June 20, 2020 12:21
-
-
Save xmichael446/a9af72db4e02f2f74dad745138e6d869 to your computer and use it in GitHub Desktop.
Transformers Education Bot
This file contains hidden or 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
import json | |
import logging | |
from aiogram import Bot, Dispatcher, executor, types | |
TOKEN = '1170507053:AAEV4pYWfIerISpthd2ULz9-_36SyeCkPi0' | |
logging.basicConfig(level=logging.INFO) | |
bot = Bot(token=TOKEN) | |
dp = Dispatcher(bot) | |
# messages: | |
welcome_message = 'Assalomu Aleykum' + '! Men Transformers Education o\'quv markazining hizmat botiman. Men sizga har hil datchiklar haqida malumot,narx-navo va o\'quv manbalar berishim mumkin. Shunchaki biror datchik, modul yoki plata nomini kiriting.' | |
not_found_message = 'Afsuski, siz qidirayotgan tovar tugab qolgan yoki bizga hali yetib kelmagan.' | |
error_message = "Nimadir noto'g'ri ketdi, iltimos qayta urunib ko'ring yoki birozdan keyin urunib ko'ring!" | |
file_path = "products.json" # path to .json product list | |
with open(file_path, "r") as read_file: | |
data = json.load(read_file) | |
products = data['products'] | |
@dp.message_handler(commands=['start', 'help']) # if first use or needs help: | |
async def welcome(message): | |
await message.reply_photo('https://user-images.githubusercontent.com/64916997/84414095-eba0a880-ac2a-11ea-999a-29364be2ab21.jpg', parse_mode='html', caption=welcome_message) # greeting. | |
@dp.message_handler() # in all other cases | |
async def serve(message): | |
answer = '' # predefine answer | |
product = '' # predefine chosen product | |
found = False # product not found yet | |
for i in range(len(products)): | |
if products[i]['name'] in [message.text, message.text.upper(), message.text.lower(), message.text.capitalize] and products[i]['available'] != 0: | |
# if message text in products list: | |
found = True # the product is now found | |
product = i # choose that product | |
break # brek the loop | |
else: | |
found = False | |
continue # else, continue searching | |
if found is True: # if product is found | |
# create an HTML markup for the answer | |
answer = '<b>' + message.text.capitalize() + '</b>' + ' mavjud. U haqida:\n\n' | |
answer += '<b>' + 'ID: ' + '</b>' + products[product]['id'] + '\n\n' | |
answer += '<b>' + 'Narxi: ' + '</b>' + str(products[product]['price']) + '000so\'m.\n\n' | |
answer += '<b>' + 'Soni: ' + '</b>' + str(products[product]['available']) + 'ta qolgan.\n\n' | |
answer += '<b>' + 'Ma\'lumot: ' + '</b>' + products[product]['short_info'] + '.\n\n' | |
answer += '<b>' + 'To\'liq ma\'lumot ' + '</b>' + '<a href="' + products[i]['entire_info'] + '">bu yerda</a>' + '.' | |
try: | |
await message.answer_photo(products[product]['image_url'], parse_mode='html', caption=answer) # send photo | |
except: | |
await message.answer(error_message) # send error message | |
else: | |
await message.answer(not_found_message) | |
executor.start_polling(dp, skip_updates=True) |
This file contains hidden or 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
{ | |
"products": [ | |
{ | |
"id": "A0", | |
"name": "arduino nano", | |
"price": 40, | |
"available": 10, | |
"short_info": "Arduino Nano - Arduino platalar oilasiga mansub bir kontroller. Atmega328p-u mikrokontrollerida, 8 yoki 16hz chastotada ishlaydi", | |
"entire_info": "https://website.org/arduino_nano", | |
"image_url": "https://user-images.githubusercontent.com/64916997/84414070-e6435e00-ac2a-11ea-97e2-da4d3b0cf81a.jpg" | |
}, | |
{ | |
"id": "A1", | |
"name": "arduino uno", | |
"price": 70, | |
"available": 2, | |
"short_info": "Arduino Uno - Arduino platalar oilasiga mansub bir kontroller. Atmega328p-u mikrokontrollerida, 16hz chastotada ishlaydi", | |
"entire_info": "https://website.org/arduino_uno", | |
"image_url": "https://user-images.githubusercontent.com/64916997/84414072-e6dbf480-ac2a-11ea-9ef5-0759d617e141.jpg" | |
}, | |
{ | |
"id": "B0", | |
"name": "lcd display", | |
"price": 20, | |
"available": 16, | |
"short_info": "LCD display - bu kichik bir ekrancha, u yordamida har hil matn malumot, yoki binary media chiqarishingiz mumkin", | |
"entire_info": "https://website.org/lcd_display", | |
"image_url": "https://user-images.githubusercontent.com/64916997/84414084-e8a5b800-ac2a-11ea-95bf-659ec38f44ff.jpg" | |
}, | |
{ | |
"id": "C0", | |
"name": "servo motor", | |
"price": 15, | |
"available": 13, | |
"short_info": "Servo motor - bu burilish burchagini sozlash imkoniyatiga ega kichik bir motorcha", | |
"entire_info": "https://website.org/servo_motor", | |
"image_url": "https://user-images.githubusercontent.com/64916997/84414099-ed6a6c00-ac2a-11ea-935e-9e0fa8620fe7.jpg" | |
}, | |
{ | |
"id": "C1", | |
"name": "stepper motor", | |
"price": 18, | |
"available": 7, | |
"short_info": "Siz bergan qadam boyicha aylanadigan motor", | |
"entire_info": "https://website.org/stepper_motor", | |
"image_url": "https://user-images.githubusercontent.com/64916997/84426878-607cde00-ac3d-11ea-9a7d-e6bde126a8c7.png" | |
} | |
] | |
} |
This file contains hidden or 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
aiogram==2.9.2 | |
aiohttp==3.6.2 | |
async-timeout==3.0.1 | |
attrs==19.3.0 | |
Babel==2.8.0 | |
certifi==2020.4.5.2 | |
chardet==3.0.4 | |
idna==2.9 | |
multidict==4.7.6 | |
pkg-resources==0.0.0 | |
pytz==2020.1 | |
requests==2.23.0 | |
six==1.15.0 | |
urllib3==1.25.9 | |
yarl==1.4.2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment