- :
import telebot
from telebot.types import LabeledPrice
import re
import yandex
from send_email import send_mail
- token :
token = '*********:**********************************'
bot = telebot.TeleBot(token)
- :
@bot.message_handler(commands=['start', 'donation'])
def send_welcom(message):
if message.text == '/start':
keyboard = telebot.types.InlineKeyboardMarkup()
keyboard.row(telebot.types.InlineKeyboardButton(' ', callback_data='bd_get'))
bot.send_message(message.chat.id, ' : ', reply_markup=keyboard)
if message.text == '/donation':
bot.send_invoice(message.chat.id,
title='Donation',
description=' .',
invoice_payload='donation',
provider_token='*********:TEST:*******',
currency='RUB',
prices=[LabeledPrice(label='Donation', amount=10000)],
start_parameter='pay_start',
photo_url='https://cdn.imgbin.com/22/0/5/imgbin-donation-computer-icons-'
'fundraising-justgiving-charitable-organization-donation-'
'5Yehm9UecF2cRWrqtms4e6emn.jpg',
photo_height=512,
photo_width=512,
photo_size=512,
is_flexible=False)
IF /start, IF /donation.
invoice_payload , .
bot.send_invoice token . , .
token . , . token.
: 123:TEST:XXXX
: 123:LIVE:XXXX
- :
@bot.pre_checkout_query_handler(func=lambda query: True)
def checkout(message):
bot.answer_pre_checkout_query(message.id, ok=True,
error_message=" CVV , "
" . "
" , "
" .")
.. digital , , ok True. .
, , . , , . , ok=True False , error_message.
- :
@bot.message_handler(content_types=['successful_payment'])
def got_payment(message):
if message.json['successful_payment']['invoice_payload'].split(',')[0] == 'buy':
email = message.json['successful_payment']['order_info']['email']
bot.send_message(message.chat.id,
'! : `{} {}`.\n'
' (`{}`) .\n\n' \
' @_'\
.format(message.successful_payment.total_amount / 100,
message.successful_payment.currency,
email),
parse_mode='Markdown')
request_text = message.json['successful_payment']['invoice_payload'].split(',')[1]
city = message.json['successful_payment']['invoice_payload'].split(',')[2]
write_in_BD, name_excel_BD = yandex.write_exl(text=request_text, city_name=city)
if write_in_BD == True:
send_mail(name_file=name_excel_BD, to_address=email)
elif message.json['successful_payment']['invoice_payload'] == 'donation':
bot.send_video(message.chat.id, 'https://media0.giphy.com/media/QAsBwSjx9zVKoGp9nr/giphy.gif')
- InlineKeyboardMarkup:
@bot.callback_query_handler(func=lambda call: True)
def callback_key(message):
if message.data == 'bd_get':
input_city(message)
elif re.search(r'bd_yes/',message.data):
location = re.sub('bd_yes/','',message.data)
bot.answer_callback_query(message.id, text=location, show_alert=False)
input_text(message, city=location)
elif message.data == 'bd_no':
input_city(message)
elif re.search(r'pay', message.data):
info_get_bd_limit = re.split(r'/',message.data)
sent_text = info_get_bd_limit[1]
sent_city = info_get_bd_limit[2]
found = info_get_bd_limit[3]
bot.answer_callback_query(message.id, text='', show_alert=False)
pay(message, text=sent_text, city=sent_city, found=found)
- :
def pay(message, text, city, found):
bot.send_invoice(message.from_user.id,
title=' ',
description=' .\n'
': '+text+'\n: '+city+'\n- : '+found,
invoice_payload='buy,{},{}'.format(text, city),
provider_token='*********:TEST:*******',
currency='RUB',
prices=[LabeledPrice(label=' ', amount=20000)],
start_parameter='pay_start',
photo_url='https://encrypted-tbn0.gstatic.com/images?q=tbn:'
'ANd9GcRVUs3eGt4U9YSXZrsbOkJoNEdpcYUdq0vEzM-ci_oIxEWs1FK0',
photo_height=300,
photo_width=300,
photo_size=300,
need_email=True,
is_flexible=False)
need_email , , .
- , :
def input_city(message):
bot.send_message(message.from_user.id, ' :')
bot.register_next_step_handler_by_chat_id(message.from_user.id, get_city)
bot.answer_callback_query(message.id, text=' ', show_alert=False)
- , :
def get_city(message):
get_location, get_location_point = yandex.get_location(message.text)
keyboard = telebot.types.InlineKeyboardMarkup()
keyboard.row(telebot.types.InlineKeyboardButton('', callback_data='bd_yes/'+message.text),
telebot.types.InlineKeyboardButton('', callback_data='bd_no'))
bot.send_location(message.chat.id, get_location_point[1], get_location_point[0], reply_markup=keyboard)
- , :
def input_text(message, city=None):
global location_city
location_city = city
info_message_id = bot.send_message(message.from_user.id, ' ( ): ')
bot.register_next_step_handler_by_chat_id(message.from_user.id, get_bd_limit)
- - :
def get_bd_limit(message):
information = yandex.get_information_limit(message.text, location_city)
if information:
found = yandex.sum_taken_object(yandex.get_all_infomations(message.text, location_city))
bot.send_message(message.from_user.id, ' .\n'
' 5 .')
i = 1
text = ''
for key, value in information.items():
name = value['name']
address = value['address']
url = value['url']
phones = value['phones']
hours = value['hours']
text = text + '' + str(i) + ') : '+name+'\n: '+address+'\n: '+url+\
'\n: '+phones+'\n : '+hours+'\n----------\n'
i += 1
bot.send_message(message.from_user.id, text, disable_web_page_preview=True)
keyboard = telebot.types.InlineKeyboardMarkup()
keyboard.row(telebot.types.InlineKeyboardButton(' 200 .',
callback_data='pay/' + message.text+'/'+location_city+'/'+found))
bot.send_message(message.from_user.id, ' : ' + found+''
'\n\n .', reply_markup=keyboard)
else:
bot.send_message(message.from_user.id, ' !')
- Telegram:
while True:
try:
bot.polling(none_stop=True)
except Exception as e:
time.sleep(15)