telegram bot api镜像类,具有数据集驱动的sqlite数据库。
davtelepot的Python项目详细描述
达夫特利波特
这个项目使用类Bot
方便地镜像了telegram bot api。
请注意,运行异步代码需要python3.5+。
检查Requirements.txt中的第三方依赖项。
查看help(Bot)
了解详细信息。
项目文件夹
davtelepot/data
文件夹
config.py
包含配置设置(例如证书路径、本地主机、端口等)passwords.py
包含要被git忽略的机密信息(例如bot令牌)*.db
文件是bots使用的sqlite数据库*.log
:日志文件(在data/config.py
模块中存储日志文件名和错误文件名)
examples
文件夹
此文件夹包含对简单davtelepot.bot telegram bots的完整注释和准备运行示例。
用法
import sys
from davtelepot.bot import Bot
from data.passwords import my_token, my_other_token
long_polling_bot = Bot(token=my_token, database_url='my_db')
webhook_bot = Bot(token=my_other_token, hostname='example.com',
certificate='path/to/certificate.pem',
database_url='my_other_db')
@long_polling_bot.command('/foo')
async def foo_command(bot, update, user_record):
return "Bar!"
@webhook_bot.command('/bar')
async def bar_command(bot, update, user_record):
return "Foo!"
exit_state = Bot.run(
local_host='127.0.0.5',
port=8552
)
sys.exit(exit_state)
查看help(Bot)
了解详细信息。
Webhook附加信息
要在webhook模式中运行bot,必须在bot实例化时提供hostname
和certificate
,在调用Bot.run
方法时提供local_host
和port
。
- 电报将在
https://{hostname}/webhook/{tokens}/
发送POST请求,使用certificate
进行加密 aiohttp.web.Application
服务器将在http://{local_host}:{port}
上侦听更新
因此,需要一个反向代理将传入的请求传递给本地主机。
用于此目的的nginx反向代理示例
server{listen8553ssl;listen[::]:8553ssl;server_nameexample.comwww.example.com;location/telegram/{proxy_passhttp://127.0.0.5:8552/;}ssl_certificate/path/to/fullchain.pem;ssl_certificate_key/path/to/privkey.pem;}
这种情况下的python配置文件示例
# File data/config.py, gitignored and imported in main scripthostname="https://www.example.com:8553/telegram"certificate="/path/to/fullchain.pem"local_host="127.0.0.5"port=8552# Main scriptfromdata.configimporthostname,certificate,local_host,portfromdata.passwordsimportbot_tokenfromdavtelepot.botimportBotmy_bot=Bot(token=bot_token,hostname=hostname,certificate=certificate)# ...Bot.run(local_host=local_host,port=port)