-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbot.py
53 lines (45 loc) · 1.9 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
from telegram import Update
from telegram.ext import Updater, CommandHandler, CallbackContext
from twitter import tweet
from config import token, admin_id
import csv
def tweet_command(update: Update, context: CallbackContext) -> None:
admins = get_admins()
sender = str(update.message.from_user.id)
if (admin_id == int(sender) or admins.__contains__(sender)):
message = update.message.text.removeprefix("/tweet ")
try:
tweet(message)
update.message.reply_text(f'Tweeted: {message}')
except Exception as e:
update.message.reply_text(f'An error occured: {e}')
else:
update.message.reply_text("You are not authorized to use this command.")
def add_command(update: Update, context: CallbackContext) -> None:
if (update.message.from_user.id == admin_id):
message = update.message.text.removeprefix("/add ")
f = open('users.csv','a')
f.write(f'{message}\n')
f.close()
update.message.reply_text("User added to list.")
else:
update.message.reply_text("You are not authorized to use this command.")
def admin_command(update: Update, context: CallbackContext) -> list:
admins = get_admins()
if (len(admins) == 0):
update.message.reply_text("No admins found.")
else:
sarr = [str(a).lstrip("['").rstrip("']") for a in admins]
update.message.reply_text(f'Current admins: {", " . join(sarr)}')
def get_admins() -> list:
with open("users.csv") as file_name:
file_read = csv.reader(file_name)
array = list(file_read)
array = list(map(lambda x: x[0], array))
return array
updater = Updater(token)
updater.dispatcher.add_handler(CommandHandler('tweet', tweet_command))
updater.dispatcher.add_handler(CommandHandler('add', add_command))
updater.dispatcher.add_handler(CommandHandler('admins', admin_command))
updater.start_polling()
updater.idle()