Skip to content

Commit

Permalink
Merge pull request #2 from Hack-The-Travel/develop
Browse files Browse the repository at this point in the history
v0.1.0 Changes

Major

* Birth!
  • Loading branch information
gurza authored Jun 16, 2018
2 parents f191adc + 8e3647d commit 2af2014
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
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/
8 changes: 7 additions & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
Release History
===============

0.1.0 (2018-06-16)
++++++++++++++++++

* Birth!


0.0.1 (2018-06-11)
++++++++++++++++++

* Frustration
* Conception
* Conception
16 changes: 16 additions & 0 deletions README.md
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
2 changes: 0 additions & 2 deletions README.rst

This file was deleted.

63 changes: 63 additions & 0 deletions bot.py
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()
6 changes: 6 additions & 0 deletions conf.sample.py
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 = []
18 changes: 18 additions & 0 deletions manage.py
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()

0 comments on commit 2af2014

Please sign in to comment.