-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexperiments.py
106 lines (68 loc) · 2.64 KB
/
experiments.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import json
import telegram
import logging
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.DEBUG
)
def load_json(filename):
with open(filename, "r") as f:
return json.load(f)
token = load_json("token.json")
bot = telegram.Bot(token=token)
# for u in bot.get_updates():
# bot.send_message(text="in progress", chat_id=u.message.from_user.id)
from telegram.ext import Updater # noqa
updater = Updater(token=token)
dispatcher = updater.dispatcher
# adding dispatcher to react to /start
from telegram import Update # noqa
from telegram.ext import CallbackContext # noqa
def start(update: Update, context: CallbackContext):
context.bot.send_message(
chat_id=update.effective_chat.id,
text="This is the UnifestBestellBot for 2022, talk to me!",
)
from telegram.ext import CommandHandler # noqa
start_handler = CommandHandler("start", start)
dispatcher.add_handler(start_handler)
# adding handler to echo all incoming messages
from telegram.ext import MessageHandler, Filters # noqa
def echo(update: Update, context: CallbackContext):
context.bot.send_message(chat_id=update.effective_chat.id, text=update.message.text)
echo_handler = MessageHandler(Filters.text & (~Filters.command), echo)
dispatcher.add_handler(echo_handler)
# adding handler to return stuff in caps
def caps(update: Update, context: CallbackContext):
text_caps = " ".join(context.args).upper()
context.bot.send_message(chat_id=update.effective_chat.id, text=text_caps)
caps_handler = CommandHandler("caps", caps)
dispatcher.add_handler(caps_handler)
# adding inline-handler for caps
from telegram import InlineQueryResultArticle, InputTextMessageContent # noqa
def inline_caps(update: Update, context: CallbackContext):
query = update.inline_query.query
if not query:
return
results = []
results.append(
InlineQueryResultArticle(
id=query.upper(),
title="Caps",
input_message_content=InputTextMessageContent(query.upper()),
)
)
context.bot.answer_inline_query(update.inline_query.id, results)
from telegram.ext import InlineQueryHandler # noqa
inline_caps_handler = InlineQueryHandler(inline_caps)
dispatcher.add_handler(inline_caps_handler)
# unknown commands
def unknown(update: Update, context: CallbackContext):
context.bot.send_message(
chat_id=update.effective_chat.id,
text="Sorry, I didn't understand that command.",
)
unknown_handler = MessageHandler(Filters.command, unknown)
dispatcher.add_handler(unknown_handler)
# begin action loop
updater.start_polling()
updater.idle()