-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathscript.py
More file actions
44 lines (32 loc) · 1.58 KB
/
script.py
File metadata and controls
44 lines (32 loc) · 1.58 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
# Telethon utility # pip install telethon
from telethon import TelegramClient, events
from telethon.tl.custom import Button
import configparser # Library for reading from a configuration file, # pip install configparser
import datetime # Library that we will need to get the day and time, # pip install datetime
#### Access credentials
config = configparser.ConfigParser() # Define the method to read the configuration file
config.read('config.ini') # read config.ini file
api_id = config.get('default','api_id') # get the api id
api_hash = config.get('default','api_hash') # get the api hash
BOT_TOKEN = config.get('default','BOT_TOKEN') # get the bot token
# Create the client and the session called session_master. We start the session as the Bot (using bot_token)
client = TelegramClient('sessions/session_master', api_id, api_hash).start(bot_token=BOT_TOKEN)
# Define the /start command
@client.on(events.NewMessage(pattern='/(?i)start'))
async def start(event):
sender = await event.get_sender()
SENDER = sender.id
text = "Docker Bot 🤖 ready\nHello! I'm answering you from Docker!"
await client.send_message(SENDER, text, parse_mode="HTML")
### First command, get the time and day
@client.on(events.NewMessage(pattern='/(?i)time'))
async def time(event):
# Get the sender of the message
sender = await event.get_sender()
SENDER = sender.id
text = "Received! Day and time: " + str(datetime.datetime.now())
await client.send_message(SENDER, text, parse_mode="HTML")
### MAIN
if __name__ == '__main__':
print("Bot Started!")
client.run_until_disconnected()