-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add configuration file and advanced features
- Loading branch information
Showing
6 changed files
with
1,962 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,76 @@ | ||
# telegram-bot | ||
A simple Telegram bot that provides helpful commands. | ||
<div align="center"> | ||
<img src="src/img/icon.png" alt="Telegram Bot" width="200" height="200"> | ||
<h1>Telegram Bot</h1> | ||
<p>A simple Telegram bot that provides helpful commands.</p> | ||
<a href="#features"><strong>Features</strong></a> • | ||
<a href="#installation"><strong>Installation</strong></a> • | ||
<a href="#dependencies"><strong>Dependencies</strong></a> • | ||
<a href="#logging"><strong>Logging</strong></a> | ||
</div> | ||
|
||
--- | ||
|
||
# Overview | ||
|
||
**Telegram Bot** is a basic, informative Telegram bot that interacts with users via simple commands. It's built using the [node-telegram-bot-api](https://github.com/yagop/node-telegram-bot-api) library. | ||
|
||
## Features | ||
|
||
- `/start` - Sends a welcome message when interacting with the bot. | ||
- `/help` - Lists all available commands for the user. | ||
- `/info` - Provides information about the bot. | ||
- `/joke` - Fetches a random joke from an external joke API and sends it to the user. | ||
- Echoes any non-command text sent to the bot. | ||
|
||
## Installation | ||
|
||
1. Clone this repository: | ||
|
||
```bash | ||
git clone https://github.com/Jesewe/telegram-bot.git | ||
cd telegram-bot | ||
``` | ||
|
||
2. Install the required dependencies: | ||
|
||
```bash | ||
npm install | ||
``` | ||
|
||
3. Create a `config.json` file in the root directory with the following structure: | ||
|
||
```json | ||
{ | ||
"token": "YOUR_REAL_BOT_TOKEN_HERE", | ||
"botName": "Telegram Bot" | ||
} | ||
``` | ||
|
||
4. Run the bot: | ||
|
||
```bash | ||
node bot.js | ||
``` | ||
|
||
## Dependencies | ||
|
||
- **[node-telegram-bot-api](https://github.com/yagop/node-telegram-bot-api)**: Telegram Bot API wrapper for Node.js. | ||
- **[axios](https://axios-http.com/)**: HTTP client for making requests to the external joke API. | ||
|
||
## Logging | ||
|
||
When the bot starts, it will log the following to the console: | ||
|
||
```bash | ||
Telegram Bot is running... | ||
``` | ||
|
||
Any polling errors or issues fetching the joke will also be logged in the console with error details. For example: | ||
|
||
```bash | ||
Error fetching joke: Error message here | ||
``` | ||
|
||
## License | ||
|
||
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
const TelegramBot = require('node-telegram-bot-api'); | ||
const fs = require('fs'); | ||
const axios = require('axios'); | ||
|
||
// Load the configuration file | ||
const config = JSON.parse(fs.readFileSync('config.json', 'utf8')); | ||
|
||
// Create the bot with the token from the config file | ||
const bot = new TelegramBot(config.token, { polling: true }); | ||
|
||
// Log when the bot is running | ||
console.log(`${config.botName} is running...`); | ||
|
||
// Welcome message when user starts the bot | ||
bot.onText(/\/start/, (msg) => { | ||
const chatId = msg.chat.id; | ||
const welcomeMessage = `Hello, ${msg.from.first_name}! Welcome to ${config.botName}. I can assist you with various tasks. Type /help to see what I can do.`; | ||
bot.sendMessage(chatId, welcomeMessage); | ||
}); | ||
|
||
// Help command to list available commands | ||
bot.onText(/\/help/, (msg) => { | ||
const chatId = msg.chat.id; | ||
const helpMessage = `Here are the commands you can use:\n | ||
/start - Start interacting with the bot\n | ||
/help - List available commands\n | ||
/info - Get more information about the bot\n | ||
/joke - Get a random joke`; | ||
bot.sendMessage(chatId, helpMessage); | ||
}); | ||
|
||
// Info command to provide information about the bot | ||
bot.onText(/\/info/, (msg) => { | ||
const chatId = msg.chat.id; | ||
const infoMessage = `This bot is called ${config.botName}. It was created to demonstrate a basic Telegram bot structure.`; | ||
bot.sendMessage(chatId, infoMessage); | ||
}); | ||
|
||
// Joke command | ||
bot.onText(/\/joke/, async (msg) => { | ||
const chatId = msg.chat.id; | ||
|
||
try { | ||
// Fetch a random joke from the Official Joke API | ||
const response = await axios.get('https://official-joke-api.appspot.com/random_joke'); | ||
const joke = response.data; | ||
|
||
// Send the joke to the user | ||
const jokeMessage = `${joke.setup}\n\n${joke.punchline}`; | ||
bot.sendMessage(chatId, jokeMessage); | ||
|
||
} catch (error) { | ||
// Handle any errors | ||
bot.sendMessage(chatId, "Sorry, I couldn't fetch a joke at the moment. Please try again later."); | ||
console.error('Error fetching joke:', error); | ||
} | ||
}); | ||
|
||
// Echo any non-command message | ||
bot.on('message', (msg) => { | ||
const chatId = msg.chat.id; | ||
const text = msg.text; | ||
|
||
// Ignore commands (starting with /) | ||
if (!text.startsWith("/")) { | ||
bot.sendMessage(chatId, `You said: ${text}`); | ||
} | ||
}); | ||
|
||
// Gracefully handle polling errors | ||
bot.on('polling_error', (error) => { | ||
console.error('Polling error:', error.code); // Output polling error codes | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"token": "YOUR_API_KEY_HERE", | ||
"botName": "Telegram Bot" | ||
} |
Oops, something went wrong.