forked from millennium-salander/ai_python_bot
- Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbot.py
More file actions
Latest commit
31 lines (31 loc) · 1.38 KB
/
Copy pathbot.py
File metadata and controls
31 lines (31 loc) · 1.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Options
fromtelegram.extimportUpdater, CommandHandler, MessageHandler, Filters
importapiai, json
updater=Updater(token='YOURE TOKEN') # Telegram API Token
dispatcher=updater.dispatcher
# Обработка команд
defstartCommand(bot, update):
bot.send_message(chat_id=update.message.chat_id, text='Hello, do you want to talk?')
deftextMessage(bot, update):
request=apiai.ApiAI('YOURE DIALOGFLOW TOKEN').text_request() # Dialogflow API Token
request.lang='en'# Request language
request.session_id='YoureBot'# ID dialog session (for bot training)
request.query=update.message.text# Send request to AI ИИ with the user message
responseJson=json.loads(request.getresponse().read().decode('utf-8'))
response=responseJson['result']['fulfillment']['speech'] # Take JSON answer
ifresponse:
bot.send_message(chat_id=update.message.chat_id, text=response)
else:
bot.send_message(chat_id=update.message.chat_id, text='I dont understand!')
# Handlers
start_command_handler=CommandHandler('start', startCommand)
text_message_handler=MessageHandler(Filters.text, textMessage)
# Add handlers to the dispatcher
dispatcher.add_handler(start_command_handler)
dispatcher.add_handler(text_message_handler)
# Start update search
updater.start_polling(clean=True)
# Stops the bot
updater.idle()