Fix openweathermap API and add commands to messenger bot

parent 2479dbd1
...@@ -59,6 +59,7 @@ class OWMWeather(object): ...@@ -59,6 +59,7 @@ class OWMWeather(object):
page="http://api.openweathermap.org/data/2.5/weather?q="+str(self.city)+"&units=metric" page="http://api.openweathermap.org/data/2.5/weather?q="+str(self.city)+"&units=metric"
else: else:
page="http://api.openweathermap.org/data/2.5/weather?lat="+str(self.lat)+"&lon="+str(self.lon)+"&mode=json&units=metric" page="http://api.openweathermap.org/data/2.5/weather?lat="+str(self.lat)+"&lon="+str(self.lon)+"&mode=json&units=metric"
page+="&APPID="+str(self.owmid)
cb=wu.getPage(page, headers={'x-api-key': str(self.owmid)}).addCallbacks(self.onData, self.onError) cb=wu.getPage(page, headers={'x-api-key': str(self.owmid)}).addCallbacks(self.onData, self.onError)
def updateDatabase(self): def updateDatabase(self):
......
...@@ -929,3 +929,13 @@ def getChartSeries(chartname): ...@@ -929,3 +929,13 @@ def getChartSeries(chartname):
return StatsChartsSeries.find(where=["name='%s'" % str(chartname)]) return StatsChartsSeries.find(where=["name='%s'" % str(chartname)])
def getScreenshotList(screenshot=False):
qstring="SELECT button_name FROM mediasources WHERE (websection='camera' OR websection='citophone')"
if screenshot:
qstring+=" AND screenshot IS NOT NULL"
return runQuery(qstring)
def getVoiceCommandList():
qstring="SELECT speech_string FROM speech_actions WHERE active=1"
return runQuery(qstring)
...@@ -2449,6 +2449,12 @@ class domotikaService(service.Service): ...@@ -2449,6 +2449,12 @@ class domotikaService(service.Service):
return dmdb.getChartSeries(chartname).addCallback(self.parseChartSeries, resdata, chartname) return dmdb.getChartSeries(chartname).addCallback(self.parseChartSeries, resdata, chartname)
return defer.fail('No chart with the name '+str(chartname)) return defer.fail('No chart with the name '+str(chartname))
def web_on_getVoiceCommandList(self):
return dmdb.getVoiceCommandList()
def web_on_getScreenshotList(self, screenshot=True):
return dmdb.getScreenshotList(screenshot=screenshot)
def web_on_getChartData(self, chartname): def web_on_getChartData(self, chartname):
log.debug('GET CHART DATA FOR '+str(chartname)) log.debug('GET CHART DATA FOR '+str(chartname))
return dmdb.getChartData(chartname).addCallback(self.getChartData, chartname) return dmdb.getChartData(chartname).addCallback(self.getChartData, chartname)
......
...@@ -179,7 +179,7 @@ class MessengerBot(BotCore, MessengerCore): ...@@ -179,7 +179,7 @@ class MessengerBot(BotCore, MessengerCore):
if len(res) > 0: if len(res) > 0:
if res[0] == 'Ok' and len(res) > 1: if res[0] == 'Ok' and len(res) > 1:
try: try:
result = 'ho eseguito "'+" ".join(res[1]['clean']+'"') result = 'ho eseguito "'+" ".join(res[1]['clean'])+'"'
except: except:
pass pass
else: else:
...@@ -210,10 +210,17 @@ class MessengerBot(BotCore, MessengerCore): ...@@ -210,10 +210,17 @@ class MessengerBot(BotCore, MessengerCore):
self.sendMessage(senderid, 'Ok, devo ancora implementare l\'aiuto!') self.sendMessage(senderid, 'Ok, devo ancora implementare l\'aiuto!')
elif txt == u'logout': elif txt == u'logout':
self.sendMessage(senderid, 'Ok, devo ancora implementare anche il logout') self.sendMessage(senderid, 'Ok, devo ancora implementare anche il logout')
elif txt == u'command list':
self.sendCommandList(senderid)
elif txt == u'screenshot list':
self.sendScreenshotList(senderid)
elif txt.startswith('screenshot '):
self.sendScreenshot(senderid, " ".join(txt.split()[1:]))
else: else:
self.sendMessage(senderid, 'Io ci provo a dare questo comando...')
self.core.voiceReceived(txt, confidence=1.0, lang="it").addCallback(voiceResult) self.core.voiceReceived(txt, confidence=1.0, lang="it").addCallback(voiceResult)
def receivedDeliveryConfirmation(self, msg): def receivedDeliveryConfirmation(self, msg):
log.info("Messenger bot received delivery confirmation: "+str(msg)) log.info("Messenger bot received delivery confirmation: "+str(msg))
...@@ -232,6 +239,28 @@ class MessengerBot(BotCore, MessengerCore): ...@@ -232,6 +239,28 @@ class MessengerBot(BotCore, MessengerCore):
if status == u'linked': if status == u'linked':
self.core.add_messenger_psid(senderid, authcode) self.core.add_messenger_psid(senderid, authcode)
def sendCommandList(self, senderid):
def pushList(res):
for r in res:
self.sendMessage(senderid, " * "+str(r[0]))
self.sendMessage(senderid, "Target list for string/voice commands (call them with the right action like up,down,open,close and so on.):")
self.core.getVoiceCommandList().addCallback(pushList)
def sendScreenshotList(self, senderid):
def pushList(res):
for r in res:
self.sendMessage(senderid, " * "+str(r[0]))
self.sendMessage(senderid, "Cam list:")
self.core.getScreenshotList().addCallback(pushList)
def sendScreenshot(self, senderid, target):
def pushImage(res):
self.sendImageMessage(recipient_id, res)
self.core.getScreenshot(target).addCallback(pushImage)
def sendMessage(self, recipient_id, message): def sendMessage(self, recipient_id, message):
payload = { payload = {
'recipient': { 'recipient': {
...@@ -243,6 +272,22 @@ class MessengerBot(BotCore, MessengerCore): ...@@ -243,6 +272,22 @@ class MessengerBot(BotCore, MessengerCore):
} }
return self.sendAPI(payload) return self.sendAPI(payload)
def sendImageMessage(self, recipient_id, imageuri):
payload = {
'recipient': {
'id': recipient_id
},
'message': {
'attachment': {
'type': 'image',
'payload': {
'url': imageuri
}
}
}
}
return self.sendAPI(payload)
def sendAuthRequest(self, recipient_id): def sendAuthRequest(self, recipient_id):
payload = { payload = {
'recipient': { 'recipient': {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment