繁体   English   中英

如何使用 asp.net 和 c# 发送电报消息?

[英]How can I send telegram message using asp.net and c#?

我用c#写了一个asp代码,用来给一个手机号码发送消息。 我有一个有效的 api_id 和 api_hash。 该程序在 C# windows 窗体应用程序中运行良好。 但是,我在 ASP.net 上遇到了一个新错误,在bool isConnected = await telegram.ConnectAsync();上使用 C# .

错误信息是:

mscorlib.dll 中发生了“System.IO.FileNotFoundException”类型的异常,但未在用户代码中处理。 附加信息:无法加载文件或程序集“Ionic.ZLib,版本=2.0.0.14,Culture=neutral,PublicKeyToken=null”或其依赖项之一。 该系统找不到指定的文件。

我该如何解决此类错误?

ASP.NET:

    %@ Page Language="C#" Async="true" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
        <table class="auto-style1">
            <tr>
                <td>
                    <asp:Label ID="Label1" runat="server" Text="Mobile Number"></asp:Label>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="Label2" runat="server" Text="Message"></asp:Label>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="Button1" runat="server" Text="Send" OnClick="Button1_Click" />
                </td>
            </tr>
        </table>
    <div>

    </div>
    </form>
</body>
</html>

C#:

using System;
using System.Linq;
using TLSharp.Core;
using TeleSharp.TL;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected async void Button1_Click(object sender, EventArgs e)
    {
        string Alert="";
        string phoneNumber = "*********";
        int api_id = ******; string api_hash = "********************";
        var store = new FileSessionStore();
        TLUser user = null;
        var client = new TelegramClient(api_id, api_hash, store);
        bool isConnected = await client.ConnectAsync();
        if (!isConnected)
            Alert = "Check wifi connection!";
        while (!client.IsUserAuthorized())
        {
            var phoneCodeHash = await client.SendCodeRequestAsync(phoneNumber);
            Alert = "Input verification code on your phone here:";
            var code = "*****"; // you can change code in debugger, code will send via telegram to you
            user = await client.MakeAuthAsync(phoneNumber, phoneCodeHash, code);
        }
        string[] phonelist = new string[2];

        if (client.IsUserAuthorized())
        {
            //get available contacts
            var result = await client.GetContactsAsync();

            //find recipient in contacts
            var userr = result.users.lists.Where(x => x.GetType() == typeof(TLUser)).Cast<TLUser>().FirstOrDefault(x => x.phone == "98"+TextBox1.Text);

            //send message
            await client.SendMessageAsync(new TLInputPeerUser() { user_id = userr.id }, TextBox2.Text);
        }
    }
}

Asp.net 网页和代码文件是在用户第一次请求资源时动态编译的。

因此,在您的代码中您引用了第三方程序集。 IDE 中的所有内容都将正确验证。 但在实际使用中,第三方程序集引用了另一个程序集。 在这种情况下,Ionic.ZLib 程序集。

添加缺少的程序集的最简单方法是使用Ionic.Zlib Nuget 包

Package Manager
PM>  Install-Package Ionic.Zlib -Version 1.9.1.5
>  dotnet add package Ionic.Zlib --version 1.9.1.5

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM