forked from ozansz/simple-telegram-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
96 lines (66 loc) · 2.6 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
import re
import random
import requests
from lxml import html
from telegram.ext import Updater, CommandHandler
DEBUG = 1
def __debug(msg):
if DEBUG:
print("\033[94m[DEBUG]\033[0m", msg)
def start(bot, update):
__debug("CALL start")
update.message.reply_text(
"Selam! Sana nasıl yardım edebileceğimi öğrenmek için /help yazabilirsin.")
def help(bot, update):
__debug("CALL help")
update.message.reply_text(
"/video_at - sana Youtube’da şu an trend olan videolardan birini atarım.\n"
"/haber_at - sana BBC’de en çok okunan haberlerden birini atarım.\n\n"
"/help - bu yardım mesajını gösterir")
def video_at(bot, update):
__debug("CALL video_at")
__debug(
"video_at: Sending request to https://www.youtube.com/feed/trending")
pg = requests.get("https://www.youtube.com/feed/trending")
__debug("video_at: Got response: %d" % pg.status_code)
if pg.status_code != 200:
update.message.reply_text("Aman! Bir hata oldu :(")
return
video_list = re.findall('href="/watch\?v=\w{11}"', pg.text)
if len(video_list) == 0:
update.message.reply_text("Aman! Bir hata oldu! Hiç video bulamadım :(")
return
update.message.reply_text("https://youtube.com" + random.choice(video_list)[6:-1])
def haber_at(bot, update):
__debug("CALL haber_at")
__debug("haber_at: Sending request to https://www.bbc.com/news")
pg = requests.get("https://www.bbc.com/news")
__debug("haber_at: Got response: %d" % pg.status_code)
if pg.status_code != 200:
update.message.reply_text("Aman! Bir hata oldu :(")
return
tree = html.fromstring(pg.text)
news_list = list()
for i in range(1, 11):
elem = tree.xpath("//li[@data-entityid='most-popular-read-{}']//a".format(i))
if elem != []:
news_list.append(elem[0])
if len(news_list) == 0:
update.message.reply_text("Aman! Bir hata oldu! Hiç haber bulamadım :(")
return
update.message.reply_text("https://www.bbc.com" + random.choice(news_list).attrib["href"])
def main():
__debug("CALL main")
__debug("main: Initializing updater")
updater = Updater("BOT TOKEN")
dp = updater.dispatcher
__debug("main: Configuring handlers")
dp.add_handler(CommandHandler("start", start))
dp.add_handler(CommandHandler("help", help))
dp.add_handler(CommandHandler("video_at", video_at))
dp.add_handler(CommandHandler("haber_at", haber_at))
__debug("main: Starting idle process")
updater.start_polling()
updater.idle()
if __name__ == "__main__":
main()