def receive_message(msg): if msg["date"] < time.time() - 2: return # old logger.msg(msg) if msg["text"] == "/help" or msg["text"] == "/help@" + bot.getBotUsername( ): keyboard = {"keyboard": [["/help"], ["/quit"], ["/test keyboard"]]} bot.sendMessage(msg["chat"]["id"], "Commands: (in custom keyboard)", reply_markup=json.dumps(keyboard)) if msg["text"] == "/quit" or msg["text"] == "/quit@" + bot.getBotUsername( ): if msg["from"]["id"] == 43804645: bot.sendMessage(msg["chat"]["id"], "Bye!") bot.quit = True else: bot.sendMessage(msg["chat"]["id"], "You don't have permission!") if msg["text"] == "/test keyboard": keyboard = { "keyboard": [[ "1⃣", "2⃣", "3⃣", "4⃣", "5⃣", "6⃣", "7⃣", "8⃣", "9⃣", "0⃣", "XD" ], ["q", "w", "e", "r", "t", "y", "u", "i", "o", "p"], ["/help", "/quit", "/test keyboard"]] } bot.sendMessage(msg["chat"]["id"], "Sending custom keyboard!", reply_markup=json.dumps(keyboard))
def onOutgoingCall( self, id, caller, callee ): now = datetime.datetime.now() logger.msg( "Invoked onOutgoingCall handler" ) query = "begin; \ insert into calls ( dt_start, id_caller ) values( '%s', %s ); \ select currval('calls_id_seq');" % ( now, caller ) self.db.runQuery( query ).addCallback( self.addDictionaryEntry, id ).addCallback( self.insertCallHistory, self.callStatuses[ 'OUTGOING' ], now ).addErrback( self._queryFail )
def onAnswer( self, id, caller, callee ): now = datetime.datetime.now() logger.msg( "Invoked onAnswer handler" ) if not self.ids.has_key( id ): logger.warn( "Something strange happened: answered a non-received call." ) else: self.insertCallHistory( self.ids[ id ], self.callStatuses[ 'ANSWER' ], now )
def onAnswer(self, id, caller, callee): now = datetime.datetime.now() logger.msg("Invoked onAnswer handler") if not self.ids.has_key(id): logger.warn( "Something strange happened: answered a non-received call.") else: self.insertCallHistory(self.ids[id], self.callStatuses['ANSWER'], now)
def onOutgoingCall(self, id, caller, callee): now = datetime.datetime.now() logger.msg("Invoked onOutgoingCall handler") query = "begin; \ insert into calls ( dt_start, id_caller ) values( '%s', %s ); \ select currval('calls_id_seq');" % (now, caller) self.db.runQuery(query).addCallback( self.addDictionaryEntry, id).addCallback(self.insertCallHistory, self.callStatuses['OUTGOING'], now).addErrback(self._queryFail)
def onHangup( self, id, caller, callee ): now = datetime.datetime.now() logger.msg( "Invoked onHangup handler" ) if not self.ids.has_key( id ): logger.warn( "Something strange happened: hanged-up a non-received call." ) else: # TODO: maybe it's better to do it with chain of deffereds? query = "select dt_start from calls where id = %i;" % ( self.ids[ id ] ) self.db.runQuery( query ).addCallback( self.updateCallEntry, now, self.ids[ id ], callee ).addErrback( self._queryFail ) self.insertCallHistory( self.ids[ id ], self.callStatuses[ 'HANG_UP' ], now ) del self.ids[ id ] logger.msg( self.ids )
def on_msg_receive(msg): #if not syncFinished: # print ("RET - 1") # return; #pp.pprint(utils.msg2dict(msg)) logger.msg(msg) if msg.dest.id == our_id: # direct message peer = msg.src else: # chatroom peer = msg.dest if msg.text is not None and msg.text.startswith("!ping"): peer.send_msg("PONG!") elif msg.text is not None and msg.text.startswith("!emoji"): peer.send_msg("Emojis: \n" + emoji.emojize(':white_check_mark: :smile: :laughing: :blush: :smiley: :x: :heavy_check_mark:', use_aliases=True).encode('UTF-8')) return
def onHangup(self, id, caller, callee): now = datetime.datetime.now() logger.msg("Invoked onHangup handler") if not self.ids.has_key(id): logger.warn( "Something strange happened: hanged-up a non-received call.") else: # TODO: maybe it's better to do it with chain of deffereds? query = "select dt_start from calls where id = %i;" % ( self.ids[id]) self.db.runQuery(query).addCallback( self.updateCallEntry, now, self.ids[id], callee).addErrback(self._queryFail) self.insertCallHistory(self.ids[id], self.callStatuses['HANG_UP'], now) del self.ids[id] logger.msg(self.ids)
def receive_message(msg): if msg["date"] < time.time() - 2: return # old logger.msg(msg) if msg["text"] == "/help" or msg["text"] == "/help@" + bot.getBotUsername(): keyboard = {"keyboard": [["/help"], ["/quit"], ["/test keyboard"]]} bot.sendMessage(msg["chat"]["id"], "Commands: (in custom keyboard)", reply_markup=json.dumps(keyboard)) if msg["text"] == "/quit" or msg["text"] == "/quit@" + bot.getBotUsername(): if msg["from"]["id"] == 43804645: bot.sendMessage(msg["chat"]["id"], "Bye!") bot.quit = True else: bot.sendMessage(msg["chat"]["id"], "You don't have permission!") if msg["text"] == "/test keyboard": keyboard = { "keyboard": [ ["1⃣", "2⃣", "3⃣", "4⃣", "5⃣", "6⃣", "7⃣", "8⃣", "9⃣", "0⃣", "XD"], ["q", "w", "e", "r", "t", "y", "u", "i", "o", "p"], ["/help", "/quit", "/test keyboard"], ] } bot.sendMessage(msg["chat"]["id"], "Sending custom keyboard!", reply_markup=json.dumps(keyboard))
def receive_location(msg): logger.msg(msg)
def new_chat_title(msg): logger.msg(msg)
def receive_video(msg): logger.msg(msg)
def receive_contact(msg): logger.msg(msg)
def connectionLost( self, reason ): logger.msg( "Connection terminated" )
def receive_sticker(msg): logger.msg(msg)
def receive_audio(msg): logger.msg(msg)
def group_chat_created(msg): logger.msg(msg)
def connectionMade( self ): logger.msg( "Received new connection" )
def left_chat_participant(msg): logger.msg(msg)
def _querySuccess(self, result): # TODO: need something more useful here logger.msg("Query committed.")
def connectionLost(self, reason): logger.msg("Connection terminated")
def connectionMade(self): logger.msg("Received new connection")
def new_chat_photo(msg): logger.msg(msg)
def receive_document(msg): logger.msg(msg)
def delete_chat_photo(msg): logger.msg(msg)
def receive_photo(msg): logger.msg(msg)
def receive_message(msg): if msg["date"] < time.time() - 2: return # old logger.msg(msg) if msg["text"] == "/help" or msg["text"] == "/help@" + bot.getBotUsername(): options = {"keyboard":[["/help"], ["/quit"], ["/test keyboard"], ["/mqm"]]} bot.sendMessage(msg["chat"]["id"], "Commands: (in custom keyboard)", reply_markup=json.dumps(options)) elif msg["text"] == "/quit" or msg["text"] == "/quit@" + bot.getBotUsername(): if msg["from"]["id"] == 43804645 or msg["from"]["id"] == 61615919: bot.sendMessage(msg["chat"]["id"], "Bye!") bot.quit = True else: bot.sendMessage(msg["chat"]["id"], "You don't have permission!") elif msg["text"] == "/test keyboard": keyboard = {"keyboard":[["1⃣","2⃣","3⃣","4⃣","5⃣","6⃣","7⃣","8⃣","9⃣","0⃣", "XD"],["q","w","e","r","t","y","u","i","o","p"],["/help", "/quit", "/test keyboard"]]} bot.sendMessage(msg["chat"]["id"], "Sending custom keyboard!", reply_markup=json.dumps(keyboard)) elif msg["text"] == "/mqm": imgClasses = None for i in range(1,4500): request = requests.get('http://www.mequeme.com/page/'+str(i)) tree = html.fromstring(request.content) imgClasses =tree.xpath("//div[@class='media']//img") if len(imgClasses) != 0: break imgClass = imgClasses[0] # Debug print (html.etree.tostring(imgClass, pretty_print=True)) imgURL = imgClass.attrib['src'] # Not the best way to do this but... caption = imgClass.attrib['alt'] print(caption) response = requests.get(imgURL) f = open("sample.gif", 'wb') f.write(response.content) f.close() img = open('sample.gif', 'rb') bot.sendChatAction(msg["chat"]["id"], 'upload_photo') bot.sendImage(msg["chat"]["id"], img, caption=caption) img.close() os.remove('sample.gif') elif msg["text"] == "/mqm random": imgClasses=None while True: rand = random.randint(0,4500) request = requests.get('http://www.mequeme.com/page/'+str(rand)) tree = html.fromstring(request.content) imgClasses =tree.xpath("//div[@class='media']//img") if len(imgClasses) != 0: break imgClass=imgClasses[random.randint(0,len(imgClasses)-1)] # Debug print (html.etree.tostring(imgClass, pretty_print=True)) imgURL = imgClass.attrib['src'] # Not the best way to do this but... caption = imgClass.attrib['alt'] print(caption) response = requests.get(imgURL) f = open("sample.gif", 'wb') f.write(response.content) f.close() img = open('sample.gif', 'rb') bot.sendChatAction(msg["chat"]["id"], 'upload_photo') bot.sendImage(msg["chat"]["id"], img, caption=caption) img.close() os.remove('sample.gif')
def _querySuccess( self, result ): # TODO: need something more useful here logger.msg( "Query committed." )