Skip to content

Commit

Permalink
Merge branch 'main' of github.com:lek-x/tg_weather_bot
Browse files Browse the repository at this point in the history
  • Loading branch information
lek-x committed Jul 15, 2023
2 parents c08f19d + f0a773e commit 07a11b0
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 31 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/build_dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ on:
branches:
- dev

pull_request:
branches:
- main

env:
path_bot: bot_code
path_nomad: nomad_code
Expand Down
6 changes: 2 additions & 4 deletions .github/workflows/build_prod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ on:
required: true
default: 'prod'
options:
- dev
- prod

pull_request:
branches:
- main
types:
- closed

env:
path_bot: bot_code
Expand Down Expand Up @@ -74,9 +75,6 @@ jobs:
- name: Terraform Init
id: init
run: terraform init
env:
https_proxy: '164.92.180.74:3128'
http_proxy: '164.92.180.74:3128'

- name: Terraform Validate
id: validate
Expand Down
9 changes: 2 additions & 7 deletions .github/workflows/check_nomad.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,14 @@ on:
options:
- dev
- prod
push:
branches:
- dev

pull_request:
branches:
- main

env:
path_bot: bot_code
path_nomad: nomad_code
image_name: tgbot
image_name: tgwbot
repo: ghcr.io
job_env: dev
ver: 0
Expand All @@ -44,8 +40,7 @@ jobs:

- name: Checkout current repo
uses: actions/checkout@v3
with:
path: ${{env.path_nomad}}


Rendering_nomad_file:
runs-on:
Expand Down
2 changes: 2 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ repos:
name: pylint
entry: pylint
language: system
args:
- --fail-under=7
types:
- python
- repo: 'https://github.com/gruntwork-io/pre-commit'
Expand Down
104 changes: 104 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Telegram Weather bot with CI/CD deploying pipline.

## Description:
This repo contains Python code of telegram bot and CI/CD code for deployig in dev/prod environments:

## Bot telegram id [PROD version]

https://t.me/weather_rms_bot


## About App:
It is a python based app Telegram bot

## Features:

- **Free weather API** - bot uses free Open Weather API

- **Weather autosend function** - bot sends Weather at desired time.




## Requrements:
- Linux based OS
- Terraform >= 1.0
- Docker
- Anchore Grype tool
- HashiCorp Vault
- HashiCorp Nomad
- Github runner
- poetry
- Python > 3.7
- pip


```mermaid
---
title: Scheme
---
graph TD
X[user]
subgraph A[GitHuB]
B[Repository]
end
subgraph C[Linux VM]
direction TB
subgraph D[NOMAD Cluster]
E[PostgreSQL 15]
F[Weather Bot]
F <-.Storing data.-> E
end
F -- Get secrets and vars --> G
E -- Get secrets and vars-->G
D -- Get images-->A
subgraph G[Vault]
H[Secret1]
J[Secret2]
K[SecretN]
end
end
X <--> F
```

```mermaid
---
title: CI/CD Environments Logic
---
flowchart LR;
A[On push to DEV] --> B[Auto Deploy to Dev]
C[On PR merged into main] --> E[Nomad Plan]-->D[Auto start PROD deploying]
```


```mermaid
---
title: DEV/PROD Pipeline steps
---
flowchart LR;
A[Clean curent directory + \ndocker system prune] --> B[Checkout] --> C[Building Docker Image\n and push to registry] --> D[Check image\nby Anchore Grype] --> E[Rendering Terraform template\n for Nomad] -->F[Nomad job run]
```


## Main files:
1. Dockerfile - to build app
2. main.py - app core
3. .github/ - CI/CD workflow
4. bot.tpl - terraform template
5. entrypoint.sh - entrypoint for container
6. main.tf - terraform main file
7. .pre-commit-config.yml - config for pre-commit tool
8. requiremenets.txt - python packages for app

## Quick start:
TBD


## Known bugs and limitations
1. Hourly weather may show the wrong hour, will be fixed in next releases.
2. It is small possibility that bot sends auto message twice, default check interval 58 seconds.


## License
GNU GPL v3
12 changes: 7 additions & 5 deletions bot.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ job "wtbot-${job_env}" {
mode = "delay"
}
update {
max_parallel = 1
max_parallel = 2
min_healthy_time = "5s"
healthy_deadline = "3m"
auto_revert = false
auto_revert = true
canary = 0
}

Expand Down Expand Up @@ -132,9 +132,11 @@ job "wtbot-${job_env}" {
port = "botapp"
check {
name ="alive"
type = "tcp"
interval = "10s"
timeout = "2s"
type = "script"
command = "/bin/bash"
args = ["-c","test -f /app/main.py" ]
interval = "20s"
timeout = "7s"
}
}

Expand Down
50 changes: 39 additions & 11 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

## vars
timezone = pytz.timezone("Europe/Istanbul")
DBCHECK_INTERVAL = 50
DBCHECK_INTERVAL = 58

emoji = {
"0": "Clear \u2600\ufe0f",
Expand Down Expand Up @@ -78,6 +78,20 @@
logger = telebot.logger
telebot.logger.setLevel(logging.INFO)


conn = psycopg2.connect(params)
cur = conn.cursor()

while True:
try:
cur.execute("SELECT * FROM information_schema.tables limit 1")
testcont = cur.fetchone()
if testcont[1] == "pg_catalog" or testcont[1] == "public":
break
except Exception as er:
print(er)


### Start Initial Block ###
def create_table():
"""Creating 3 tables if they are not exist"""
Expand Down Expand Up @@ -106,16 +120,18 @@ def create_table():
cur = conn.cursor()
for command in commands:
cur.execute(command)
conn.commit()

except (Exception, psycopg2.DatabaseError) as error:
print(error, error.pgerror, error.diag.message_detail)

finally:
if conn is not None:
conn.commit()
conn.close()


create_table()

### END Initial Block ###

### Start Functions Block ###
Expand Down Expand Up @@ -266,7 +282,7 @@ def start(message):
"""
bot.send_message(
message.chat.id,
"Hello! \nI can show you the weather today in any city and send planned notification.\nPlease send me the name of the city.\nSend 'help' for showing commands",
"Hello! \nI can show you the weather today in any city and send planned notification.\nPlease send me the name of the city.\nBefore configuring autosend function send my any city name to remember you\nSend 'help' for showing commands",
)


Expand All @@ -291,7 +307,7 @@ def status(message):
finally:
if conn is not None:
conn.close()
send_enabled = "Enabled" if user_status[1] == True else "Disabled"
send_enabled = "Enabled" if user_status[1] is True else "Disabled"
usr_id = user_status[2]
set_time = user_status[3].strftime("%H:%M")
set_city = user_status[4]
Expand All @@ -316,6 +332,17 @@ def auto_send(message):
bot.register_next_step_handler(message, get_switch)


@bot.message_handler(commands=["help"])
def help(message):
"""
function for showing help
"""
bot.send_message(
message.chat.id,
"Before activating auto notification send to bot at least one city name\n/status - Show details about planned notification\n/auto - Activate auto notification configuration dialogue\n",
)


def get_switch(message):
"""Function for reciveing meassage, for enabling auto_send"""
message_str = message.text
Expand Down Expand Up @@ -352,11 +379,14 @@ def get_weather(message):
"""
func for get weather and sending it to user
"""
if re.match("help|Help",str(message.text)):
bot.reply_to(message,"/status - show details about planned notification\n/auto - activate auto notification configuration dialogue\n")

elif re.match("Glory to Ukraine|Slava Ukraine|glory to ukraine|slava ukraine|Слава Украине|слава украине",str(message.text)):
bot.reply_to(message,"Героям Слава!\U0001f1fa\U0001f1e6\nGlory to the Heroes!\U0001f1fa\U0001f1e6")
if re.match(
"Glory to Ukraine|Slava Ukraine|glory to ukraine|slava ukraine|Слава Украине|слава украине|Слава Україні|cлава україні|слава Україні",
str(message.text),
):
bot.reply_to(
message,
"Героям Слава!\U0001f1fa\U0001f1e6\nGlory to the Heroes!\U0001f1fa\U0001f1e6",
)

else:
### Emoji block for weather status
Expand Down Expand Up @@ -442,8 +472,6 @@ def get_weather(message):
bot.send_message(message.chat.id, "I can't find this city. Try again.")


create_table()

Thread(target=schedule_checker).start()
# bot.polling(non_stop=True, interval=0)
bot.infinity_polling()

0 comments on commit 07a11b0

Please sign in to comment.