Skip to content

Commit 0325917

Browse files
authored
Merge pull request Python-World#428 from Alex108-lab/master
Bot project for telegram
2 parents adfdade + 656b6cc commit 0325917

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

projects/telegram_bot/README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#Telegram bot
2+
3+
This is a demo project of a telegram bot
4+
5+
## Dependencies
6+
7+
```
8+
pip install requirements.txt
9+
```
10+
11+
## Search telegram to test the bot
12+
13+
```
14+
@Bottest_bot_bot_bot_bot
15+
```
16+
17+
## Start the program
18+
19+
```
20+
python ./main.py
21+
```
22+
23+
## Author Name
24+
25+
[Alexander Monterrosa](https://github.com/Alex108-lab)

projects/telegram_bot/main.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
'''
2+
BOT FOR TELEGRAM
3+
'''
4+
5+
import random
6+
import logging
7+
from telegram import (ParseMode)
8+
from telegram.ext import (Updater, CommandHandler)
9+
10+
logging.basicConfig(
11+
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
12+
logger = logging.getLogger(__name__)
13+
14+
15+
def error_callback(update, context):
16+
logger.warning('Update "%s" caused error "%s"', update, context.error)
17+
18+
19+
def start(update, context):
20+
'''
21+
Start
22+
'''
23+
context.bot.send_message(update.message.chat_id,
24+
"Welcome! to simple telegram bot", parse_mode=ParseMode.HTML)
25+
26+
'''
27+
We can call other commands, without it being activated in the chat (/ help).
28+
'''
29+
coin(update, context)
30+
31+
32+
def coin(update, context):
33+
'''
34+
⚪️ / ⚫️ Currency
35+
Generate an elatory number between 1 and 2.
36+
'''
37+
cid = update.message.chat_id
38+
39+
msg = "⚫️ face " if random.randint(1, 2) == 1 else "⚪️ cross"
40+
'''
41+
He responds directly on the channel where he has been spoken to.
42+
'''
43+
update.message.reply_text(msg)
44+
45+
46+
def main():
47+
TOKEN = "1914536904:AAF4ZnqNvyg1pk-1pCPzTqhDYggAyf-1CF8"
48+
49+
updater = Updater(TOKEN, use_context=True)
50+
51+
dp = updater.dispatcher
52+
53+
'''
54+
Events that will activate our bot.
55+
'''
56+
dp.add_handler(CommandHandler('start',start))
57+
dp.add_handler(CommandHandler('coin',coin))
58+
59+
dp.add_error_handler(error_callback)
60+
61+
'''
62+
The bot starts
63+
'''
64+
updater.start_polling()
65+
66+
'''
67+
or leave listening. Keep it from stopping.
68+
'''
69+
updater.idle()
70+
71+
72+
if __name__ == '__main__':
73+
print('[Telegram simple bot] Start...')
74+
main()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
python-telegram-bot

0 commit comments

Comments
 (0)