Last active
October 25, 2016 14:25
-
-
Save mie00/10f136face5f801c2b33c9adaebdc67a 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
#!/usr/bin/env python3 | |
from twx.botapi import TelegramBot, send_message | |
import time | |
d = {} | |
dr = {} | |
bot = TelegramBot('bot-token') | |
bot.update_bot_info().wait() | |
updates = bot.get_updates().wait() | |
user_id = None | |
lastid = updates[-1].update_id + 1 if updates else None | |
while True: | |
for update in updates: | |
try: | |
print(update) | |
if not update.message or not update.message.text: | |
continue | |
thing = ' '.join(update.message.text.split(' ')[1:]) | |
if thing == '': | |
thing = getattr(update.message.chat, 'title', getattr(update.message.chat, 'username', '')) | |
if 'help' in update.message.text.split(' ')[0]: | |
bot.send_message(update.message.chat.id, '`/lock [resource]` to lock resource\n`/unlock [resource]` to unlock it\n`/funlock [resource]` to force unlock it\n`/list` to list locks\n`/where` for the ip of the bot\n`/who` for the source code of the bot', parse_mode="Markdown") | |
elif 'list' in update.message.text.split(' ')[0]: | |
for k,v in d.items(): | |
bot.send_message(update.message.chat.id, '%s: %s' % (k, v)) | |
elif 'unlock' in update.message.text.split(' ')[0]: | |
if thing not in d: | |
bot.send_message(update.message.chat.id, '`%s` is not locked' % (thing), parse_mode="Markdown") | |
elif d[thing] == update.message.sender.username or 'funlock' in update.message.text.split(' ')[0]: | |
del d[thing] | |
bot.send_message(update.message.chat.id, '`%s` is not locked anymore\n[%s]' % (thing, ', '.join('@%s' % i for i in dr[thing])), parse_mode="Markdown") | |
del dr[thing] | |
else: | |
bot.send_message(update.message.chat.id, '`%s` is locked by @%s, he has to unlock it himself' % (thing, d[thing]), parse_mode="Markdown") | |
elif 'lock' in update.message.text.split(' ')[0]: | |
if thing in d: | |
if d[thing] == update.message.sender.username: | |
bot.send_message(update.message.chat.id, "@%s: you are already locking `%s`" % (d[thing], thing), parse_mode="Markdown") | |
else: | |
bot.send_message(update.message.chat.id, "`%s` is locked by @%s" % (thing, d[thing]), parse_mode="Markdown") | |
dr[thing].append(update.message.sender.username) | |
else: | |
d[thing] = update.message.sender.username | |
dr[thing] = [] | |
bot.send_message(update.message.chat.id, '@%s is now locking `%s`' % (d[thing], thing), parse_mode="Markdown") | |
elif 'where' in update.message.text.split(' ')[0]: | |
bot.send_message(update.message.chat.id, requests.get('https://ifconfig.co', headers={'User-Agent': 'curl/7.47.0'}).text.strip(), parse_mode="Markdown") | |
elif 'who' in update.message.text.split(' ')[0]: | |
bot.send_message(update.message.chat.id, 'https://gist.github.com/mie00/10f136face5f801c2b33c9adaebdc67a', parse_mode="Markdown") | |
except: | |
pass | |
time.sleep(1) | |
lastid = updates[-1].update_id + 1 if updates else lastid | |
if lastid: | |
updates = bot.get_updates(offset=lastid).wait() | |
else: | |
updates = bot.get_updates().wait() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment