-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.py
188 lines (138 loc) · 7 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# Elizabeth is trying to use the tutorial
# run: $Env:BOT_TOKEN = 'TOKEN_HERE'; python bot.py
import os
import github
from github_functions import search_repos
# some packages are built-in but for others you need to import
# others req installing pip (pip install python-telegram-bot)
from telegram.ext import Updater
from telegram.ext import PicklePersistence, \
CallbackQueryHandler # PicklePersistence: utility class, lets us save data about bot to a file
from github_functions import repo_overview, star_repo, \
watch_repo # Need to import the function: in __init__.py, it imports the function from the file
persistence = PicklePersistence(filename='data')
updater = Updater(token=os.getenv("BOT_TOKEN"), persistence=persistence,
use_context=True) # use_context=True is special for v12 of library; default=False
dispatcher = updater.dispatcher # introduce locally for updater quicker access to dispatcher
import logging # logging module setup to know when/why if things don't work
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
# /start command calls this: returns welcome message
def start(update, context):
context.bot.send_message(chat_id=update.effective_chat.id,
text="Hello. I'm a bot, please talk to me! Type /help for more info.")
def ping(update, context):
context.bot.send_message(chat_id=update.effective_chat.id, text="pong✌😝✌")
def help(update, context):
context.bot.send_message(chat_id=update.effective_chat.id, text="""I will echo back all your statements while I'm running.
Special commands include /start, /help, /caps [x], /ping, /auth [x], /repo [x]""")
from telegram.ext import CommandHandler
start_handler = CommandHandler('start', start)
ping_handler = CommandHandler('ping', ping)
help_handler = CommandHandler('help', help)
dispatcher.add_handler(start_handler)
updater.start_polling()
# listens for regular messages and echoes them; MessageHandler class and Handler subclass
def echo(update, context):
context.bot.send_message(chat_id=update.effective_chat.id, text=update.message.text)
from telegram.ext import MessageHandler, Filters
echo_handler = MessageHandler(Filters.text & (~Filters.command), echo)
dispatcher.add_handler(echo_handler)
# /caps command calls this; takes text and replies in caps
# can receive the arguments (as a list, split on spaces) passed to command in callback function
def caps(update, context):
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)
# /setinline command calls this; bot call on command via inline mode
from telegram import InlineQueryResultArticle, InputTextMessageContent
def inline_caps(update, context):
query = update.inline_query.query
if not query:
return
results = list()
results = [
InlineQueryResultArticle(
id=repo.id,
title=repo.full_name,
input_message_content=InputTextMessageContent(repo_overview(repo, context.user_data['token'])[0]),
reply_markup=InlineKeyboardMarkup(repo_overview(repo, context.user_data['token'])[1]),
url=repo.html_url,
description=repo.description
) for repo in search_repos(context.user_data['token'], query)[:20]
]
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
inline_caps_handler = InlineQueryHandler(inline_caps)
dispatcher.add_handler(inline_caps_handler)
from telegram import InlineKeyboardMarkup
def repo_info(update, context):
if "token" in context.user_data:
desc, keyboard = repo_overview(context.args[0], context.user_data["token"])
if keyboard: # If keyboard exists
reply_markup = InlineKeyboardMarkup(keyboard)
else:
reply_markup = None
context.bot.send_message(chat_id=update.effective_chat.id, text=desc, reply_markup=reply_markup)
else:
context.bot.send_message(chat_id=update.effective_chat.id,
text="Not logged in. Please use the /auth command to log in.")
repo_info_handler = CommandHandler("repo", repo_info)
dispatcher.add_handler(repo_info_handler)
# use MessageHandler with command filter to reply to all unrecognized commands
def button(update, context):
query = update.callback_query
# CallbackQueries need to be answered, even if no notification to the user is needed
# Some clients may have trouble otherwise. See https://core.telegram.org/bots/api#callbackquery
# 'watch_xxxxx (repo id)'
if query.data.startswith('star'):
repo_id = int(query.data.replace('star_', '')) # repo_id = 'xxxxxxx'
# User's auth token will be update.user_data['token']
string, repo_name = star_repo(context.user_data['token'], repo_id)
if query.data.startswith('watch'):
repo_id = int(query.data.replace('watch_', '')) # repo_id = 'xxxxxxx'
# User's auth token will be update.user_data['token']
string, repo_name = watch_repo(context.user_data['token'], repo_id)
query.answer(text="The repo, " + repo_name + " has been " + string)
# another way to do this is f"Selected option: {query.data}"
# query.edit_message_text(text="Selected option: {}".format(query.data))
# selected option: watch_277662457
updater.dispatcher.add_handler(CallbackQueryHandler(button))
def unknown(update, context):
context.bot.send_message(chat_id=update.effective_chat.id, text="Sorry, I didn't understand that command.")
from github import Github
def auth(update, context):
try:
g = Github(context.args[0])
user = g.get_user()
context.bot.send_message(chat_id=update.effective_chat.id,
text="Logged in as " + user.name + "(" + user.login + ")")
context.user_data["token"] = context.args[0]
# /auth will get set to user.context_data['token']
except:
context.bot.send_message(chat_id=update.effective_chat.id, text="Invalid input. Enter token like '/auth token'")
auth_handler = CommandHandler("auth", auth)
dispatcher.add_handler(auth_handler)
unknown_handler = MessageHandler(Filters.command, unknown)
dispatcher.add_handler(unknown_handler)
# added lastly: (if added it could be trigger before CommandHandlers has chance to update)
# to circumvent this, keyword argument group (int) can be passed to add_handler with a value other than 0
# updater.stop()
updater.idle()
# results = list()
# for repo in search_repos(query):
# desc, keyboard = repo_overview()
# results.append(InlineQueryResultArticle) (
# id=repo.id,
# title=repo.name,
# input_message_content=desc
# reply_markup=keyboard
# url
# ))