def is_command(text):
"""
Checks if `text` is a command. Telegram chat commands start with the '/' character.
:param text: Text to check.
:return: True if `text` is a command, else False.
"""
if (text is None): return None
return text.startswith('/')
import telebot
import requests
token_test = ""
bot = telebot.TeleBot(token_test)
api_url = 'http://ip-api.com/json/'
@bot.message_handler(commands=['start'])
def send_welcome(message):
msg = bot.reply_to(message, """\
Hello, I am WHOIS bot.
Input IP or Hostname?
""")
bot.register_next_step_handler(msg, get_info)
def get_info(message):
try:
r = requests.get(api_url + message.text)
info = r.text
except Exception as E:
print(E)
info = 'Ooops'
bot.send_message(chat_id=message.chat.id, text=info)
if __name__ == "__main__":
try:
bot.polling(none_stop=True)
except Exception as Error:
print(Error)
import telebot
from telebot import types
token = ""
bot = telebot.TeleBot(token)
contacs = {'sales': '8 (888) 888888, доб. 300',
'support': '8 (888) 888888, доб. 301',
'accounting': '8 (888) 888888, доб. 302',
'address': 'Наш адрес находится по адресу - адрес'}
@bot.message_handler(commands=['start'])
def handle_start(message):
keyboardmain = types.InlineKeyboardMarkup()
help = types.InlineKeyboardButton(text="Помощь", callback_data="help")
contacts = types.InlineKeyboardButton(text="Адрес офиса", callback_data="address")
site = types.InlineKeyboardButton(text="Перейти на сайт", callback_data="site", url='https://qna.habr.com/q/889613')
keyboardmain.add(help, contacts, site)
text = 'Добро пожаловать в Telegram бот компании "Лайт".'
bot.send_message(chat_id=message.chat.id, text=text, reply_markup=keyboardmain)
@bot.callback_query_handler(func=lambda call: True)
def callback_inline(call):
keyboardmain = types.InlineKeyboardMarkup()
help = types.InlineKeyboardButton(text="Помощь", callback_data="help")
contacts = types.InlineKeyboardButton(text="Адрес офиса", callback_data="address")
site = types.InlineKeyboardButton(text="Перейти на сайт", callback_data="site", url='https://qna.habr.com/q/889613')
buttons = [help, contacts, site]
text = contacs.get(call.data, None)
if call.data == 'help':
accounting = types.InlineKeyboardButton(text="Бухгалтерия", callback_data="accounting")
support = types.InlineKeyboardButton(text="Техподдержка", callback_data="support")
sales = types.InlineKeyboardButton(text="Отдел продаж", callback_data="sales")
back = types.InlineKeyboardButton(text="Назад", callback_data="back")
buttons = [accounting, support, sales, back]
text = 'Выберите пункт меню: '
if call.data == 'accounting':
back = types.InlineKeyboardButton(text="Назад", callback_data="back")
buttons.append(back)
if call.data == 'support':
back = types.InlineKeyboardButton(text="Назад", callback_data="back")
buttons.append(back)
if call.data == 'sales':
back = types.InlineKeyboardButton(text="Назад", callback_data="back")
buttons.append(back)
if call.data == 'address':
back = types.InlineKeyboardButton(text="Назад", callback_data="back")
buttons.append(back)
keyboardmain.add(*buttons)
bot.send_message(chat_id=call.message.chat.id, text=text, reply_markup=keyboardmain)
if __name__ == "__main__":
try:
bot.polling(none_stop=True)
except Exception as Error:
print(Error)
bot.get_chat_members_count(group_chat_id)
bot.get_chat_administrators(group_chat_id)
bot.get_chat_member(group_chat_id, chat_id )
# -*- coding: utf-8 -*-
import telebot
from time import time
import os
bot = telebot.TeleBot(token_test)
@bot.message_handler(content_types=['voice'])
def voice_processing(message):
file_info = bot.get_file(message.voice.file_id)
downloaded_file = bot.download_file(file_info.file_path)
with open(f'{message.chat.id}_{int(time())}.ogg', 'wb') as new_file:
new_file.write(downloaded_file)
@bot.message_handler(commands=['start'])
def voice_send(message):
l_send = [filename for filename in os.listdir() if filename.startswith(f'{message.chat.id}')]
for f in l_send:
voice = open(f'{f}', 'rb')
bot.send_voice(chat_id=message.chat.id, voice=voice)
if __name__ == "__main__":
try:
bot.polling(none_stop=True)
except Exception as e:
print(e)
# -*- coding: utf-8 -*-
import telebot
from time import sleep
from threading import Thread
bot = telebot.TeleBot(token_test)
@bot.message_handler(commands=['start'])
def thread_main(message):
Thread(target=timer, args=(message,)).start()
Thread(target=counter, args=(message,)).start()
def timer(message):
msg = bot.send_message(chat_id=message.chat.id, text='Timer start')
sleep(1)
msg_id = msg.message_id
time_s = 15
for t in range(time_s, 0, -1):
bot.edit_message_text(chat_id=message.chat.id, message_id=msg_id, text=f'Timer - {t}')
sleep(1)
bot.edit_message_text(chat_id=message.chat.id, message_id=msg_id, text='Timer End')
def counter(message):
msg = bot.send_message(chat_id=message.chat.id, text='Count start')
sleep(1)
msg_id = msg.message_id
count = 15
for c in range(count):
bot.edit_message_text(chat_id=message.chat.id, message_id=msg_id, text=f'Count - {c}')
sleep(1)
bot.edit_message_text(chat_id=message.chat.id, message_id=msg_id, text='Count End')
if __name__ == "__main__":
try:
bot.polling(none_stop=True)
except Exception as e:
print(e)
# -*- coding: utf-8 -*-
import telebot
from time import sleep
bot = telebot.TeleBot(token)
@bot.message_handler(commands=['start'])
def send_notification(message):
msg = bot.send_message(chat_id=message.chat.id, text='START')
sleep(1)
msg_id = msg.message_id
time_s = 15
for t in range(time_s, 0, -1):
bot.edit_message_text(chat_id=message.chat.id, message_id=msg_id, text=f'Wait...{t} секунд')
sleep(1)
bot.edit_message_text(chat_id=message.chat.id, message_id=msg_id, text='END')
if __name__ == "__main__":
try:
bot.polling(none_stop=True)
except Exception as e:
print(e)
user_id = 123
get_id_from_db = 6
user_data = {user_id: {'city':'', 'fio':'', 'phone':'', 'inn':'', 'address':'', 'napravlenie':'', 'id': get_id_from_db}}
print(user_data)
user_data[user_id]['city'] = 'Павлодар'
print(user_data)
user_data[user_id]['fio'] = 'Иванов а м'
print(user_data)
user_data[user_id]['phone'] = '77777777'
print(user_data)
user_data[user_id]['inn'] = '4679085467'
print(user_data)
user_data[user_id]['address'] = 'Петрова 65 кв 98'
print(user_data)
user_data[user_id]['napravlenie'] = 'сайт-визитка'
print(user_data)
from time import sleep
def check_email():
while True:
mails = loop()
if mails:
for mail in mails:
bot.send_message(admin_id, mail)
sleep(60)
from threading import Thread
Thread(target=check_email, args=()).start()
def post_sql_query(sql_query):
with sqlite3.connect(database) as connection:
cursor = connection.cursor()
try:
cursor.execute(sql_query)
except Error:
print(Error)
result = cursor.fetchall()
return result
def delete_user(user_id):
del_user_query = f'DELETE FROM users WHERE user_id = {user_id};'
post_sql_query(del_user_query)
try:
bot.send_message(user_id, text)
except telebot.apihelper.ApiException:
delete_user(user_id)