forked from Otxoto/Bot-examples
-
Notifications
You must be signed in to change notification settings - Fork 6
/
timer_bot.py
47 lines (31 loc) · 1.12 KB
/
timer_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
# *- coding: utf-8 -*-
#This script shows how to program a timer in a secondary thread.
#this timer can be used to execute tasks from time to time or at a specific time
import telebot
import time, datetime
import threading
bot = telebot.TeleBot('TOKEN')
chat_id = None
def tic_tac():
i = 0
while True:
this_moment = datetime.datetime.now()
#Executes each 60 secondss
if ((i % 60) == 0):
if chat_id:
bot.send_message(chat_id, "Hello! How are you?")
#Check if it is 12:00:00; if it is true sends the message
if this_moment.hour == 12 and this_moment.minute == 0 and this_moment.second == 0:
if chat_id:
bot.send_message(chat_id, "It is the noon!")
i += 1
time.sleep(1)
#Needs to be started to know the chat id to send messages
@bot.message_handler(commands=['start'])
def save_chat_id(message):
global chat_id
chat_id = message.chat.id
bot.send_message(chat_id, 'I got it', reply_to_message_id=message.message_id)
timThr = threading.Thread(target=tic_tac)
timThr.start()
bot.polling(none_stop=True)