Advertisement
Guest User

Untitled

a guest
Apr 20th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.33 KB | None | 0 0
  1. import telegram
  2. from telegram.ext import Updater
  3. import logging
  4. import time
  5. import os
  6. import json
  7. from watson_developer_cloud import DialogV1, SpeechToTextV1
  8.  
  9. token = '184561786:AAGT5NJjxPSqTm3P4b30_hmGLhnTzgZV34s'
  10.  
  11. dialog_credinitials = {
  12.     "url": "https://gateway.watsonplatform.net/dialog/api",
  13.     "password": "Mw4bIj47ckGZ",
  14.     "username": "a1d505c8-c92f-4cf7-8fec-9a5df38c53bd"
  15. }
  16. dialog_id = 'a103aed4-1141-42da-a618-889f6b634682'
  17.  
  18. speech_to_text_credentials = {
  19.     "url": "https://stream.watsonplatform.net/speech-to-text/api",
  20.     "password": "Dquwd3TcGsws",
  21.     "username": "62413b3c-e9d3-42e7-a013-5829170dba80"
  22. }
  23.  
  24. audio_content_type = 'audio/ogg;codecs=opus'
  25.  
  26. def start(bot, update):
  27.     print 'start called!'
  28.     bot.watson_info = dict()
  29.     bot.credinitials = dialog_credinitials
  30.     bot.watson_info['dialog_id'] = dialog_id
  31.     bot.dialog = DialogV1(username=dialog_credinitials['username'], password = dialog_credinitials['password'])
  32.     response = bot.dialog.conversation(dialog_id)
  33.     print 'got a resp from start'
  34.     bot.speech_to_text = SpeechToTextV1(url=speech_to_text_credentials['url'],
  35.                                         username=speech_to_text_credentials['username'],
  36.                                         password=speech_to_text_credentials['password'])
  37.  
  38.     bot.watson_info['conversation_id'] = response['conversation_id']
  39.     bot.watson_info['client_id'] = response['client_id']
  40.  
  41.     bot.sendMessage(chat_id=update.message.chat_id, text='\n'.join(response['response']))
  42.     print 'sent a message!'
  43.  
  44.  
  45. def echo(bot, update):
  46.     #print dir(update.message)
  47.     #print update.message.voice
  48.     if update.message.voice is not None:
  49.         print 'got an audiofile!'
  50.         voice_file = bot.getFile(update.message.voice.file_id)
  51.         voice_file.download('voice.ogg')
  52.         response = bot.speech_to_text.recognize(open('voice.ogg', 'rb'), audio_content_type)
  53.         print response
  54.         if response['results'] and float(response['results'][response['result_index']]['alternatives'][0]['confidence']) >= 0.6:
  55.             update.message.text = response['results'][response['result_index']]['alternatives'][0]['transcript']
  56.             print update.message.text
  57.     if update.message.text is not None and update.message.text != '':
  58.         print 'text got'
  59.         response = bot.dialog.conversation(bot.watson_info['dialog_id'],update.message.text,bot.watson_info['client_id'],
  60.                     bot.watson_info['conversation_id'])
  61.         text = response['response']
  62.         print response
  63.         print 'echoing!'
  64.         bot.sendMessage(update.message.chat_id, text ='\n'.join(text))
  65.     else:
  66.         bot.sendMessage(update.message.chat_id, text="Don't know! Rly, help me and try again!")
  67.     print 'message sent!'
  68.  
  69. if __name__ == '__main__':
  70.     logger = logging.getLogger()
  71.     logger.setLevel(logging.INFO)
  72.     bot = telegram.Bot(token=token)
  73.     updater = Updater(token=token)
  74.     # updater.dispatcher.telegram_command_handlers.pop('start')
  75.     updater.dispatcher.addTelegramCommandHandler('start', start)
  76.     # updater.dispatcher.telegram_message_handlers.pop()
  77.     updater.dispatcher.addTelegramMessageHandler(echo)
  78.     updater.start_polling()
  79.     print 'polled!'
  80.     if "VCAP_SERVICES" in os.environ:
  81.         vcaps = json.loads(os.environ["VCAP_SERVICES"])
  82.         print vcaps
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement