def start(bot, update): id = update.message.chat.id if id > 0: # groups ignored keyboard = [['Status', 'Media'], ['Settings', 'Chat'], ['Referral link']] reply_markup = ReplyKeyboardMarkup(keyboard, resize_keyboard=True) welcome_text = '''Welcome to the AIRDROP CTLgroup bot! To get 25 CTLcoin please do the following: - specify in the bot settings the links to your reposts from our Facebook and Twitter pages - subscribe to our telegram channel @CryptoTradeLine and go to chat To get additional tokens invite your friends to the bot and get 1 CTLcoin for each one connected to the bot''' bot.send_message(chat_id=id, text=welcome_text, reply_markup=reply_markup) msg = update.message.text.replace('/start', '').strip() entered_user = User.query.filter(User.id==id).first() if entered_user == None: db_session.add(User(id=id, name=update.message.chat.username, inviter=msg)) db_session.commit() print('commited') if msg != '' and msg != str(id): bonus = 1 inviter = User.query.filter(User.id==msg).first() inviter.tokens_acc += bonus inviter.refs += 1 tokens_left = Parametr.query.filter(Parametr.parametr=='Tokens_left').first() tokens_left.value_flt -= bonus db_session.commit() bot.send_message(chat_id=msg, text='+ %s tokens for friend inviting ' % bonus + update.message.chat.first_name) elif msg == str(id): update.message.text_reply('You have received a link to yourself, do /start') elif msg == str(id): bot.send_message(chat_id=id, text='You invited yourself')
def seed_cedict(): """Seed the Chinese/English dictionary with data from CEDICT.""" with open('seeds/cedict3.csv', 'rb') as f: reader = csv.reader(f, delimiter=',', quotechar='"') for row in reader: if row[0] == '#': continue else: for i in range(len(row)): row[i] = row[i].decode('utf-8') trad, simp, pinyin = row[0], row[1], row[2] definition = ''.join(row[3:]) pinyin = pinyin.strip('"') definition = definition.strip('"') entry = Dict_Entry(simplified=simp, traditional=trad, pinyin=pinyin, definition=definition) db_session.add(entry) try: db_session.commit() except sqlalchemy.exc.IntegrityError, e: db_session.rollback()
def new_user(email, username, password, management_api, admin=False): """Initializes a new user, generating a salt and encrypting his password. Then creates a RabbitMQ user if needed and sets permissions. """ email = email.lower() user = User(email=email, username=username, admin=admin) user.salt = os.urandom(14).encode('base_64') user.secret_hash = hash_password(password=password, salt=user.salt) if management_api is not None: # Creating the appropriate rabbitmq user if it doesn't already # exist. try: management_api.user(username) except management_api.exception: management_api.create_user(username=username, password=password) read_perms = '^(queue/{0}/.*|exchange/.*)'.format(username) write_conf_perms = '^(queue/{0}/.*|exchange/{0}/.*)'.format(username) management_api.set_permission(username=username, vhost='/', read=read_perms, configure=write_conf_perms, write=write_conf_perms) db_session.add(user) db_session.commit() return user
def post(self): # 获取request的json并新建一个用户 data = request.get_json() address = self.object(data) db_session.add(address) db_session.commit() return 'ok', 'basic'
def create_new_tag(dish_id, rest_id, tag_id): """Add more tags""" rest_dish = Rest_Dish(dish_id=dish_id, rest_id=rest_id) db_session.add(rest_dish) db_session.commit() dish_tag = Dish_Tag(dish_id=dish_id, tag_id=tag_id, rest_dish_id=rest_dish.id) db_session.add(dish_tag) db_session.commit()
def seed_restaurants(): """Seed restaurants with test data.""" r1 = Restaurant(name="Chef Zhao's") r2 = Restaurant(name="Fu Lam Mum") r3 = Restaurant(name="Chef Chu's") r4 = Restaurant(name="Queen House") r5 = Restaurant(name="Cafe Macs") db_session.add(r1) db_session.add(r2) db_session.add(r3) db_session.add(r4) db_session.commit()
def new_user(email, admin=False): """Initializes a new user, generating a salt and encrypting his password. Then creates a RabbitMQ user if needed and sets permissions. """ email = email.lower() user = User(email=email, admin=admin) db_session.add(user) db_session.commit() return user
def seed_users(): """Seed users with test users.""" emilypass = os.environ.get("EMILYPASS") emily = User(username='******') emily.set_password(emilypass) nickpass = os.environ.get("NICKPASS") nick = User(username="******") nick.set_password(nickpass) db_session.add(emily) db_session.add(nick) db_session.commit()
def check_group(bot, update, new, left): id = left.id if left != None else new[0].id opr = 'out' if left != None else 'in' usr = User.query.filter(User.id==id).first() if usr == None: return #prm = Parametr.query.filter(Parametr.parametr=='Tokens_left').first() usr.group = True if opr=='in' else False #usr.tokens_acc = usr.tokens_acc+1 if opr=='in' else usr.tokens_acc-1 #prm.value_flt = prm.value_flt-1 if opr=='in' else prm.value_flt+1 db_session.commit() text = 'You entered to the chat' if opr=='in' else 'You left the chat' bot.send_message(chat_id=id, text=text) check_bonus(usr, bot)
def seed_images(): with open("seeds/images_seed.txt") as f: reader = csv.reader(f, delimiter="|") for row in reader: dish_id = row[0] filename = row[1].strip() image = Image(dish_id=dish_id, filename=filename) db_session.add(image) try: db_session.commit() except sqlalchemy.exc.IntegrityError, e: db_session.rollback()
def seed_tags(): """Seed tags with test data.""" tags = [ Tag(name="spicy"), Tag(name="chicken"), Tag(name="pork"), Tag(name="beef"), Tag(name="vegetarian"), Tag(name="fish") ] for tag in tags: db_session.add(tag) db_session.commit()
def create_user(username, password, password_verify): if password != password_verify: return "Error: Passwords do not match." user = db_session.query(User).filter_by(username=username) if user: return "Error: User already exists." user = User(username=username) user.set_password(password) db_session.add(user) db_session.commit() return "User created."
def change_password(self, new_password, management_api): """"Changes" a user's password by deleting his rabbitmq account and recreating it with the new password. """ try: management_api.delete_user(self.username) except management_api.exception: pass management_api.create_user(username=self.username, password=new_password) management_api.set_permission(username=self.username, vhost='/', read='.*', configure='.*', write='.*') self.salt = os.urandom(14).encode('base_64') self.secret_hash = hash_password(password=new_password, salt=self.salt) db_session.add(self) db_session.commit()
def check_bonus(usr, bot): bonus = 25 prm = Parametr.query.filter(Parametr.parametr=='Tokens_left').first() if usr.vallet != '' and usr.facebook != '' and usr.twitter != '' and usr.email != '' and usr.channel: print(usr.id, 'Bonus!') if not usr.bonus: usr.tokens_acc += bonus prm.value_flt -= bonus usr.bonus = True bot.send_message(chat_id=usr.id, text='+ %s tokens for settings ' % bonus) db_session.commit() else: if usr.bonus: usr.tokens_acc -= bonus prm.value_flt += bonus usr.bonus = False bot.send_message(chat_id=usr.id, text='+ %s tokens for settings ' % bonus) db_session.commit()
def seed_food_words(): """Seed food_words table with common food words.""" with open('seeds/food_words_seed.txt', 'rb') as f: reader = csv.reader(f, delimiter='|') for row in reader: for i in range(len(row)): row[i] = row[i].decode('utf-8') row[i] = row[i].strip() simplified = row[0] english = row[1].lower() food_word = Food_Word(simplified=simplified, english=english) db_session.add(food_word) try: db_session.commit() except sqlalchemy.exc.IntegrityError, e: db_session.rollback()
def check_channel(bot, job): allusers = User.query.filter().all() #prm = Parametr.query.filter(Parametr.parametr=='Tokens_left').first() for usr in allusers: usr_tele = bot.get_chat_member(-1001325975220, user_id=usr.id, timeout=10) if usr_tele.status == 'left' and usr.channel: #usr.tokens_acc -= 1 #prm.value_flt += 1 usr.channel = False bot.send_message(chat_id=usr.id, text='you unsubscribed from the channel') db_session.commit() check_bonus(usr, bot) elif usr_tele.status != 'left' and not usr.channel: #usr.tokens_acc += 1 #prm.value_flt -= 1 usr.channel = True bot.send_message(chat_id=usr.id, text='you subscribed to the channel') db_session.commit() check_bonus(usr, bot)
def seed_dishes(): """Seed dishes table with common dish names from Dianping and others.""" with open('seeds/dishes_seed.txt', 'rb') as f: reader = csv.reader(f, delimiter='|') for row in reader: for i in range(len(row)): row[i] = row[i].decode('utf-8') row[i] = row[i].strip() chin_name = row[0] eng_name = row[1].lower() if len(row) > 2: desc = row[2] dish = Dish(chin_name=chin_name, eng_name=eng_name, desc=desc) else: dish = Dish(chin_name=chin_name, eng_name=eng_name) db_session.add(dish) try: db_session.commit() except sqlalchemy.exc.IntegrityError, e: db_session.rollback()
def add_review(user_id, dish_id, rest_id, tag_id, text): now = datetime.datetime.now() rest_dish = Rest_Dish(dish_id=dish_id, rest_id=rest_id) db_session.add(rest_dish) db_session.commit() dish_tag = Dish_Tag(dish_id=dish_id, tag_id=tag_id, rest_dish_id=rest_dish.id) db_session.add(dish_tag) db_session.commit() review = Review(user_id=user_id, rest_dish_id=rest_dish.id, dish_id=dish_id, text=text, date=now) db_session.add(review) db_session.commit()
def msg_handler(bot, update): try: id = update.message.chat.id print('Chat ID', id) except: id = update.channel_post.chat.id print('Channel ID', id) if id < 0: return usr = User.query.filter(User.id==id).first() if usr == None: update.message.reply_text('Account not found, do /start') return msg = update.message.text if msg == 'Settings': keyboard = [[InlineKeyboardButton("Email", callback_data='set_email')], [InlineKeyboardButton("ETH Wallet", callback_data='set_wallet')], [InlineKeyboardButton("Facebook", callback_data='set_fb')], [InlineKeyboardButton("Twitter", callback_data='set_twit')]] markup = InlineKeyboardMarkup(keyboard) bot.send_message(chat_id=id, reply_markup=markup, text='Set:') usr.cmd = '' db_session.commit() elif msg == 'Status': res_left = Parametr.query.filter(Parametr.parametr=='Tokens_left').first().value_flt res = 'Amount: ' + str(usr.tokens_acc) + '\nAvailable: ' + str(res_left) + '\n' res += 20 * '-' res += '\nEmail: ' + usr.email + '\nETH wallet (ERC 20): ' + usr.vallet + '\nFacebook: ' + usr.facebook + '\nTwitter: ' + usr.twitter update.message.reply_text(res) usr.cmd = '' db_session.commit() elif msg == 'Referral link': url = 'https://t.me/Airdrop_Coin_bot?start=' + str(id) text = 'Invite your friends by link: ' update.message.reply_text(text) update.message.reply_text(url) usr.cmd = '' db_session.commit() elif msg == 'Media': text = '''Website:\nhttps://cryptotradeline.org/\n\nTelegram channel:\nhttps://t.me/CryptoTradeLine\nFacebook:\nhttps://www.facebook.com/Cryptotradeline/\nTwitter:\nhttps://twitter.com/cryptotradeline\nReddit:\nhttps://www.reddit.com/user/cryptotradeline\nMedium:\nhttps://medium.com/@cryptotradeline\nBitcointalk:\nhttps://bitcointalk.org/index.php?topic=4609704.new#new\nYoutube:\nhttps://www.youtube.com/channel/UC0THTvP7FzJIuSrFoGZV8Dw''' update.message.reply_text(text=text) usr.cmd = '' db_session.commit() elif msg == 'Chat': text = 'En chat: ' + 'https://t.me/joinchat/G_IwyRIqIyZPdYSudBbhcw' + ' \n' text += 'Ru chat: ' + 'https://t.me/joinchat/G_IwyRG5Xa0C-N7l9N8vcQ' update.message.reply_text(text=text) usr.cmd = '' db_session.commit() else: if usr.cmd == 'email': usr.email = msg elif usr.cmd == 'wallet': usr.vallet = msg elif usr.cmd == 'fb': usr.facebook = msg elif usr.cmd == 'twit': usr.twitter = msg else: usr.cmd = '' db_session.commit() update.message.reply_text('Unknown command') return 0 usr.cmd = '' db_session.commit() update.message.reply_text('Done') check_bonus(usr, bot)
def set_twit(bot, update): cur_id = update.callback_query.message.chat.id bot.send_message(chat_id=cur_id, text='Enter your twitter link') usr = User.query.filter(User.id==cur_id).first() usr.cmd = 'twit' db_session.commit()
def set_wallet(bot, update): cur_id = update.callback_query.message.chat.id bot.send_message(chat_id=cur_id, text='Enter your ETH wallet(ERC 20)') usr = User.query.filter(User.id==cur_id).first() usr.cmd = 'wallet' db_session.commit()