def broadcast(message): trans = tw.get_translation(message) if trans == 1: return if message.from_user.username == config.owner_username: try: conn = sqlite3.connect('data.db') curs = conn.cursor() curs.execute('SELECT chat_id FROM chats') rows = curs.fetchall() conn.close() msg = bot.send_message(chat_id=message.chat.id, text=trans['admin']['broadcast']['start']) i = 0 for row in rows: if not row[0] > 0: bot.send_message(chat_id=row[0], text=message.text.split(' ', 1)[1]) i += 1 bot.edit_message_text( chat_id=msg.chat.id, message_id=msg.message_id, text=trans['admin']['broadcast']['process'].format( count=str(i))) bot.edit_message_text( chat_id=msg.chat.id, message_id=msg.message_id, text=trans['admin']['broadcast']['end'].format(count=str(i))) except Exception as e: print(e)
def delnote(message): trans = tw.get_translation(message) if trans == 1: return try: member = bot.get_chat_member(chat_id=message.chat.id, user_id=message.from_user.id) if member.status == 'creator' or member.status == 'administrator': words = message.text.split() name = words[1] conn = sqlite3.connect('data.db') curs = conn.cursor() cmd = """ DELETE FROM notes WHERE name = ? AND chat_id = ?""" curs.execute(cmd, (name, message.chat.id)) conn.commit() conn.close() bot.reply_to(message, trans['note']['delnote']) else: bot.reply_to(message, trans['global']['errors']['admin']) except Exception: bot.reply_to(message, trans['global']['errors']['default'])
def demote(message): trans = tw.get_translation(message) if trans == 1: return try: member = bot.get_chat_member(chat_id=message.chat.id, user_id=message.from_user.id) if member.status == 'creator' or member.status == 'administrator': bot.promote_chat_member( chat_id=message.chat.id, user_id=message.reply_to_message.from_user.id, can_pin_messages=False, can_change_info=False, can_invite_users=False, can_delete_messages=False, can_promote_members=False, can_restrict_members=False) bot.send_message( chat_id=message.chat.id, text=trans['perms']['demote'].format( username=str(message.reply_to_message.from_user.username))) else: bot.reply_to(message, trans['global']['errors']['admin']) except Exception: bot.reply_to(message, trans['global']['errors']['default'])
def unmute(message): trans = tw.get_translation(message) if trans == 1: return try: member = bot.get_chat_member(chat_id=message.chat.id, user_id=message.from_user.id) if member.status == 'creator' or member.status == 'administrator': chat = bot.get_chat(chat_id=message.chat.id) perms = chat.permissions bot.restrict_chat_member(chat_id=message.chat.id, user_id=message.reply_to_message.from_user.id, can_send_messages=True, can_send_media_messages=perms.can_send_media_messages, can_send_polls=perms.can_send_polls, can_send_other_messages=perms.can_send_other_messages, can_add_web_page_previews=perms.can_add_web_page_previews, until_date=0) bot.send_message(chat_id=message.chat.id, text=trans['mute']['unmute'].format(username=str(message.reply_to_message.from_user.username))) else: bot.reply_to(message, trans['global']['errors']['admin']) except Exception: bot.reply_to(message, trans['global']['errors']['default'])
def purge(message): trans = tw.get_translation(message) if trans == 1: return try: member = bot.get_chat_member(chat_id=message.chat.id, user_id=message.from_user.id) if member.status == 'creator' or member.status == 'administrator': start = message.reply_to_message.message_id end = message.message_id + 1 chat_id = message.chat.id msgs = [] for i in range(start, end): msgs.append(i) if len(msgs) == 100: del_msgs(msgs, chat_id) msgs = [] del_msgs(msgs, chat_id) sent_msg = bot.send_message(chat_id=chat_id, text=trans['messages']['purge']) sleep(5) bot.delete_message(chat_id=chat_id, message_id=sent_msg.message_id) except Exception: bot.reply_to(message, trans['global']['errors']['default'])
def notes(message): trans = tw.get_translation(message) if trans == 1: return try: cmd = """ SELECT name FROM notes WHERE chat_id = ?""" conn = sqlite3.connect('data.db') curs = conn.cursor() curs.execute(cmd, (message.chat.id, )) rows = curs.fetchall() conn.close() text = '┏━━━━━━━━━━━━━━━━━━━\n┣' + trans['note']['notes'][ 'list'] + '\n┃\n' for row in rows: text += '┣[' text += row[0] text += '\n' text += '┗━━━━━━━━━━━━━━━━━━━\n' text += trans['note']['notes']['instruction'] bot.reply_to(message, text) except Exception: bot.reply_to(message, trans['global']['errors']['default'])
def addnote(message): trans = tw.get_translation(message) if trans == 1: return try: member = bot.get_chat_member(chat_id=message.chat.id, user_id=message.from_user.id) if member.status == 'creator' or member.status == 'administrator': words = message.text.split() name = words[1] conn = sqlite3.connect('data.db') curs = conn.cursor() cmd = """ INSERT INTO notes(name, message_id, chat_id) VALUES(?,?,?) """ params = (name, message.reply_to_message.message_id, message.chat.id) curs.execute(cmd, params) conn.commit() conn.close() bot.reply_to(message, trans['note']['addnote']) else: bot.reply_to(message, trans['global']['errors']['admin']) except Exception: bot.reply_to(message, trans['global']['errors']['default'])
def weather(message): trans = tw.get_translation(message) if trans == 1: return try: words = message.text.split() city_name = words[1] loc = geolocator.geocode(city_name) if loc is None: bot.reply_to(message, trans['weather']['city_not_found_err']) else: weather_message = bot.send_message(chat_id=message.chat.id, text=trans['weather']['making_forecast'], reply_to_message_id=message.message_id) global weathers weathers[weather_message.message_id] = message.from_user.id response = requests.get('https://api.openweathermap.org/data/2.5/onecall?lat=' + str(loc.latitude) + '&lon=' + str(loc.longitude) + '&appid=c1c0032b6ff3be83e44ab641e780fc3d&lang=RU' + '&units=metric') data = json.loads(response.content) destination = loc.address.split(',') dest = '' for i in destination: if i == destination[0]: dest += i else: dest += ',' + i text = trans['weather']['weather_in'].format(city=dest) + '\n' text += '━━━━━━━━━━━━━━━━━━━━\n' text += trans['weather']['weather']['current_weather'] + '\n' text += '━━━━━━━━━━━━━━━━━━━━\n' text += '<b>' + str(data['current']['temp']) + ' °C <i>' + data['current']['weather'][0][ 'description'].capitalize() + '</i></b>\n' text += trans['weather']['weather']['feels_like'].format( feels_like=str(data['current']['feels_like'])) + '\n' text += trans['weather']['humidity'].format(humidity=str(data['current']['humidity'])) + '\n' text += trans['weather']['pressure'].format(pressure=str(data['current']['pressure'])) + '\n' text += trans['weather']['wind_speed'].format(wind_speed=str(data['current']['wind_speed'])) + '\n' text += trans['weather']['cloudiness'].format(cloudiness=str(data['current']['clouds'])) + '\n' text += trans['weather']['uvi'].format(uvi=str(data['current']['uvi'])) keyboard = types.InlineKeyboardMarkup() key_close = types.InlineKeyboardButton(text=trans['weather']['close_button'], callback_data='weather_close') keyboard.add(key_close) bot.edit_message_text(chat_id=message.chat.id, message_id=weather_message.message_id, text=text, parse_mode='HTML', reply_markup=keyboard) except Exception: bot.reply_to(message, trans['global']['errors']['default'])
def call_handler(call): trans = tw.get_translation(call) try: conn = sqlite3.connect('data.db') curs = conn.cursor() member = bot.get_chat_member(chat_id=call.message.chat.id, user_id=call.message.from_user.id) if member.status == 'creator' or member.status == 'administrator': curs.execute( """UPDATE chats SET language = ?, setup_is_finished = ? WHERE chat_id = ?""", (call.data[5:], 1, call.message.chat.id)) conn.commit() conn.close() trans = tw.get_translation(call) bot.answer_callback_query(callback_query_id=call.id, text=trans['lang_set']) keyboard = types.InlineKeyboardMarkup(row_width=2) for i in tw.available: name = i.split('.')[0] keyboard.add( types.InlineKeyboardButton(text=tw.get_labels()[name], callback_data=f'lang_{name}')) bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text=trans['introduction']['start'], reply_markup=keyboard) else: bot.answer_callback_query(callback_query_id=call.id, text=trans['global']['errors']['admin']) except Exception: bot.answer_callback_query(callback_query_id=call.id, text=trans['global']['errors']['default'])
def banme(message): trans = tw.get_translation(message) if trans == 1: return try: bot.kick_chat_member(chat_id=message.chat.id, user_id=message.from_user.id, until_date=0) bot.send_message(chat_id=message.chat.id, text=trans['ban']['ban'].format( username=str(message.from_user.username))) except Exception: bot.reply_to(message, trans['global']['errors']['default'])
def button_callback_handler(call): trans = tw.get_translation(call) try: if call.data == 'captcha_ok': greeting.call_handler(call) if 'forecast' in call.data or 'weather' in call.data: weather.call_handler(call) if 'lang' in call.data: introduction.call_handler(call) except Exception: bot.answer_callback_query(callback_query_id=call.id, text=trans['global']['errors']['default'])
def help(message): trans = tw.get_translation(message) if trans == 1: return conn = sqlite3.connect('data.db') curs = conn.cursor() curs.execute( """SELECT language FROM chats WHERE chat_id = ? AND setup_is_finished = ?""", (message.chat.id, 1)) rows = curs.fetchall() conn.close() if not rows: text = trans['global']['errors']['setup'] else: text = trans['introduction']['help'] bot.send_message(chat_id=message.chat.id, text=text)
def start(message): conn = sqlite3.connect('data.db') curs = conn.cursor() curs.execute('SELECT chat_id FROM chats') chats = curs.fetchall() try: if (message.chat.id, ) in chats: return except IndexError: pass curs.execute('INSERT INTO chats(chat_id, setup_is_finished) VALUES(?,?)', (message.chat.id, 0)) conn.commit() keyboard = types.InlineKeyboardMarkup(row_width=2) for i in tw.available: name = i.split('.')[0] keyboard.add( types.InlineKeyboardButton(text=tw.get_labels()[name], callback_data=f'lang_{name}')) # key_close = types.InlineKeyboardButton(text='Next >>', callback_data='setup_next') # keyboard.add(key_close) if 'eng.json' in tw.available: curs.execute( """UPDATE chats SET language = ?, setup_is_finished = ? WHERE chat_id = ?""", ('eng', 1, message.chat.id)) else: curs.execute( """UPDATE chats SET language = ?, setup_is_finished = ? WHERE chat_id = ?""", (tw.available[0].split('.')[0], 1, message.chat.id)) conn.commit() conn.close() trans = tw.get_translation(message) bot.send_message(message.chat.id, trans['introduction']['start'], reply_markup=keyboard)
def ban(message): trans = tw.get_translation(message) if trans == 1: return try: member = bot.get_chat_member(chat_id=message.chat.id, user_id=message.from_user.id) if member.status == 'creator' or member.status == 'administrator': bot.kick_chat_member(chat_id=message.chat.id, user_id=message.reply_to_message.from_user.id, until_date=0) bot.send_message( chat_id=message.chat.id, text=trans['ban']['ban'].format( username=str(message.reply_to_message.from_user.username))) else: bot.reply_to(message, trans['global']['errors']['admin']) except Exception: bot.reply_to(message, trans['global']['errors']['default'])
def note(message): trans = tw.get_translation(message) if trans == 1: return try: words = message.text.split() name = words[1] conn = sqlite3.connect('data.db') curs = conn.cursor() cmd = """ SELECT message_id FROM notes WHERE name = ? AND chat_id = ?""" curs.execute(cmd, (name, message.chat.id)) rows = curs.fetchall() conn.close() row = rows[0] bot.forward_message(message.chat.id, message.chat.id, row[0]) except Exception: bot.reply_to(message, trans['global']['errors']['default'])
def tr(message): trans = tw.get_translation(message) if trans == 1: return try: words = message.text.split() lang_code = words[1] result = translator.translate(message.reply_to_message.text, dest=lang_code) langs = googletrans.LANGUAGES text = '<i>' if trans['translate']['tr'] != '': text += trans['translate']['tr'].format( src_lang=langs[result.src], dest_lang=langs[lang_code]) + '\n' text += 'Translate from <b>' + langs[result.src] + '</b> to <b>' + langs[lang_code] + '</b></i>\n\n' + \ result.text bot.send_message(chat_id=message.chat.id, reply_to_message_id=message.message_id, parse_mode='HTML', text=text) except Exception: bot.reply_to(message, trans['global']['errors']['default'])
def forecast(message): trans = tw.get_translation(message) if trans == 1: return try: words = message.text.split() city_name = words[1] loc = geolocator.geocode(city_name) if loc is None: bot.reply_to(message, trans['weather']['city_not_found_err']) else: forecast_message = bot.send_message(chat_id=message.chat.id, text=trans['weather']['making_forecast'], reply_to_message_id=message.message_id) global forecasts forecasts[forecast_message.message_id] = [] response = requests.get('https://api.openweathermap.org/data/2.5/onecall?lat=' + str(loc.latitude) + '&lon=' + str(loc.longitude) + '&appid=c1c0032b6ff3be83e44ab641e780fc3d&lang=RU' + '&units=metric') data = json.loads(response.content) destination = loc.address.split(',') for i in range(8): dest = '' for j in destination: if j == destination[0]: dest += j else: dest += ',' + j text = trans['weather']['weather_in'].format(city=dest) + '\n' text += '━━━━━━━━━━━━━━━━━━━━\n' text += trans['weather']['forecast']['forecast_for'].format(time=time.strftime("%d/%m", time.gmtime(data['daily'][i]['dt']))) + '\n' text += '━━━━━━━━━━━━━━━━━━━━\n' text += '<b>' + str(data['daily'][i]['temp']['day']) + ' °C <i>' + data['daily'][i]['weather'][0][ 'description'].capitalize() + '</i></b>\n' text += trans['weather']['forecast']['min_temp'].format( min_temp=str(data['daily'][i]['temp']['min']))+'\n' text += trans['weather']['forecast']['max_temp'].format( max_temp=str(data['daily'][i]['temp']['max']))+'\n' text += trans['weather']['forecast']['morn_temp'].format( morn_temp=str(data['daily'][i]['temp']['morn']))+'\n' text += trans['weather']['forecast']['eve_temp'].format( eve_temp=str(data['daily'][i]['temp']['eve']))+'\n' text += trans['weather']['forecast']['night_temp'].format( night_temp=str(data['daily'][i]['temp']['night']))+'\n' text += trans['weather']['humidity'].format(humidity=str(data['current']['humidity'])) + '\n' text += trans['weather']['pressure'].format(pressure=str(data['current']['pressure'])) + '\n' text += trans['weather']['wind_speed'].format(wind_speed=str(data['current']['wind_speed'])) + '\n' text += trans['weather']['cloudiness'].format(cloudiness=str(data['current']['clouds'])) + '\n' text += trans['weather']['uvi'].format(uvi=str(data['current']['uvi'])) forecasts[forecast_message.message_id].append(text) forecasts[forecast_message.message_id].append(0) forecasts[forecast_message.message_id].append(message.from_user.id) keyboard = types.InlineKeyboardMarkup(row_width=2) key_prev = types.InlineKeyboardButton(text='<<', callback_data='forecast_prev') key_next = types.InlineKeyboardButton(text='>>', callback_data='forecast_next') keyboard.add(key_prev, key_next) key_close = types.InlineKeyboardButton(text=trans['weather']['close_button'], callback_data='forecast_close') keyboard.add(key_close) bot.edit_message_text(chat_id=message.chat.id, message_id=forecast_message.message_id, text=forecasts[forecast_message.message_id][0], parse_mode='HTML', reply_markup=keyboard) except Exception: bot.reply_to(message, trans['global']['errors']['default'])
def call_handler(call): trans = tw.get_translation(call) if trans == 1: return if call.data == 'forecast_prev': if call.from_user.id == forecasts[call.message.message_id][9]: if not forecasts[call.message.message_id][8] <= 0: forecasts[call.message.message_id][8] -= 1 keyboard = types.InlineKeyboardMarkup(row_width=2) key_prev = types.InlineKeyboardButton(text='<<', callback_data='forecast_prev') key_next = types.InlineKeyboardButton(text='>>', callback_data='forecast_next') keyboard.add(key_prev, key_next) key_close = types.InlineKeyboardButton(text=trans['weather']['close_button'], callback_data='forecast_close') keyboard.add(key_close) bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text=forecasts[call.message.message_id][forecasts[call.message.message_id][8]], parse_mode='HTML', reply_markup=keyboard) else: bot.answer_callback_query(callback_query_id=call.id, text=trans['weather']['forecast']['start_of_list']) else: bot.answer_callback_query(callback_query_id=call.id, text=trans['weather']['forecast']['other_user_err']) elif call.data == 'forecast_next': if call.from_user.id == forecasts[call.message.message_id][9]: if not forecasts[call.message.message_id][8] >= 7: forecasts[call.message.message_id][8] += 1 keyboard = types.InlineKeyboardMarkup(row_width=2) key_prev = types.InlineKeyboardButton(text='<<', callback_data='forecast_prev') key_next = types.InlineKeyboardButton(text='>>', callback_data='forecast_next') keyboard.add(key_prev, key_next) key_close = types.InlineKeyboardButton(text=trans['weather']['close_button'], callback_data='forecast_close') keyboard.add(key_close) bot.edit_message_text(chat_id=call.message.chat.id, message_id=call.message.message_id, text=forecasts[call.message.message_id][forecasts[call.message.message_id][8]], parse_mode='HTML', reply_markup=keyboard) else: bot.answer_callback_query(callback_query_id=call.id, text=trans['weather']['forecast']['end_of_list']) else: bot.answer_callback_query(callback_query_id=call.id, text=trans['weather']['forecast']['other_user_err']) elif call.data == 'forecast_close': if call.from_user.id == forecasts[call.message.message_id][9]: forecasts.pop(call.message.message_id) bot.delete_message(chat_id=call.message.chat.id, message_id=call.message.message_id) else: bot.answer_callback_query(callback_query_id=call.id, text=trans['weather']['forecast']['other_user_err']) elif call.data == 'weather_close': if call.from_user.id == weathers[call.message.message_id]: weathers.pop(call.message.message_id) bot.delete_message(chat_id=call.message.chat.id, message_id=call.message.message_id) else: bot.answer_callback_query(callback_query_id=call.id, text=trans['weather']['forecast']['other_user_err'])
def mute(message): trans = tw.get_translation(message) if trans == 1: return try: member = bot.get_chat_member(chat_id=message.chat.id, user_id=message.from_user.id) if member.status == 'creator' or member.status == 'administrator': if len(message.text.split()) > 1: words = message.text.split() timeout = words[1] timeout_units = timeout[-1:] timeout_numbers = timeout[:-1] if timeout_units == 's' and int(timeout_numbers) < 30: bot.send_message(chat_id=message.chat.id, text=trans['mute']['tmute_too_few']) return final_timeout = None timeout_text = None if str(type(tw.get_translation(message)['global']['time']['seconds'])) == "<class 'list'>": if timeout_units == 's': final_timeout = int(timeout_numbers) if int(timeout_numbers[-1:]) == 1: text = trans['global']['time']['seconds'][0] elif 2 <= int(timeout_numbers) <= 4: text = trans['global']['time']['seconds'][1] else: text = trans['global']['time']['seconds'][2] timeout_text = timeout_numbers + ' ' + text elif timeout_units == 'm': final_timeout = int(timeout_numbers) * 60 if int(timeout_numbers[-1:]) == 1: text = trans['global']['time']['minutes'][0] elif 2 <= int(timeout_numbers) <= 4: text = trans['global']['time']['minutes'][1] else: text = trans['global']['time']['minutes'][2] timeout_text = timeout_numbers + ' ' + text elif timeout_units == 'h': final_timeout = int(timeout_numbers) * 3600 if int(timeout_numbers) == 1: text = trans['global']['time']['hours'][0] elif 2 <= int(timeout_numbers) <= 4: text = trans['global']['time']['hours'][1] else: text = trans['global']['time']['hours'][2] timeout_text = timeout_numbers + ' ' + text elif timeout_units == 'd': final_timeout = int(timeout_numbers) * 86400 if int(timeout_numbers) == 1: text = trans['global']['time']['days'][0] elif 2 <= int(timeout_numbers[-1:]) <= 4: text = trans['global']['time']['days'][1] else: text = trans['global']['time']['days'][2] timeout_text = timeout_numbers + ' ' + text else: if timeout_units == 's': final_timeout = int(timeout_numbers) text = trans['global']['time']['seconds'] timeout_text = timeout_numbers + ' ' + text elif timeout_units == 'm': final_timeout = int(timeout_numbers) * 60 text = trans['global']['time']['minutes'] timeout_text = timeout_numbers + ' ' + text elif timeout_units == 'h': final_timeout = int(timeout_numbers) * 3600 text = trans['global']['time']['hours'] timeout_text = timeout_numbers + ' ' + text elif timeout_units == 'd': final_timeout = int(timeout_numbers) * 86400 text = trans['global']['time']['days'] timeout_text = timeout_numbers + ' ' + text bot.restrict_chat_member(chat_id=message.chat.id, user_id=message.reply_to_message.from_user.id, can_send_messages=False, until_date=int(time.time()) + final_timeout) bot.send_message(chat_id=message.chat.id, text=trans['mute']['tmute'].format( username=str(message.reply_to_message.from_user.username), time=timeout_text)) else: bot.restrict_chat_member(chat_id=message.chat.id, user_id=message.reply_to_message.from_user.id, can_send_messages=False, until_date=0) bot.send_message(chat_id=message.chat.id, text=trans['mute']['mute'].format(username=str(message.reply_to_message.from_user.username))) else: bot.reply_to(message, trans['global']['errors']['admin']) except Exception: bot.reply_to(message, trans['global']['errors']['default'])