class RtmEventHandler(object): def __init__(self, slack_clients): self.clients = slack_clients self.wit_token = "CBTOMOXYK3WQ74JW47IZGICC65EXBGZ2" logging.info("wit token: {}".format(self.wit_token)) self.wit_client = Wit(self.wit_token) logging.info("wit: {}".format(dir(self.wit_client))) def handle(self, event): if 'type' in event: self._handle_by_type(event['type'], event) def _handle_by_type(self, event_type, event): # See https://api.slack.com/rtm for a full list of events if event_type == 'error': # error logger.debug('Error event') elif event_type == 'message': # message was sent to channel self._handle_message(event) elif event_type == 'channel_joined': # you joined a channel logger.debug('Channel joined') elif event_type == 'group_joined': # you joined a private group logger.debug('Group joined') else: pass def _handle_message(self, event): # Filter out messages from the bot itself if not self.clients.is_message_from_me(event['user']): logger.debug('Got event: {}'.format(event)) user = self.clients.rtm.server.users.find(event['user']) logger.debug('From user: {}'.format(user)) msg_txt = event['text'] if self.clients.is_bot_mention(msg_txt): # User mentiones bot session_id = 'test' channel_id = event['channel'] context = { 'channel_id':channel_id, 'user': user.name, 'user_id': event['user'], } logger.debug('Sending msg: {} to Wit.ai'.format(msg_txt)) resp = self.wit_client.converse(session_id, msg_txt,context) logger.debug('Got resp: {}'.format(resp)) if resp['type'] == 'msg': msg = resp['msg'] if isinstance(channel_id, dict): channel_id = channel_id['id'] logger.debug('Sending msg: {} to channel: {}'.format(msg, channel_id)) channel = self.clients.rtm.server.channels.find(channel_id) channel.send_message("{}".format(msg.encode('ascii', 'ignore'))) elif resp['type'] == 'action': logger.debug('do {}'.format(resp['action']))
def converse(Query): access_token = "HUYFSZATE2FRGLETGVWWTCHNSVTXBKDC" def send(request, response): print(response['text']) actions = { 'send': send, } tweet = {"tweet_text": "", "tweet_url": [], "media_url": []} client = Wit(access_token=access_token, actions=actions) #Query="" data = client.converse(1, Query) X = "" for key in data['entities'].keys(): X = X + " " + str(data['entities'][key][0]['value']) mydict = json.load(open(os.path.join(BASE, "mydict.json"))) solr = True for key_list in mydict.keys(): flag = True for key in data['entities'].keys(): if str(data['entities'][key][0]['value']).upper() not in str( key_list): flag = False if flag: if "JOKE" in key_list: x = randint(1, 4) tweet['tweet_text'] = mydict[key_list[:-1] + str(x)] else: tweet['tweet_text'] = mydict[key_list] solr = False if len(data['entities'].keys()) == 0: solr = True X = Query if solr: tweet = solrhandler.solrcall(X, data) #print tweet['tweet_url'][0][0] x = [] for i in range(len(tweet['tweet_url'])): x.append(tweet['tweet_url'][i][0]) tweet['tweet_url'] = x print tweet return tweet # whats your name # what can you do # What is your opinion on demonetization # Tell me a joke # Show me the most positive/neg/neut tweets <from dna> on demonetization # Show me the most retweeted/favourited tweet <from dna> on demonetization # Who implemented demonetization
def send(request, response): print(response['text']) actions = { 'send': send, } client = Wit(access_token=access_token, actions=actions) #client.interactive() # each message being sent below should be configured in the online session json = client.converse(access_token, 'Hi', {}) print('Yay, got Wit.ai response: ' + str(json)) json = client.converse(access_token, 'Shutup', {}) print('Yay, got Wit.ai response: ' + str(json)) # for more details on parsing, check wit.py __run_actions print('Response type: %s' % resp['type']) print("Response: %s" % json.get('msg').encode('utf8')) # the below call run_actions which will print the response on the console (but doesn't return it back - TBD) # context0 = {} # context1 = client.run_actions(access_token, 'Hi', context0) # print('The session state is now: ' + str(context1)) # context2 = client.run_actions(access_token, 'shutup', context1)
greetings = ['Hello','Hola', 'Bonjour'] def send(request, response): print(response['text']) def greet(request): context=request['context'] context['greets']=greetings[randint(0,2)] return context actions = { 'send': send, 'greet':greet, } query=sys.argv[1] client = Wit(access_token=access_token, actions=actions) pprint(client.converse('test_session',query,{})) #msg=client.converse('test_session',query,{}) #while msg['type']!='stop': # if 'msg' not in msg: # pass # else: # print msg['msg'] # msg=client.converse('test_session',query,{}) client.interactive()