-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Hack-The-Travel/develop
v0.1.0 Changes Major * Birth!
- Loading branch information
Showing
7 changed files
with
113 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
# general things to ignore | ||
*.py[cod] | ||
|
||
conf.py | ||
edward.db | ||
|
||
# Mr Developer | ||
.idea/ | ||
.ve/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
Edward England Bot | ||
============================ | ||
|
||
## Telegram bot settings | ||
|
||
### Description | ||
Hello! My name is Edward England. | ||
I can help you keep up to date with news from Russian online travel market. | ||
|
||
Send /subscribe to start receiving news. | ||
Tell the bot to /unsubscribe when you want to stop receiving news. | ||
|
||
|
||
### Commands | ||
subscribe - subscribe the news | ||
unsubscribe - unsubscribe the news |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# -*- coding: utf-8 -*- | ||
import telebot | ||
import sqlite3 | ||
from conf import TOKEN, DB_NAME, ADMIN_IDS | ||
|
||
bot = telebot.TeleBot(TOKEN) | ||
|
||
|
||
def execute_sql(query): | ||
db_connection = sqlite3.connect(DB_NAME) | ||
cursor = db_connection.cursor() | ||
cursor.execute(query) | ||
rows = cursor.fetchall() | ||
db_connection.commit() | ||
db_connection.close() | ||
return rows | ||
|
||
|
||
def broadcast(messages): | ||
for message in messages: | ||
if message.chat.id not in ADMIN_IDS: | ||
continue | ||
if message.content_type == 'text': | ||
if message.text.startswith('/'): | ||
continue # ignore commands | ||
query = 'SELECT id FROM chat ORDER BY created_at ASC LIMIT 10' | ||
chat_ids = execute_sql(query) | ||
for chat_id in chat_ids: | ||
bot.send_message(chat_id[0], message.text) | ||
|
||
|
||
@bot.message_handler(commands=['subscribe']) | ||
def command_subscribe(message): | ||
chat_id = message.chat.id | ||
query = '''INSERT OR IGNORE INTO chat (id, username, first_name, last_name, type) | ||
VALUES ({id}, '{username}', '{first_name}', '{last_name}', '{type}') | ||
'''.format(id=chat_id, username=message.chat.username, first_name=message.chat.first_name, | ||
last_name=message.chat.last_name, type=message.chat.type) | ||
execute_sql(query) | ||
bot.send_message(chat_id, 'You have been subscribed.') | ||
|
||
|
||
@bot.message_handler(commands=['unsubscribe']) | ||
def command_unsubscribe(message): | ||
chat_id = message.chat.id | ||
query = 'DELETE from chat where id={}'.format(chat_id) | ||
execute_sql(query) | ||
bot.send_message(chat_id, 'You have been unsubscribed.') | ||
|
||
|
||
@bot.message_handler(commands=['help']) | ||
def command_help(message): | ||
chat_id = message.chat.id | ||
bot.send_message( | ||
chat_id, | ||
'\n'.join([ | ||
'I can help you keep up to date with news from Russian online travel market.', | ||
'Add me to contacts and wait for messages...']) | ||
) | ||
|
||
|
||
bot.set_update_listener(broadcast) | ||
bot.polling() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# -*- coding: utf-8 -*- | ||
# It's sample config | ||
# Add conf.py with correct values | ||
TOKEN = '999999999:ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ' # ask @BotFather | ||
DB_NAME = 'edward.db' | ||
ADMIN_IDS = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# -*- coding: utf-8 -*- | ||
import sqlite3 | ||
from conf import DB_NAME | ||
|
||
|
||
if __name__ == '__main__': | ||
db_connection = sqlite3.connect(DB_NAME) | ||
cursor = db_connection.cursor() | ||
cursor.execute('''CREATE TABLE IF NOT EXISTS chat ( | ||
id INTEGER PRIMARY KEY, | ||
username VARCHAR, | ||
first_name VARCHAR, | ||
last_name VARCHAR, | ||
type VARCHAR, | ||
created_at integer(4) not null default (strftime('%s','now')) | ||
)''') | ||
db_connection.commit() | ||
db_connection.close() |