Skip to content

Instantly share code, notes, and snippets.

@xmichael446
Last active June 20, 2020 12:21
Show Gist options
  • Select an option

  • Save xmichael446/a9af72db4e02f2f74dad745138e6d869 to your computer and use it in GitHub Desktop.

Select an option

Save xmichael446/a9af72db4e02f2f74dad745138e6d869 to your computer and use it in GitHub Desktop.

Revisions

  1. xmichael446 revised this gist Jun 20, 2020. No changes.
  2. xmichael446 revised this gist Jun 20, 2020. 3 changed files with 90 additions and 22 deletions.
    48 changes: 26 additions & 22 deletions bot.py
    Original file line number Diff line number Diff line change
    @@ -1,35 +1,41 @@
    import telebot as tb # pyTelegramBotAPI
    import json # for working with .json files
    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!"

    TOKEN = '1170507053:AAEV4pYWfIerISpthd2ULz9-_36SyeCkPi0' # bot token, tke it from botfather
    bot = tb.TeleBot(TOKEN)

    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']

    @bot.message_handler(commands=['start', 'help']) # if first use of needs help:
    def welcome(message):
    chat_id = message.chat.id
    welcome_data = '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.'
    bot.send_photo(chat_id, photo='https://user-images.githubusercontent.com/64916997/84414095-eba0a880-ac2a-11ea-999a-29364be2ab21.jpg', caption=welcome_data) # greeting.

    @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.

    @bot.message_handler(func=lambda message: True) # in all other cases
    def serve(message):
    chat_id = message.chat.id # predefine chat id

    @dp.message_handler() # in all other cases
    async def serve(message):
    answer = '' # predefine answer
    product = '' # predefine chosen product
    message_text = message.text # predefine text message

    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 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
    @@ -42,20 +48,18 @@ def serve(message):
    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>' + 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:
    bot.send_photo(chat_id, photo=products[product]['image_url'], parse_mode='html', caption=answer) # send photo
    await message.answer_photo(products[product]['image_url'], parse_mode='html', caption=answer) # send photo
    except:
    bot.send_message(chat_id, "Nimadir noto'g'ri ketdi, iltimos qayta urunib ko'ring yoki birozdan keyin urunib ko'ring!") # send error message
    await message.answer(error_message) # send error message
    else:
    answer = 'Afsuski, siz qidirayotgan tovar tugab qolgan yoki bizga hali yetib kelmagan.' # send "not found" message
    bot.send_message(chat_id, answer)

    await message.answer(not_found_message)

    bot.polling(none_stop=True)
    executor.start_polling(dp, skip_updates=True)
    49 changes: 49 additions & 0 deletions products.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,49 @@
    {
    "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"
    }
    ]
    }
    15 changes: 15 additions & 0 deletions requirements.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    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
  3. xmichael446 revised this gist Jun 12, 2020. 1 changed file with 51 additions and 50 deletions.
    101 changes: 51 additions & 50 deletions bot.py
    Original file line number Diff line number Diff line change
    @@ -1,60 +1,61 @@
    import telebot as tb
    import telebot as tb # pyTelegramBotAPI
    import json # for working with .json files

    TOKEN = '1170507053:AAEV4pYWfIerISpthd2ULz9-_36SyeCkPi0'

    TOKEN = '1170507053:AAEV4pYWfIerISpthd2ULz9-_36SyeCkPi0' # bot token, tke it from botfather
    bot = tb.TeleBot(TOKEN)

    products = {
    'arduino nano': {
    'price': 40,
    'available': 10,
    'short_info': 'Arduino Nano - Arduino platalar oilasiga mansub bir kontroller. Atmega328p-u mikrokontrollerida, 8 yoki 16hz chastotada ishlaydi',
    'video_tutorial': 'https://website.org/arduino_nano',
    'image_path': './img/arduino_nano.jpg'
    },
    'arduino uno': {
    'price': 70,
    'available': 1,
    'short_info': 'Arduino Uno - Arduino platalar oilasiga mansub bir kontroller. Atmega328p-u mikrokontrollerida, 16hz chastotada ishlaydi',
    'video_tutorial': 'https://website.org/arduino_uno',
    'image_path': './img/arduino_uno.jpg'
    },
    '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',
    'video_tutorial': 'https://website.org/lcd_display',
    'image_path': './img/lcd_display.jpg'
    },
    'Servo motor': {
    'price': 15,
    'available': 13,
    'short_info': 'Servo motor - bu burilish burchagini sozlash imkoniyatiga ega kichik bir motorcha.',
    'video_tutorial': 'https://website.org/servo_motor',
    'image_path': './img/servo_motor.jpg'
    }
    }

    @bot.message_handler(commands=['start', 'help'])
    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']

    @bot.message_handler(commands=['start', 'help']) # if first use of needs help:
    def welcome(message):
    chat_id = message.chat.id
    welcome_data = 'Assalomu Aleykum! Men Transformer 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.'
    bot.send_photo(chat_id, photo=open('./img/logo.jpg', 'rb'), caption=welcome_data)
    welcome_data = '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.'
    bot.send_photo(chat_id, photo='https://user-images.githubusercontent.com/64916997/84414095-eba0a880-ac2a-11ea-999a-29364be2ab21.jpg', caption=welcome_data) # greeting.


    @bot.message_handler(func=lambda message: True)
    @bot.message_handler(func=lambda message: True) # in all other cases
    def serve(message):
    chat_id = message.chat.id
    answer = ''
    message_text = message.text
    found = False
    for i in products.keys():
    if i == message_text and products[i]['available'] != 0:
    answer = '<b>' + str(message_text).capitalize() + '</b>' + ' mavjud. U haqida qisqacha malumot: \n\n<b>Narxi:</b> ' + str(products[i]['price']) + '000 so\'m' + '\n\nSoni: ' + str(products[i]['available']) + 'ta' + '\n\n<b>Malumot:</b>\n ' + str(products[i]['short_info']) + '\n\n<b>Video darslik:</b> ' + str(products[i]['video_tutorial'])
    found = True
    bot.send_photo(chat_id, photo=open(products[i]['image_path'], 'rb'), parse_mode='html', caption=answer)
    break
    chat_id = message.chat.id # predefine chat id

    answer = '' # predefine answer
    product = '' # predefine chosen product
    message_text = message.text # predefine text message

    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:
    answer = 'Afsuski, siz qidirayotgan tovar tugab qolgan yoki bizga hali yetib kelmagan'
    if not found:
    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:
    bot.send_photo(chat_id, photo=products[product]['image_url'], parse_mode='html', caption=answer) # send photo
    except:
    bot.send_message(chat_id, "Nimadir noto'g'ri ketdi, iltimos qayta urunib ko'ring yoki birozdan keyin urunib ko'ring!") # send error message
    else:
    answer = 'Afsuski, siz qidirayotgan tovar tugab qolgan yoki bizga hali yetib kelmagan.' # send "not found" message
    bot.send_message(chat_id, answer)

    bot.polling(none_stop=True)

    bot.polling(none_stop=True)
  4. xmichael446 created this gist Jun 8, 2020.
    60 changes: 60 additions & 0 deletions bot.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,60 @@
    import telebot as tb

    TOKEN = '1170507053:AAEV4pYWfIerISpthd2ULz9-_36SyeCkPi0'
    bot = tb.TeleBot(TOKEN)

    products = {
    'arduino nano': {
    'price': 40,
    'available': 10,
    'short_info': 'Arduino Nano - Arduino platalar oilasiga mansub bir kontroller. Atmega328p-u mikrokontrollerida, 8 yoki 16hz chastotada ishlaydi',
    'video_tutorial': 'https://website.org/arduino_nano',
    'image_path': './img/arduino_nano.jpg'
    },
    'arduino uno': {
    'price': 70,
    'available': 1,
    'short_info': 'Arduino Uno - Arduino platalar oilasiga mansub bir kontroller. Atmega328p-u mikrokontrollerida, 16hz chastotada ishlaydi',
    'video_tutorial': 'https://website.org/arduino_uno',
    'image_path': './img/arduino_uno.jpg'
    },
    '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',
    'video_tutorial': 'https://website.org/lcd_display',
    'image_path': './img/lcd_display.jpg'
    },
    'Servo motor': {
    'price': 15,
    'available': 13,
    'short_info': 'Servo motor - bu burilish burchagini sozlash imkoniyatiga ega kichik bir motorcha.',
    'video_tutorial': 'https://website.org/servo_motor',
    'image_path': './img/servo_motor.jpg'
    }
    }

    @bot.message_handler(commands=['start', 'help'])
    def welcome(message):
    chat_id = message.chat.id
    welcome_data = 'Assalomu Aleykum! Men Transformer 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.'
    bot.send_photo(chat_id, photo=open('./img/logo.jpg', 'rb'), caption=welcome_data)

    @bot.message_handler(func=lambda message: True)
    def serve(message):
    chat_id = message.chat.id
    answer = ''
    message_text = message.text
    found = False
    for i in products.keys():
    if i == message_text and products[i]['available'] != 0:
    answer = '<b>' + str(message_text).capitalize() + '</b>' + ' mavjud. U haqida qisqacha malumot: \n\n<b>Narxi:</b> ' + str(products[i]['price']) + '000 so\'m' + '\n\nSoni: ' + str(products[i]['available']) + 'ta' + '\n\n<b>Malumot:</b>\n ' + str(products[i]['short_info']) + '\n\n<b>Video darslik:</b> ' + str(products[i]['video_tutorial'])
    found = True
    bot.send_photo(chat_id, photo=open(products[i]['image_path'], 'rb'), parse_mode='html', caption=answer)
    break
    else:
    answer = 'Afsuski, siz qidirayotgan tovar tugab qolgan yoki bizga hali yetib kelmagan'
    if not found:
    bot.send_message(chat_id, answer)

    bot.polling(none_stop=True)