Skip to content
This repository has been archived by the owner on Dec 15, 2022. It is now read-only.

Commit

Permalink
在準備部署 Heroku 之前
Browse files Browse the repository at this point in the history
也就是在你開發完成機器人的狀態
  • Loading branch information
hms5232 committed May 20, 2020
0 parents commit a361bd2
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

[*]
indent_style = tab
indent_size = 4
insert_final_newline = true
charset = utf-8
curly_bracket_next_line = false
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config.ini
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 hms5232

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
52 changes: 52 additions & 0 deletions bot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/usr/bin/env python3
# -*- coding:utf-8 -*-

"""
Author:hms5232
Repo:https://github.com/hms5232/deploy-python-telegram-bot-on-heroku
"""


from configparser import ConfigParser

from telegram.ext import Updater, CommandHandler


# 設定一些個人的環境變數
env = ConfigParser()
env.read('config.ini')
# If you don't want use config.ini, please edit following variables for your environment.
TOKEN = env['bot']['TOKEN']


updater = Updater(token=TOKEN) # 呼叫 bot 用


def welcome(bot, update):
""" Show user welcome message. """

chat_id = update.message.from_user.id

about_bot = ''
about_bot = about_bot + 'Hello! 感謝您的使用\n'
about_bot = about_bot + '本機器人由 [hms5232](https://medium.com/@hms5232) 開發\n'
about_bot = about_bot + '用於將機器人部署在 Heroku 上的教學文的範例\n'
about_bot = about_bot + '可於 [Github](https://github.com/hms5232) 上找到我\n'
about_bot = about_bot + '文章原文可在[repo簡介](https://github.com/hms5232/deploy-python-telegram-bot-on-heroku)上找到\n'

bot.send_message(chat_id, about_bot, parse_mode='Markdown')


def hello(bot, update):
""" Hello World! """

update.message.reply_text('Hello world!')


updater.dispatcher.add_handler(CommandHandler('start', welcome)) # 歡迎訊息
updater.dispatcher.add_handler(CommandHandler('hi', hello)) # Hello World!


# 執行機器人必須要的,讓機器人運作聽命
updater.start_polling()
updater.idle()
2 changes: 2 additions & 0 deletions config.ini.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[bot]
TOKEN =

0 comments on commit a361bd2

Please sign in to comment.