Advertisement
Guest User

bot_code

a guest
Oct 7th, 2016
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.45 KB | None | 0 0
  1. from telegram.ext import Updater, CommandHandler, MessageHandler, Filters
  2. import ephem
  3.  
  4. phrases={"привет":"привет","как дела":"хорошо,а у тебя","хорошо":"ну и здорово","плохо":"взбодрись"}
  5.  
  6. numbers_in_russian={'один':1,'два':2,'три':3,
  7. 'четыре':4,'пять':5,'шесть':6,'семь':7,'восемь':8,
  8. 'девять':9,'десять':10,
  9. 'умножить_на':'*','разделить_на':'/','плюс':'+','минус':'-'}
  10.  
  11. incorrect='?.,!'
  12.  
  13.  
  14.  
  15. def get_answer(key,phrases):
  16.     for symbol in incorrect:
  17.         key=key.replace(symbol,"")
  18.     key=key.strip()
  19.     return phrases.get(key.lower(), "")
  20.  
  21.  
  22.  
  23. def start(bot,update):
  24.     print("Вызван/start")
  25.     bot.sendMessage(update.message.chat_id, text='Привет.')
  26.  
  27.  
  28. def bot_help(bot,update):
  29.     print("Помощь пользователю/help")
  30.     bot.sendMessage(update.message.chat_id, text="Сообщения ,на которые отвечает бот : привет, как дела, хорошо, плохо")
  31.  
  32. def talk_to_me(bot,update):
  33.     print('Получено сообщение: %s' % update.message.text)
  34.     bot.sendMessage(update.message.chat_id, text=get_answer(update.message.text, phrases))
  35.  
  36.  
  37. def words_count(bot,update):
  38.     print('Получено сообщение: %s' % update.message.text)
  39.     words_list=update.message.text.split()
  40.     words=len(words_list)-1
  41.     bot.sendMessage(update.message.chat_id, text=str(words))
  42.  
  43.  
  44. def calculate(bot,update):
  45.     print('Получено сообщение: %s' % update.message.text)
  46.  
  47.     nums_for_calculation=update.message.text.strip().split()
  48.     if "*" in nums_for_calculation:
  49.         result=int(nums_for_calculation[1])* int(nums_for_calculation[3])
  50.         bot.sendMessage(update.message.chat_id, text=int(result))
  51.  
  52.     if "+" in nums_for_calculation:
  53.         result=int(nums_for_calculation[1])+ int(nums_for_calculation[3])
  54.         bot.sendMessage(update.message.chat_id, text=int(result))
  55.  
  56.  
  57.     if "-" in nums_for_calculation:
  58.         result=int(nums_for_calculation[1])- int(nums_for_calculation[3])
  59.         bot.sendMessage(update.message.chat_id, text=int(result))    
  60.  
  61.     if "/" in nums_for_calculation:
  62.         if int(nums_for_calculation[3])==0:
  63.             bot.sendMessage(update.message.chat_id, text='Нельзя делить на 0')
  64.         else:
  65.             result=int(nums_for_calculation[1])/ int(nums_for_calculation[3])
  66.             bot.sendMessage(update.message.chat_id, text=int(result))    
  67.  
  68.  
  69. def text_calculation(bot,update):
  70.     print('Получено сообщение: %s' % update.message.text)
  71.     text_calculation_list=update.message.text.split()
  72.     #bot.sendMessage(update.message.chat_id, text="/".join(text_calculation_list))
  73.     first_num=numbers_in_russian.get(text_calculation_list[1])
  74.     operation=numbers_in_russian.get(text_calculation_list[2])
  75.     second_num=numbers_in_russian.get(text_calculation_list[3])
  76.  
  77.     if "+" in operation:
  78.         result=int(first_num)+int(second_num)
  79.         bot.sendMessage(update.message.chat_id, text=int(result))
  80.  
  81.     if "-" in operation:
  82.         result=int(first_num)-int(second_num)
  83.         bot.sendMessage(update.message.chat_id, text=int(result))
  84.  
  85.     if "*" in operation:
  86.         result=int(first_num)*int(second_num)
  87.         bot.sendMessage(update.message.chat_id, text=int(result))
  88.  
  89.     if "/" in operation:
  90.         result=int(first_num)/int(second_num)
  91.         bot.sendMessage(update.message.chat_id, text=int(result))
  92.  
  93.  
  94. def next_full_moon(bot,update):
  95.     print('Получено сообщение: %s' % update.message.text)
  96.     ephem_list=update.message.text.split()
  97.     date=ephem_list[1]
  98.     bot.sendMessage(update.message.chat_id, text=str(ephem.next_full_moon(date)))
  99.  
  100.  
  101.  
  102.  
  103. def bot_work():
  104.     updater=Updater("286223894:AAFR57Bru4f7xEcEBTQ4Q12pVw8SJXM0uU8")
  105.  
  106.    
  107.     dp=updater.dispatcher
  108.     dp.add_handler(CommandHandler('text_calculation',text_calculation))
  109.     dp.add_handler(CommandHandler('calculate',calculate))
  110.     dp.add_handler(CommandHandler('words_count',words_count))
  111.     dp.add_handler(CommandHandler('start',start))
  112.     dp.add_handler(CommandHandler('bot_help',bot_help))
  113.     dp.add_handler(MessageHandler([Filters.text], talk_to_me))
  114.     dp.add_handler(CommandHandler('next_full_moon',next_full_moon))
  115.  
  116.  
  117.     updater.start_polling()
  118.     updater.idle()
  119.  
  120.  
  121. if __name__==("__main__"):
  122.     bot_work()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement