Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
- import ephem
- phrases={"привет":"привет","как дела":"хорошо,а у тебя","хорошо":"ну и здорово","плохо":"взбодрись"}
- numbers_in_russian={'один':1,'два':2,'три':3,
- 'четыре':4,'пять':5,'шесть':6,'семь':7,'восемь':8,
- 'девять':9,'десять':10,
- 'умножить_на':'*','разделить_на':'/','плюс':'+','минус':'-'}
- incorrect='?.,!'
- def get_answer(key,phrases):
- for symbol in incorrect:
- key=key.replace(symbol,"")
- key=key.strip()
- return phrases.get(key.lower(), "")
- def start(bot,update):
- print("Вызван/start")
- bot.sendMessage(update.message.chat_id, text='Привет.')
- def bot_help(bot,update):
- print("Помощь пользователю/help")
- bot.sendMessage(update.message.chat_id, text="Сообщения ,на которые отвечает бот : привет, как дела, хорошо, плохо")
- def talk_to_me(bot,update):
- print('Получено сообщение: %s' % update.message.text)
- bot.sendMessage(update.message.chat_id, text=get_answer(update.message.text, phrases))
- def words_count(bot,update):
- print('Получено сообщение: %s' % update.message.text)
- words_list=update.message.text.split()
- words=len(words_list)-1
- bot.sendMessage(update.message.chat_id, text=str(words))
- def calculate(bot,update):
- print('Получено сообщение: %s' % update.message.text)
- nums_for_calculation=update.message.text.strip().split()
- if "*" in nums_for_calculation:
- result=int(nums_for_calculation[1])* int(nums_for_calculation[3])
- bot.sendMessage(update.message.chat_id, text=int(result))
- if "+" in nums_for_calculation:
- result=int(nums_for_calculation[1])+ int(nums_for_calculation[3])
- bot.sendMessage(update.message.chat_id, text=int(result))
- if "-" in nums_for_calculation:
- result=int(nums_for_calculation[1])- int(nums_for_calculation[3])
- bot.sendMessage(update.message.chat_id, text=int(result))
- if "/" in nums_for_calculation:
- if int(nums_for_calculation[3])==0:
- bot.sendMessage(update.message.chat_id, text='Нельзя делить на 0')
- else:
- result=int(nums_for_calculation[1])/ int(nums_for_calculation[3])
- bot.sendMessage(update.message.chat_id, text=int(result))
- def text_calculation(bot,update):
- print('Получено сообщение: %s' % update.message.text)
- text_calculation_list=update.message.text.split()
- #bot.sendMessage(update.message.chat_id, text="/".join(text_calculation_list))
- first_num=numbers_in_russian.get(text_calculation_list[1])
- operation=numbers_in_russian.get(text_calculation_list[2])
- second_num=numbers_in_russian.get(text_calculation_list[3])
- if "+" in operation:
- result=int(first_num)+int(second_num)
- bot.sendMessage(update.message.chat_id, text=int(result))
- if "-" in operation:
- result=int(first_num)-int(second_num)
- bot.sendMessage(update.message.chat_id, text=int(result))
- if "*" in operation:
- result=int(first_num)*int(second_num)
- bot.sendMessage(update.message.chat_id, text=int(result))
- if "/" in operation:
- result=int(first_num)/int(second_num)
- bot.sendMessage(update.message.chat_id, text=int(result))
- def next_full_moon(bot,update):
- print('Получено сообщение: %s' % update.message.text)
- ephem_list=update.message.text.split()
- date=ephem_list[1]
- bot.sendMessage(update.message.chat_id, text=str(ephem.next_full_moon(date)))
- def bot_work():
- updater=Updater("286223894:AAFR57Bru4f7xEcEBTQ4Q12pVw8SJXM0uU8")
- dp=updater.dispatcher
- dp.add_handler(CommandHandler('text_calculation',text_calculation))
- dp.add_handler(CommandHandler('calculate',calculate))
- dp.add_handler(CommandHandler('words_count',words_count))
- dp.add_handler(CommandHandler('start',start))
- dp.add_handler(CommandHandler('bot_help',bot_help))
- dp.add_handler(MessageHandler([Filters.text], talk_to_me))
- dp.add_handler(CommandHandler('next_full_moon',next_full_moon))
- updater.start_polling()
- updater.idle()
- if __name__==("__main__"):
- bot_work()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement