Skip to content

Instantly share code, notes, and snippets.

@twkang
Created August 17, 2021 03:21
Show Gist options
  • Save twkang/4b388d411c82c4c95cb864dcab32e029 to your computer and use it in GitHub Desktop.
Save twkang/4b388d411c82c4c95cb864dcab32e029 to your computer and use it in GitHub Desktop.
Simple python example to send telegram message
import cx_Oracle
from telethon import TelegramClient
db_server = '1.2.3.4'
db_service = 'DBSID1'
db_id = 'dbuserid'
db_pw = 'dbpassword'
db_sql = """
select to_char(sysdate 'YYYYMMDD') from dual
"""
# using @BotFather
bot_token = '_TELEGRAM_BOT_TOKEN_'
# https://my.telegram.org/apps
api_id = '_TELEGRAM_API_ID_'
api_hash = '_TELEGRAM_API_HASH_'
# to get the group ID, using @getidsbot
# or https://api.telegram.org/bot<bot_token>/getUpdates
# be careful - group id changes when it's upgraded to supergroup
recv_ids = [-10000000, -10000001]
def read_data():
dsn = cx_Oracle.makedsn(db_server, 1521, service_name=db_service)
conn = cx_Oracle.connect(db_id, db_pw, dsn, encoding="UTF-8")
csr = conn.cursor()
csr.execute(db_sql)
rset = csr.fetchall()
msg = rset[0][0]
return msg
msg = read_data()
# The first parameter is the .session file name (absolute paths allowed)
bot = TelegramClient('bot', api_id, api_hash).start(bot_token=bot_token)
async def send_message():
for rid in recv_ids:
await bot.send_message(rid, msg)
with bot:
bot.loop.run_until_complete(send_message())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment