-
-
Save raku-cat/6e6fd2d57cb77aec3f7439d21c1a75c8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import telepot, telepot.aio | |
import asyncio, aiofiles, aiohttp | |
import json | |
import datetime | |
from datetime import datetime, timedelta | |
import time | |
import os | |
import random | |
import regex | |
from telepot.aio.loop import MessageLoop | |
from threading import Lock | |
with open(sys.path[0] + '/keys.json', 'r') as f: | |
key = json.load(f) | |
bot = telepot.aio.Bot(key['telegram']) | |
lock = Lock() | |
startime = time.time() | |
interv = [] | |
async def ad_roll(): | |
adnum = 0 | |
while 1: | |
async with aiofiles.open('keys.json', 'r') as k: | |
interval = json.loads(await k.read())['interval'] | |
async with aiofiles.open('ads.json', 'r') as a: | |
ad = json.loads(await a.read())['ads'] | |
if adnum > len(ad): | |
adnum = 0 | |
await bot.sendMessage(-1001132937659, ad[adnum]) | |
dt = datetime.now() + timedelta(minutes=int(interval)) | |
adnum += 1 | |
while datetime.now() < dt: | |
await asyncio.sleep(1) | |
async def on_command(msg): | |
content_type, chat_type, chat_id, msg_date, msg_id = telepot.glance(msg, long=True) | |
try: | |
botcom = msg['entities'][0]['type'] | |
if not botcom == 'bot_command': | |
return | |
except KeyError: | |
pass | |
if content_type == 'text': | |
if chat_type == 'private': | |
from_id = msg['from']['id'] | |
if from_id == 284015590 or 105301944: | |
command = msg['text'].lower() | |
if command.startswith('/addad'): | |
await bot.sendChatAction(chat_id, 'typing') | |
await add_ad(msg) | |
elif command.startswith('/delad'): | |
await bot.sendChatAction(chat_id, 'typing') | |
await del_ad(msg) | |
elif command.startswith('/listads'): | |
await bot.sendChatAction(chat_id, 'typing') | |
await lister(msg) | |
elif command.startswith('/setinterval'): | |
await bot.sendChatAction(chat_id, 'typing') | |
await set_intv(msg) | |
async def add_ad(msg): | |
async with aiofiles.open('ads.json', 'r') as f: | |
adkey = json.loads(await f.read()) | |
content_type, chat_type, chat_id, msg_date, msg_id = telepot.glance(msg, long=True) | |
command = msg['text'].lower() | |
try: | |
new_ad = command.split(' ', 1)[1] | |
except IndexError: | |
return | |
adkey['ads'].append(new_ad) | |
with lock: | |
with open('ads.json', 'w') as f: | |
json.dump(adkey, f, indent=2) | |
await bot.sendMessage(chat_id, 'Ads list updated') | |
async def lister(msg): | |
content_type, chat_type, chat_id, msg_date, msg_id = telepot.glance(msg, long=True) | |
async with aiofiles.open('ads.json', 'r') as f: | |
ads = json.loads(await f.read())['ads'] | |
adlist = list() | |
n = 1 | |
for i in ads: | |
formatt = str(n) + '. ' + i + '\n' | |
adlist.append(formatt) | |
n += 1 | |
adslistf = ''.join(adlist) | |
await bot.sendMessage(chat_id, adslistf, parse_mode='html') | |
async def set_intv(msg): | |
content_type, chat_type, chat_id, msg_date, msg_id = telepot.glance(msg, long=True) | |
command = msg['text'].lower() | |
try: | |
newintv = command.split(' ', 1)[1] | |
except IndexError: | |
return | |
async with aiofiles.open('keys.json') as f: | |
keyfile = json.loads(await f.read()) | |
try: | |
float(newintv) | |
keyfile['interval'] = newintv | |
with open('keys.json', 'w') as f: | |
json.dump(keyfile, f, indent=2) | |
await bot.sendMessage(chat_id, 'Ad roll interval set to ' + newintv + ' hours.') | |
except ValueError: | |
await bot.sendMessage(chat_id, 'Send a number') | |
async def del_ad(msg): | |
content_type, chat_type, chat_id, msg_date, msg_id = telepot.glance(msg, long=True) | |
command = msg['text'].lower() | |
try: | |
adselc = command.split(' ', 1)[1] | |
except IndexError: | |
return | |
try: | |
float(adselc) | |
adn = int(adselc) - 1 | |
except ValueError: | |
return | |
async with aiofiles.open('ads.json', 'r') as f: | |
ads = json.loads(await f.read()) | |
del ads['ads'][adn] | |
with open('ads.json', 'w') as f: | |
json.dump(ads, f, indent=2) | |
await bot.sendMessage(chat_id, 'Ad number ' + str(adn + 1) + ' deleted') | |
loop = asyncio.get_event_loop() | |
loop.create_task(MessageLoop(bot,on_command).run_forever()) | |
cors = asyncio.wait([ad_roll()]) | |
print('Started...') | |
loop.run_until_complete(cors) | |
loop.run_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment