def main(): args = parse_option() apikey = args.apikey if apikey is None: apikey = os.environ['DOCO_API_KEY'] if apikey is None: sys.exit(1) level = logging.INFO if args.verbose is True else logging.ERROR logging.basicConfig(level=level, format=DocoInterface.debug_log_format) shiritori = args.shiritori options = {} if shiritori is True: options['mode'] = 'srtr' if args.type is not None: options['t'] = args.type client = Client(apikey=apikey, user=options) if args.intaractive is False: if args.utt is None: sys.exit(1) res = client.send(utt=args.utt, apiname='Dialogue') write(str(res)) else: console = Console() console.client = client sys.ps1 = '>>> ' console.interact('Welcome to docomo dialogue cli.\n^D to exit.')
def test_context(self, m): """ Dialogue().parse() should extract context. """ dummy_response(m) c = Client(apikey='') c.send(utt=u'ぬるぽ', apiname='Dialogue') ret = c.apis['Dialogue'].parse(c.last_response) self.assertEqual(ret['context'], 'D0yHgwljc_mhTPIGs--toQ')
def docomo_response(text): docomo_client = Client(apikey=os.environ['DOCOMO_API_KEY']) response = docomo_client.send(utt=text, apiname='Dialogue') logger.debug(response) return [{"type": "text", "text": response['utt']}]
def test_parse_response(self, m): """ Dialogue().parse() should parse request to api. """ dummy_response(m) c = Client(apikey='') c.send(utt=u'ぬるぽ', apiname='Dialogue') ret = c.apis['Dialogue'].parse(c.last_response) self.assertTrue(isinstance(ret, dict))
def replay_chat(self, word): """ チャットを返す :param word: :return: """ c = Client(apikey=docomo_key) res = c.send(utt=word, apiname='Dialogue') return res['utt']
class DocomoDialogAPI(object): def __init__(self, api_key=None): api_key = os.environ.get('DOCOMO_DIALOGUE_API_KEY', api_key) self.__client = Client(apikey=api_key) def reply(self, text): response = self.__client.send(utt=text, apiname='Dialogue') utt = response['utt'] return utt
def test_build_request(self): """ Dialogue().build_request() should update default user info. """ user = { 'nickname': 'foo', 'bloodtype': 'O', 'birthdateY': '1997', 'birthdateM': '12', 'birthdateD': '31' } c = Client(apikey='key', user=user) ret = c.apis['Dialogue'].build_request(age='17', mode='srtr', bloodtype='A') user['age'] = 17 user['mode'] = 'srtr' user['bloodtype'] = 'A' self.assertEqual(sorted(ret), sorted(user)) self.assertEqual(sorted(c.apis['Dialogue'].user), sorted(user))
def test_last_response(self, m): """ Client().send() should send request to api and set response. """ dummy_response(m) c = Client(apikey='') c.send(utt=u'ぬるぽ', apiname='Dialogue') self.assertEqual(c.last_response.status_code, 200)
def test_send(self, m): """ Client().send() should send request to api. """ dummy_response(m) c = Client(apikey='') ret = c.send(utt=u'ぬるぽ', apiname='Dialogue') self.assertEqual(ret['context'], 'D0yHgwljc_mhTPIGs--toQ')
from doco.client import Client app = Flask(__name__) HELP = """★宮大支援課お知らせBOT[非公式] このBOTは非公式のものです。宮崎大学とは一切関係ありません。 支援課からの新しいお知らせがあったときにこのBOTが教えてくれます。 '宮大'を送信すると直近5件のお知らせを表示します 'help'を送信するとこのメッセージを表示します Twitterアカウントはこちら↓ https://twitter.com/miya_330_bot""" myzk = miyadaidb.MiyadaiDatabaseControl() line_bot_api = LineBotApi(os.environ.get('CHANNEL_ACCESS_TOKEN')) # Your Channel Access Token handler = WebhookHandler(os.environ.get('CHANNEL_SECRET')) # Your Channel Secret c = Client(apikey=os.environ.get('DOCOMO_API_KEY')) @app.route("/callback", methods=['POST']) def callback(): # get X-Line-Signature header value signature = request.headers['X-Line-Signature'] # get request body as text body = request.get_data(as_text=True) app.logger.info("Request body: " + body) # handle webhook body try: handler.handle(body, signature) except InvalidSignatureError:
def test_default_useragent(self): """ Default user-agent should be `Python client 0.0.3`.""" c = Client(apikey='key') self.assertEqual(c.DEFAULT_USER_AGENT, 'Python client 0.0.3')
def test_default_header(self): """ Default headers should contains user-agent. """ c = Client(apikey='key') self.assertEqual(c.DEFAULT_HEADERS['User-Agent'], 'Python client 0.0.3')
channel_secret = settings.CHANNEL_SECRET channel_access_token = settings.ACCESS_TOKEN if channel_secret is None: print('Specify LINE_CHANNEL_SECRET as environment variable.') sys.exit(1) if channel_access_token is None: print('Specify LINE_CHANNEL_ACCESS_TOKEN as environment variable.') sys.exit(1) #process line api keys line_bot_api = LineBotApi(channel_access_token) handler = WebhookHandler(channel_secret) #process docomo api key user = settings.user_config doco = Client(docomo_api_key, user=user) @app.route("/callback", methods=['POST']) def callback(): # get X-Line-Signature header value signature = request.headers['X-Line-Signature'] # get request body as text body = request.get_data(as_text=True) app.logger.info("Request body: " + body) # handle webhook body try: handler.handle(body, signature) except InvalidSignatureError:
def __init__(self, api_key=None): api_key = os.environ.get('DOCOMO_DIALOGUE_API_KEY', api_key) self.__client = Client(apikey=api_key)