-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
151 lines (124 loc) · 3.99 KB
/
Copy pathmain.py
File metadata and controls
151 lines (124 loc) · 3.99 KB
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
#!/usr/bin/env python
# pylint: disable=C0116
# python c:\Users\HYPERPC\PycharmProjects\pythonProject\main.py
import logging
from credentials import *
from telegram import Update
from telegram import InlineKeyboardButton
from telegram import InlineKeyboardMarkup
from telegram.ext import CommandHandler, MessageHandler, CallbackContext, Updater
import requests
from bs4 import BeautifulSoup
from telegram.ext import CallbackQueryHandler
import datetime
import pytz
# Enable logging
import database
logging.basicConfig(
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO
)
logger = logging.getLogger(__name__)
def button():
keyboard = [
[
InlineKeyboardButton(text="Текущий курс валют", callback_data="money")
]
]
return InlineKeyboardMarkup(keyboard)
def amogus():
currency = ["BTC", "ETH", "USD", "EUR", "CNY"]
emoji = [
"🤑",
"😁",
"😎",
"🤨",
"😥"
]
result = ""
count = 0
for item in currency:
r = requests.get(f"https://pokur.su/{item}/rub/1/")
soup = BeautifulSoup(r.text, 'lxml')
val = soup.find("div", attrs={"class": "blockquote-classic"}).text.strip()
result += f"{emoji[count]} {val}\n"
count += 1
return result
def get_money(update: Update, _: CallbackContext):
result = amogus()
if result.strip() == update.effective_message.text:
pass
else:
update.callback_query.edit_message_text(
text=result,
reply_markup=button()
)
def morning(context: CallbackContext):
ids = database.get_id()
for id in ids:
context.bot.send_message(
chat_id=id,
text=amogus(),
reply_markup=button()
)
def evening(context: CallbackContext):
ids = database.get_id()
for id in ids:
context.bot.send_message(
chat_id=id,
text=amogus(),
reply_markup=button()
)
# Define a few command handlers. These usually take the two arguments update and
# context.
def start(update: Update, context: CallbackContext) -> None:
if context.job_queue.get_jobs_by_name("morning"):
pass
else:
context.job_queue.run_daily(
morning,
time=datetime.time(
hour=9,
minute=00,
tzinfo=pytz.timezone('Europe/Moscow')
)
)
if context.job_queue.get_jobs_by_name("evening"):
pass
else:
context.job_queue.run_daily(
evening,
time=datetime.time(
hour=20,
minute=00,
tzinfo=pytz.timezone('Europe/Moscow')
)
)
ids = database.get_id()
if update.effective_user.id in ids:
pass
else:
database.insert_into_db(update.effective_user.id)
update.message.reply_text(
text="Hey, I am a cripto_bot 😎",
reply_markup=button()
)
logging.getLogger().info(update.effective_user.id)
def main() -> None:
"""Start the bot."""
# Create the Updater and pass it your bot's token.
updater = Updater(token)
button_handler = CallbackQueryHandler(callback=get_money)
# Get the dispatcher to register handlers
dispatcher = updater.dispatcher
# on different commands - answer in Telegram
dispatcher.add_handler(CommandHandler("start", start))
dispatcher.add_handler(button_handler)
# on non command i.e message - echo the message on Telegram
# Start the Bot
updater.start_polling()
# Run the bot until you press Ctrl-C or the process receives SIGINT,
# SIGTERM or SIGABRT. This should be used most of the time, since
# start_polling() is non-blocking and will stop the bot gracefully.
updater.idle()
if __name__ == '__main__':
main()