def handle_message(msg):
flavor = telepot.flavor(msg)
content_type, chat_type, chat_id = telepot.glance(msg, flavor=flavor)
if len(chats) == 0 or chat_id in chats:
# just save every message
database.connect()
message = Messages.create(id=msg['message_id'], message=msg, chat_id=chat_id, content_type=content_type)
message.save()
database.close()
pprint(msg)
# TODO: handle message edit
python类flavor()的实例源码
def handle(msg):
content_type, chat_type, chat_id = telepot.glance(msg)
input_text = msg['text']
flavor = telepot.flavor(msg)
summary = telepot.glance(msg, flavor=flavor)
print(flavor, summary)
if input_text.startswith("https://"):
cmd = 'youtube-dl --extract-audio --audio-format mp3 \
--output "audio.%%(ext)s" %summary'%(input_text)
os.system(cmd)
sendAudio(chat_id,'audio.mp3')
else:
bot.sendMessage(chat_id,input_text)
def handle(self, msg):
flavor = telepot.flavor(msg)
if flavor == 'normal':
content_type, chat_type, chat_id = telepot.glance(msg, flavor)
server_logger.info("Normal %s message, %s." % (content_type, chat_id))
await bot.sendMessage(int(chat_id), "I'm an inline bot. You cannot speak to me directly")
elif flavor == 'inline_query':
msg_id, from_id, query_string = telepot.glance(msg, flavor='inline_query')
server_logger.info("Inline equation, %s : %s" % (from_id, query_string))
answerer.answer(msg)
def handle(msg):
flavor = telepot.flavor(msg)
# Check the message is normal
if flavor == 'normal':
content_type, chat_type, chat_id = telepot.glance2(msg)
# Check the message is text
if content_type == 'text':
command = msg['text']
if command.find('/start') != -1 or (command == '/news') != -1:
get_news(chat_id)
def handle(self, msg):
print (msg)
flavor = telepot.flavor(msg)
if flavor == 'chat':
content_type, chat_type, chat_id = telepot.glance(msg)
print ("Chat message: ", content_type, chat_type, chat_id, msg['text'])
# verifico se l'utente da cui ho ricevuto il comando è censito
user_exist = False
for u in MyIPCameraBot_config.users:
if u is None:
break
if u['telegram_id'] == str(chat_id):
user_exist = True
print ("User exist...")
# se l'utente non è censito, abortisco
# questo controllo è per evitare che le risposte dei messaggi
# vadano a richiedenti non abilitati
if user_exist == False:
print ("User NOT exist!!!")
return None
# seleziono il tipo di comando da elaborare
if msg['text'] == '/help':
self.__comm_help(chat_id)
elif msg['text'] == '/start':
self.__comm_help(chat_id)
elif msg['text'] == '/jpg':
self.__comm_jpg(chat_id)
elif msg['text'] == '/status':
self.__comm_status(chat_id)
elif msg['text'] == '/motion':
self.__comm_motion(chat_id)
elif msg['text'] == 'Motion Detection OFF':
self.__comm_motion_detection_off(chat_id)
elif msg['text'] == 'Motion Detection ON':
self.__comm_motion_detection_on(chat_id)
elif msg['text'] == '/night':
self.__comm_night(chat_id)
elif msg['text'] == 'IR Automatico':
self.__comm_night_IR_auto(chat_id)
elif msg['text'] == 'IR On':
self.__comm_night_IR_On(chat_id)
elif msg['text'] == 'IR Off':
self.__comm_night_IR_Off(chat_id)
else:
self.__comm_nonCapisco(chat_id)
else:
raise telepot.BadFlavor(msg)