def sendRequest(p, url, data, debugInfo): from main import tell_admin try: resp = requests.post(url, data) logging.info('Response: {}'.format(resp.text)) respJson = json.loads(resp.text) success = respJson['ok'] if success: return True else: status_code = resp.status_code error_code = respJson['error_code'] description = respJson['description'] if error_code == 403: # Disabled user p.setEnabled(False, put=True) #logging.info('Disabled user: '******'❗ Input user disactivated: ' + p.getFirstNameLastNameUserName() logging.debug(debugMessage) tell_admin(debugMessage) else: debugMessage = '❗ Raising unknown err ({}).' \ '\nStatus code: {}\nerror code: {}\ndescription: {}.'.format( debugInfo, status_code, error_code, description) logging.error(debugMessage) # logging.debug('recipeint_chat_id: {}'.format(recipient_chat_id)) logging.debug('Telling to {} who is in state {}'.format(p.chat_id, p.state)) tell_admin(debugMessage) except: report_exception()
def setMenu(menu_items): from main import tell_admin response_data = { "persistent_menu": [ { "locale": "default", "composer_input_disabled": False, #Set to true to disable user input in the menu. This means users will only be able to interact with your bot via the menu, postbacks, buttons, and webviews. "call_to_actions": [ { "title": "Opzioni", "type": "nested", "call_to_actions": [ { "type": "postback", "title": i, "payload": i, } for i in menu_items ] }, { "title": "Links", "type": "nested", "call_to_actions": [ { "type": "web_url", "title": "Telegram bot", "url": "https://t.me/PickMeUp_bot", "webview_height_ratio": "tall" # compact, tall, full }, { "type": "web_url", "title": "Website", "url": "http://pickmeup.trentino.it", "webview_height_ratio": "tall" # compact, tall, full } ] }, ] }, #{ # "locale": "default", # "composer_input_disabled": False #Set to true to disable user input in the menu. This means users will only be able to interact with your bot via the menu, postbacks, buttons, and webviews. #} ] } response_data_str = json.dumps(response_data) try: logging.info('sending menu with json: {}'.format(response_data)) resp = requests.post(key.FACEBOOK_PROFILE_API_URL, data=response_data_str, headers=json_headers) logging.info('responding to request: {}'.format(resp.text)) tell_admin('Response to set persisten menu: {}'.format(resp.text)) return resp.status_code == 200 except: report_exception()
def getDescription(self, driver_info=True): import routing_util import params import date_time_util as dtu import person msg = [] percorso = self.getPercorso() start_fermata, end_fermata = routing_util.decodePercorso(percorso) msg.append('*Partenza*: {}'.format(start_fermata)) msg.append('*Arrivo*: {}'.format(end_fermata)) if self.programmato: giorni = [ params.GIORNI_SETTIMANA_FULL[i] for i in self.programmato_giorni ] giorni_str = ', '.join(giorni) msg.append('*Ora partenza*: {}'.format(self.getDepartingTime())) msg.append('*Tipologia*: {}'.format(self.getTimeMode())) msg.append('*Ogni*: {}'.format(giorni_str)) else: msg.append('*Quando*: {}'.format(self.getTimeMode())) msg.append('*Ora partenza*: {}'.format(self.getDepartingTime())) date_str = dtu.formatDate(self.start_datetime) if date_str == dtu.formatDate(dtu.nowCET()): date_str += ' (OGGI)' elif date_str == dtu.formatDate(dtu.tomorrow()): date_str += ' (DOMANI)' elif self.programmato_giorni: # PROX_GIORNI giorno_index = self.programmato_giorni[0] date_str += ' ({})'.format( params.GIORNI_SETTIMANA[giorno_index]) msg.append('*Giorno partenza*: {}'.format(date_str)) if driver_info: username = person.getPersonById( self.driver_id).getUsername() # self.driver_username if username is None: from main import tell_admin tell_admin( '❗ viaggio con driver_id {} non ha più username'.format( self.driver_id)) username = '******' else: username = '******'.format(username) msg.append('*Autista*: {} {}'.format(self.getDriverName(), username)) avg_distance, avg_duration = self.getAvgDistanceDuration() msg.append('*Distanza*: {}'.format(avg_distance)) msg.append('*Durata*: {}'.format(avg_duration)) return '\n'.join(msg)
def setGetStartedButton(): from main import tell_admin response_data = {"get_started": {"payload": "START"}} response_data_str = json.dumps(response_data) try: logging.info('sending menu with json: {}'.format(response_data)) resp = requests.post(key.FACEBOOK_PROFILE_API_URL, data=response_data_str, headers=json_headers) logging.info('responding to request: {}'.format(resp.text)) tell_admin('Response to GetStartedButton: {}'.format(resp.text)) return resp.status_code == 200 except: report_exception()
def sendMessageWithList(p, msg, button_items): from main import tell_admin msg = msg.replace('*', '') request_data = { "recipient": { "id": p.chat_id }, "message": { "attachment": { "type": "template", "payload": { "template_type": "list", "top_element_style": "compact", # large, compact "elements": [{ "title": i, "subtitle": i, "default_action": { "type": "postback", "payload": i } } for i in button_items] } } } } logging.info('message request with list: {}'.format(request_data)) request_data_str = json.dumps(request_data) try: logging.info( 'responding to request with message: {}'.format(request_data)) resp = requests.post(key.FACEBOOK_MSG_API_URL, data=request_data_str, headers=json_headers) tell_admin('response to request with list: {}'.format(resp.text)) logging.info('status code: {}'.format(resp.status_code)) code = resp.status_code if code == 403: # Disabled user p.setEnabled(False, put=True) # logging.info('Disabled user: ' + p.getFirstNameLastNameUserName()) return code == 200 except: report_exception()
def getDescription(self, driver_info=True): import routing_util import params import date_time_util as dtu import person msg = [] percorso = self.getPercorso() start_stop, end_stop = routing_util.decodePercorsoShort(percorso) msg.append('*Partenza*: {}'.format(start_stop)) msg.append('*Arrivo*: {}'.format(end_stop)) if self.programmato: msg.append('*Tipologia*: {}'.format(self.getTimeMode())) if self.start_datetime: giorni = [params.GIORNI_SETTIMANA_FULL[i] for i in self.programmato_giorni] giorni_str = ', '.join(giorni) msg.append('*Ora partenza*: {}'.format(self.getDepartingTimeStr())) msg.append('*Ogni*: {}'.format(giorni_str)) else: msg.append('*Quando*: {}'.format(self.getTimeMode())) msg.append('*Giorno partenza*: {}'.format(self.getDepartingDateStr())) msg.append('*Ora partenza*: {}'.format(self.getDepartingTimeStr())) if driver_info: username = person.getPersonById(self.driver_id).getUsername() # self.driver_username if username is None: from main import tell_admin tell_admin('❗ viaggio con driver_id {} non ha più username'.format(self.driver_id)) username = '******' else: username = '******'.format(username) msg.append('*Autista*: {} {}'.format(self.getDriverName(), username)) avg_distance, avg_duration = self.getAvgDistanceDuration() msg.append('*Distanza*: {}'.format(avg_distance)) msg.append('*Durata*: {}'.format(avg_duration)) return '\n'.join(msg)
def report_exception(): from main import tell_admin import traceback msg = "❗ Detected Exception: " + traceback.format_exc() tell_admin(msg) logging.error(msg)