DEV Community

Cover image for Building An E-commerce Telegram Bot Using Python and Fauna.

Building An E-commerce Telegram Bot Using Python and Fauna.

Curious Paul on June 13, 2021

This article will show you how to build a telegram bot for a small-scale business owner. This bot will be pretty similar to the popular platform Wh...
Collapse
 
shukkkur profile image
Shukur Sabzaliev

@curiouspaul1 Amazing job, really! I was wondering about further development. Do you have any post I could look into?

Collapse
 
curiouspaul1 profile image
Curious Paul • Edited

thanks for the kind words,
any post regarding what would you like me to help you with

Collapse
 
shukkkur profile image
Shukur Sabzaliev • Edited

@curiouspaul1 I tried to modify and use your Bot in my university so students can sell and buy things.
So, a few things I wish you could show/teach:
Divide posts/ads into categories but not businesses.
Also, I noticed that when fetching posts/ads from the databases sometimes it fails because, perhaps the user added unnecessary space or capitalization error (i tried to put a bunch of lower().strip() everywhere)
In addition, it would be amazing if SME and Customer both had the same functionality (both can buy and sell).
And I kinda failed to add Back buttons everywhere, but managed to add Exit button (just used you /cancel function)
Lastly, some buttons make require to be pressed two times, do you know why?

The bot: @ucaStudentStore_bot
Code link - (removing the link since you joined)

Thread Thread
 
curiouspaul1 profile image
Curious Paul

hmm, thanks a lot for pointing all of these out, I will work on them and once an upate is ready, i'll let you know

Thread Thread
 
curiouspaul1 profile image
Curious Paul

you're also welcome to join me in implementing these, i see you;ve forked the repository already. Btw there's an option to filter post/ads by category already.

Thread Thread
 
curiouspaul1 profile image
Curious Paul

here..

Thread Thread
 
shukkkur profile image
Shukur Sabzaliev

Hi! Thank you for responding! But I your message just says "here.." and that is? Is there a link or something?

Thread Thread
 
shukkkur profile image
Shukur Sabzaliev

I opened a issue in GitHub, in telegram-business repository please, check it. I need your help

Thread Thread
 
curiouspaul1 profile image
Curious Paul

oh theres supposed to be an image

Thread Thread
 
curiouspaul1 profile image
Curious Paul

alright great!

Collapse
 
maximpylnyk profile image
Maximpylnyk • Edited

Hello. I rewrote your main code and when I run the code, it says the following:
& C: /Users/maxpy/AppData/Local/Programs/Python/Python39/python.exe
d: /Plmenno/main.py
File "", line 1
& C: /Users/maxpy/AppData/Local/Programs/Python/Python39/python.exe
^ d: /Plmenno/main.py

SyntaxError: invalid syntax.
What should I do?

Collapse
 
curiouspaul1 profile image
Curious Paul

Hi thanks for stopping by, could you share your code on github with me so i can give it a look?

Collapse
 
maximpylnyk profile image
Maximpylnyk

you will help me?

Thread Thread
 
curiouspaul1 profile image
Curious Paul

yes sure sure, do you think we can schedule a google meets call and you can show me whats wrong on the call, send me an email: Paulcurious7@gmail.com

Collapse
 
maximpylnyk profile image
Maximpylnyk
Collapse
 
maximpylnyk profile image
Maximpylnyk

Ok

Collapse
 
alvaaz profile image
Álvaro Göede Rivera

Hi! I have problem when I try upload an image using Cloudinary, I don't know what happen. This is my code:

def add_media(update, context):
  send_photo = upload('http://res.cloudinary.com/demo/image/upload/couple.jpg')
Enter fullscreen mode Exit fullscreen mode

The console show:

2021-07-15 14:01:44,494 - telegram.ext.dispatcher - ERROR - No error handlers are registered, logging exception.
Traceback (most recent call last):
  File "/Users/alvarogoederivera/web/python_bot/lib/python3.8/site-packages/telegram/ext/dispatcher.py", line 555, in process_update
    handler.handle_update(update, self, check, context)
  File "/Users/alvarogoederivera/web/python_bot/lib/python3.8/site-packages/telegram/ext/conversationhandler.py", line 626, in handle_update
    new_state = handler.handle_update(update, dispatcher, check_result, context)
  File "/Users/alvarogoederivera/web/python_bot/lib/python3.8/site-packages/telegram/ext/handler.py", line 198, in handle_update
    return self.callback(update, context)
  File "/Users/alvarogoederivera/web/python_bot/handlers.py", line 64, in add_media
    send_photo = upload('http://res.cloudinary.com/demo/image/upload/couple.jpg')
  File "/Users/alvarogoederivera/web/python_bot/lib/python3.8/site-packages/cloudinary/uploader.py", line 46, in upload
    return call_cacheable_api("upload", params, file=file, **options)
  File "/Users/alvarogoederivera/web/python_bot/lib/python3.8/site-packages/cloudinary/uploader.py", line 394, in call_cacheable_api
    result = call_api(action, params, http_headers, return_error, unsigned, file, timeout, **options)
  File "/Users/alvarogoederivera/web/python_bot/lib/python3.8/site-packages/cloudinary/uploader.py", line 459, in call_api
    response = _http.request("POST", api_url, param_list, headers, **kw)
  File "/Users/alvarogoederivera/web/python_bot/lib/python3.8/site-packages/urllib3/request.py", line 78, in request
    return self.request_encode_body(
  File "/Users/alvarogoederivera/web/python_bot/lib/python3.8/site-packages/urllib3/request.py", line 155, in request_encode_body
    body, content_type = encode_multipart_formdata(
  File "/Users/alvarogoederivera/web/python_bot/lib/python3.8/site-packages/urllib3/filepost.py", line 78, in encode_multipart_formdata
    for field in iter_field_objects(fields):
  File "/Users/alvarogoederivera/web/python_bot/lib/python3.8/site-packages/urllib3/filepost.py", line 42, in iter_field_objects
    yield RequestField.from_tuples(*field)
  File "/Users/alvarogoederivera/web/python_bot/lib/python3.8/site-packages/urllib3/fields.py", line 181, in from_tuples
    filename, data = value
ValueError: not enough values to unpack (expected 2, got 1)
Enter fullscreen mode Exit fullscreen mode

Does anyone have any idea what is going on?

Collapse
 
curiouspaul1 profile image
Curious Paul

Hi, this doesn't seem like the code i wrote in the demo, for this article, however this doesn't look like the code to upload an image to cloudinary looks like. You might want to check the docs or use the one i wrote on the product_info method in handlers.py from the tutorial.

Collapse
 
alvaaz profile image
Álvaro Göede Rivera • Edited

I get the same problem when doing the demo, that's why I simplified it.

def add_media(update: Update, context: CallbackContext):
  logger.info(f"El usuario {update.effective_user['username']}, subió foto")
  quote = context.user_data["new_quote"]
  photo = context.bot.getFile(update.message.photo[-1].file_id)
  file_ = open("product_image", "wb")
  photo.download(out=file_)
  send_photo = upload("product_image", width=200, height=150, crop='thumb')
Enter fullscreen mode Exit fullscreen mode
dp.add_handler(ConversationHandler(
  entry_points = [CommandHandler("new", handlers.start)],
  states={
    handlers.QUOTE: [
      MessageHandler(
          Filters.all, handlers.add_quote
      )
    ],
    handlers.MEDIA: [
      MessageHandler(Filters.photo, handlers.add_media)
    ]
  },
  fallbacks=[CommandHandler('cancel', handlers.cancel)],
  allow_reentry=True)
)
Enter fullscreen mode Exit fullscreen mode

The console show the same error:

ValueError: not enough values to unpack (expected 2, got 1)
Enter fullscreen mode Exit fullscreen mode

I know the error is upload because when I remove it the error does not appear.

Thread Thread
 
curiouspaul1 profile image
Curious Paul

I see, do you add caption to the image when you test it though.?

Thread Thread
 
alvaaz profile image
Álvaro Göede Rivera

Could you check my code and see if there is something I am doing wrong?

github.com/alvaaz/telegram_bot

I would really appreciate it

Thread Thread
 
curiouspaul1 profile image
Curious Paul

oh great!, thanks for making this easier I'll take a look

Collapse
 
pradiepp profile image
pradiepp

Cant seems to solve this error while running the Main.py


Traceback (most recent call last):
File "C:\Users\ACER\Desktop\Bot\Bot\main.py", line 35, in
main()
File "C:\Users\ACER\Desktop\Bot\Bot\main.py", line 16, in main
entry_points=[CommandHandler('start', handlers.start)],
NameError: name 'handlers' is not defined. Did you mean: 'handler'?

Collapse
 
curiouspaul1 profile image
Curious Paul

did you rename the "handlers.py" file

Collapse
 
pradiepp profile image
pradiepp

No, its 'handlers.py' only.

Collapse
 
prodplug profile image
prodplug

File "\main.py", line 9, in
updater = Updater(token=TOKEN, use_context=True)
File "\updater.py", line 237, in init
raise ValueError('token or bot must be passed')
ValueError: token or bot must be passed

any way to fix this (removed directory for privacy purpose)

this is when i enter python main.py

Collapse
 
curiouspaul1 profile image
Curious Paul

You have to replace "TOKEN" with the actual token that you got from the telegram Bot-father and use that instead, i just put "TOKEN" as a placeholder

Collapse
 
balighmehrez profile image
BalighMehrez

Awesome

Just wanted to notify you forgot to define the market on choose method
reply_markup=markup

Collapse
 
curiouspaul1 profile image
Curious Paul

Oh thanks a lot man..i just noticed that

Collapse
 
niketoz3 profile image
Niketoz

========== RESTART: C:\Users\User\Desktop\botpol\botpol_env\main.py ==========
Traceback (most recent call last):
File "C:\Users\User\Desktop\botpol\botpol_env\main.py", line 1, in
import handlers
File "C:\Users\User\Desktop\botpol\botpol_env\handlers.py", line 1, in
from telegram import (
ModuleNotFoundError: No module named 'telegram'

Iam really stuck.

Collapse
 
codegien profile image
codegien

Awesome

Collapse
 
curiouspaul1 profile image
Curious Paul

Thanks a lot boss

Collapse
 
dhruv13723 profile image
Dhruv

I have copied the exact code still I am getting this error. please help me
This is the error: dev-to-uploads.s3.amazonaws.com/up...