راکت

ایمپورت شدن ماژول InlineKeyboardButton از کتابخانه

3 پاسخ

وقتی اماژول InlineKeyboardButton از کتابخانه تلگرام رو اد میکنم موقع اجرا خطا میگیره..
کسی هست با پایتون برا تلگرام بات نوشته باشه؟ راهنمایی کنه ممنون

ثبت پرسش جدید

پاسخ‌ها

3
علیرضا معمارزاده
علیرضا معمارزاده
@alireza.mzh

تخصص: junior level developer

سلام خدمت شما
کدهایی که نوشتید و همچنین متن خطایی که میده رو به شکلی که در زیر توضیح داده شده قرار بدید.
https://roocket.ir/faq

mohamadamirnabavi
mohamadamirnabavi
@mohamadamirnabavi

سلام دست عزیز
@alireza.mzh
' # * coding:utf-8 *
import os
import logging
import hashlib

TOKEN = "329254099:AAG3Rc8J39ScfOZYbWJ_Y60e7veTDYWwGRE"

import pyqrcode
import telegram
from telegram import InlineKeyboardButton
from telegram import InlineKeyboardMarkup
from telegram.ext import Updater, CommandHandler, MessageHandler, Filters, \
ConversationHandler, CallbackQueryHandler

----------------------------------------------------------- show logging ----

logging.basicConfig(format='%(asctime)s\t%(levelname)s\t%(message)s',
level=logging.INFO)
logging.info('bot started')

Constants -------------------------------------------------------------------

GO_CreateQR, GO_ReadQR, GO_recieve_qr1 = range(3)

Messages --------------------------------------------------------------------

msg1 = 'به ربات ساخت کد کیو-آر خوش آمدید'
msg2 = 'لطفا متن خود را وارد کنید:' + '\n\n' + \
'برای لغو عملیات از دستور زیر استفاده کنید.' + '\n' + \
'/cancel'
msg3 = 'این کد کد کیو-آر شما میباشد.' \
' برای انتقال پول از این کد کیو-آر استفاده خواهد شد' \
' لذا آن را در دستگاه خود ذخیره کنید.'

Buttons ---------------------------------------------------------------------

btn0 = '/Home'
btn1 = '/CreateQR'
btn2 = '/About'
btn3 = '/GiveMyQR'

***** start *

def start(bot, update):
uid = update.effective_user.id
reply_keyboard = [[btn1], [btn2], [btn3]]
reply_markup = telegram.ReplyKeyboardMarkup(reply_keyboard,
one_time_keyboard=True)
bot.send_message(chat_id=uid, text=msg1, reply_markup=reply_markup)

return ConversationHandler.END

*** get_qr_info *

def get_qr_info(bot, update):
"""receive data from user to create QR"""
uid = update.effective_user.id
bot.send_message(chat_id=uid, text=msg2)

return GO_CreateQR

***** create_qr *

def create_qr(bot, update):
"""create qr code and sent it to user"""
uid = update.effective_user.id
qr_info = update.message.text
scale = 10

# creates qr and saves it
qr = pyqrcode.create(qr_info)
qr.png('QR/QR_{}.png'.format(uid), scale=scale)

return hash_qr_file(bot, update)

def hash_qr_file(bot, update):
uid = update.effective_user.id
uname = update['message']['chat']['first_name']
hasher = hashlib.new('sha256')

with open('QR/QR_{}.png'.format(uid), 'rb') as f:
    contents = f.read()
    hasher.update(contents)

reply_keyboard = [[btn3], [btn0]]
reply_markup = telegram.ReplyKeyboardMarkup(reply_keyboard,
                                            one_time_keyboard=True)
bot.send_message(chat_id=uid,
                 text='تصویر کیو-آر شما با موفقیت ساخته شد!',
                 reply_markup=reply_markup)

logging.info('{:<15}{:<15} qr created!'.format(uid, uname))

return ConversationHandler.END

*** send_qr *

def send_qr(bot, update):
uid = update.effective_user.id

exists = os.path.isfile('QR/QR_{}.png'.format(uid))
if exists:
    # read qr pic and send it to user
    qr_pic = open('QR/QR_{}.png'.format(uid), 'rb')
    bot.send_photo(chat_id=uid, photo=qr_pic)

    # show kb
    reply_keyboard = [[btn0]]
    reply_markup = telegram.ReplyKeyboardMarkup(reply_keyboard,
                                                one_time_keyboard=True)
    bot.send_message(chat_id=uid, text=msg3, reply_markup=reply_markup)
else:
    bot.send_message(chat_id=uid,
                     text='شما تصویر کیو-آر ندارید!')

bot.send_message(chat_id=uid,
                 text='برگشت به خانه'
                      '\n/start')
return ConversationHandler.END

***** about *

def about(bot, update):
uid = update.effective_user.id
keyboard = [[InlineKeyboardButton("اینستاگرام Phika",
url='www.instagram.com/Phika.ir')]
]
reply_markup = InlineKeyboardMarkup(keyboard)
bot.send_message(chat_id=update.effective_user.id,
text='Our Instagram Page',
reply_markup=reply_markup)

return start(bot, update)

***** cancel ****

def cancel(bot, update):
"""cancel function"""
reply_keyboard = [[btn0]]
reply_markup = telegram.ReplyKeyboardMarkup(reply_keyboard,
one_time_keyboard=True)
bot.send_message(chat_id=update.effective_user.id,
text='شما عملیات را با موفقیت لغو کردید!' + '\n\n' +
'برای برگشت به منو اصلی'
' میتوانید از دکمه Home استفاده کنید.',
reply_markup=reply_markup)
return ConversationHandler.END

***** error *

def error(update, error_):
uid = update.effective_user.id
uname = update['message']['chat']['first_name']

logging.warning('{:<15}{:<15} qr created!'.format(uid, uname))

***** main **

def main():
"""main function"""
updater = Updater(TOKEN)
dp = updater.dispatcher

updater.dispatcher.add_handler(CallbackQueryHandler(cancel))

conv_handler = ConversationHandler(
    entry_points=[CommandHandler('start', start),
                  CommandHandler('home', start),
                  CommandHandler('CreateQR', get_qr_info),
                  CommandHandler('GiveMyQR', send_qr),
                  CommandHandler('About', about)],
    states={
        GO_CreateQR: [MessageHandler(Filters.text, create_qr)],
    },
    fallbacks=[CommandHandler('cancel', cancel)]
)

dp.add_handler(conv_handler)
dp.add_error_handler(error)
updater.start_polling()
updater.idle()

if name == 'main':
main()
'
خطایی هم که میده اینه
'
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Program Files\JetBrains\PyCharm 2019.2.3\helpers\pydev_pydev_bundle\pydev_umd.py", line 197, in runfile
pydev_imports.execfile(filename, global_vars, local_vars) # execute the script
File "C:\Program Files\JetBrains\PyCharm 2019.2.3\helpers\pydev_pydev_imps_pydev_execfile.py", line 18, in execfile
exec(compile(contents+"\n", file, 'exec'), glob, loc)
File "C:/python program/telegram/qr.py", line 10, in <module>
from telegram import InlineKeyboardButton
ImportError: cannot import name 'InlineKeyboardButton'
'

شاید پاسخ شما همین‌جا باشد

علیرضا معمارزاده
علیرضا معمارزاده
@alireza.mzh

تخصص: junior level developer

برای ارسال پاسخ لازم است وارد شده یا ثبت‌نام کنید

ورود یا ثبت‌نام